Skip to content
This repository was archived by the owner on Feb 21, 2022. It is now read-only.

Commit 1eb3425

Browse files
committed
[feat] added no-empty-character-class rule (closes #8)
1 parent c92f89e commit 1eb3425

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ The following rules point out areas where you might have made mistakes.
215215
"no-duplicate-case": true
216216
```
217217

218-
* [no-empty-character-class](http://eslint.org/docs/rules/no-empty-character-class) => no-empty-character-class (tslint-eslint-rules) [TODO](https://github.com/buzinas/tslint-eslint-rules/issues/8)
218+
* [no-empty-character-class](http://eslint.org/docs/rules/no-empty-character-class) => no-empty-character-class (tslint-eslint-rules)
219219
* Description: disallow the use of empty character classes in regular expressions (recommended)
220220
* Usage
221221

eslint_tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"no-unexpected-multiline": true,
1212
"no-invalid-regexp": true,
1313
"no-inner-declarations": [true, "both"],
14-
"no-regex-spaces": true
14+
"no-regex-spaces": true,
15+
"no-empty-character-class": true
1516
}
1617
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path='helper.d.ts' />
2+
// import {ts, Lint} from './helper';
3+
4+
export class Rule extends Lint.Rules.AbstractRule {
5+
public static FAILURE_STRING = `don't use empty classes in regular expressions`;
6+
7+
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
8+
const walker = new NoEmptyCharacterClassWalker(sourceFile, this.getOptions());
9+
return this.applyWithWalker(walker);
10+
}
11+
}
12+
13+
class NoEmptyCharacterClassWalker extends Lint.RuleWalker {
14+
protected visitRegularExpressionLiteral(node: ts.LiteralExpression) {
15+
this.validateEmptyCharacterClass(node);
16+
super.visitRegularExpressionLiteral(node);
17+
}
18+
19+
private validateEmptyCharacterClass(node: ts.LiteralExpression) {
20+
if (!(/^\/([^\\[]|\\.|\[([^\\\]]|\\.)+\])*\/[gim]*$/.test(node.getText()))) {
21+
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
22+
}
23+
}
24+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// <reference path='../../../typings/mocha/mocha.d.ts' />
2+
import {makeTest} from './helper';
3+
4+
const rule = 'no-empty-character-class';
5+
const scripts = {
6+
valid: [
7+
'var foo = /^abc[a-zA-Z]/;',
8+
'var regExp = new RegExp(\'^abc[]\');',
9+
'var foo = /^abc/;',
10+
'var foo = /[\\[]/;',
11+
'var foo = /[\\]]/;',
12+
'var foo = /[a-zA-Z\\[]/;',
13+
'var foo = /[[]/;',
14+
'var foo = /[\\[a-z[]]/;',
15+
'var foo = /[\\-\\[\\]\\/\\{\\}\\(\\)\\*\\+\\?\\.\\\\^\\$\\|]/g;',
16+
'var foo = /\\s*:\\s*/gim;'
17+
],
18+
invalid: [
19+
'var foo = /^abc[]/;',
20+
'var foo = /foo[]bar/;',
21+
'if (foo.match(/^abc[]/)) {}',
22+
'if (/^abc[]/.test(foo)) {}',
23+
'var foo = /[]]/;',
24+
'var foo = /\\[[]/;',
25+
'var foo = /\\[\\[\\]a-z[]/;'
26+
]
27+
};
28+
29+
describe(rule, function test() {
30+
it('should pass when not using empty character classes in regular expressions', function testValid() {
31+
makeTest(rule, scripts.valid, true);
32+
});
33+
34+
it('should fail when using empty character classes in regular expressions', function testInvalid() {
35+
makeTest(rule, scripts.invalid, false);
36+
});
37+
});

0 commit comments

Comments
 (0)