added recursive checking for empty matches
DELETE
packages/@romejs/js-compiler/lint/rules/.emptyMatches.ts.swp
packages/@romejs/js-compiler/lint/rules/.emptyMatches.ts.swp
Binary files a/packages/@romejs/js-compiler/lint/rules/.emptyMatches.ts.swp and /dev/null differ

MODIFY
packages/@romejs/js-compiler/lint/rules/emptyMatches.ts
packages/@romejs/js-compiler/lint/rules/emptyMatches.ts
* LICENSE file in the root directory of this source tree.
*/
-import {AnyRegExpBodyItem} from '@romejs/js-ast';
+import {AnyRegExpExpression, AnyRegExpBodyItem} from '@romejs/js-ast';
import {Path, TransformExitResult} from '@romejs/js-compiler';
import {descriptions} from '@romejs/diagnostics';
@@ -13,19 +13,29 @@ function isQuantifiedMinZero(el: AnyRegExpBodyItem): boolean {
return el.type === 'RegExpQuantified' && el.min === 0;
}
+function lintEmptyMatches(expr: AnyRegExpExpression): boolean {
+ if (expr.type === 'RegExpSubExpression') {
+ return expr.body
+ .map((item) => {
+ if (item.type === 'RegExpGroupNonCapture' || item.type === 'RegExpGroupCapture') {
+ return lintEmptyMatches(item.expression);
+ } else {
+ return isQuantifiedMinZero(item);
+ }
+ })
+ .every(el => el === true);
+ } else {
+ return lintEmptyMatches(expr.left) || lintEmptyMatches(expr.right);
+ }
+}
+
export default {
- name: 'noEmptyCharacterClass',
+ name: 'emptyMatches',
enter(path: Path): TransformExitResult {
const {context, node} = path;
- if (
- node.type === 'RegExpLiteral' &&
- node.expression.type === 'RegExpSubExpression'
- ) {
- const {body} = node.expression;
- if (body && body.every(isQuantifiedMinZero)) {
+ if (node.type === 'RegExpLiteral' && lintEmptyMatches(node.expression)) {
context.addNodeDiagnostic(node, descriptions.LINT.EMPTY_MATCHES);
}
- }
return node;
},
};

added recursive checking for empty matches