-
-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
2ab2e58
commit 87f446d
Showing
5 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Enforce the use of `new RegExp()` instead of `RegExp()` | ||
|
||
They work the same, but `new RegExp()` should be preferred for consistency with other constructors. | ||
|
||
|
||
## Fail | ||
|
||
```js | ||
const regexp = RegExp('foo'); | ||
``` | ||
|
||
|
||
## Pass | ||
|
||
```js | ||
const regexp = new RegExp('foo'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
const create = context => { | ||
return { | ||
'CallExpression[callee.name="RegExp"]': node => { | ||
context.report({ | ||
node, | ||
message: 'Use `new RegExp()` instead of `RegExp()`', | ||
fix: fixer => fixer.insertTextBefore(node, 'new ') | ||
}); | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
fixable: 'code' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import test from 'ava'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../rules/new-regexp'; | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
env: { | ||
es6: true | ||
} | ||
}); | ||
|
||
const error = { | ||
ruleId: 'new-regexp', | ||
message: 'Use `new RegExp()` instead of `RegExp()`' | ||
}; | ||
|
||
ruleTester.run('new-regexp', rule, { | ||
valid: [ | ||
`const foo = new RegExp('foo', 'g')`, | ||
`const foo = new RegExp('foo')`, | ||
`const foo = new RegExp(/foo/, 'g')`, | ||
`new RegExp('foo', 'g').exec('foo')` | ||
], | ||
invalid: [ | ||
{ | ||
code: `const foo = RegExp('foo', 'g')`, | ||
errors: [error], | ||
output: `const foo = new RegExp('foo', 'g')` | ||
}, | ||
{ | ||
code: `const foo = RegExp('foo')`, | ||
errors: [error], | ||
output: `const foo = new RegExp('foo')` | ||
}, | ||
{ | ||
code: `const foo = RegExp(/foo/, 'g')`, | ||
errors: [error], | ||
output: `const foo = new RegExp(/foo/, 'g')` | ||
}, | ||
{ | ||
code: `RegExp('foo', 'g').exec('foo')`, | ||
errors: [error], | ||
output: `new RegExp('foo', 'g').exec('foo')` | ||
} | ||
] | ||
}); |