Skip to content

Commit f7b44d2

Browse files
committed
fix: refine rules using selector syntax. Fix edge cases
1 parent 576d29d commit f7b44d2

16 files changed

+94
-199
lines changed

lib/rules/no-async-generator.js

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
module.exports = function(context, badBrowser) {
2-
return {
3-
FunctionDeclaration(node) {
4-
if (node.async && node.generator) {
5-
context.report(
6-
node,
7-
`Async Generators are not supported in ${badBrowser}`
8-
)
9-
}
10-
}
1+
module.exports = (context, badBrowser) => ({
2+
':function[async=true][generator=true]'(node) {
3+
context.report(node, `Async Generators are not supported in ${badBrowser}`)
114
}
12-
}
13-
14-
module.exports.schema = []
5+
})

lib/rules/no-async-iteration.js

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
module.exports = function(context, badBrowser) {
2-
return {
3-
ForOfStatement(node) {
4-
if (node.await) {
5-
context.report(
6-
node,
7-
`Async Iteration is not supported in ${badBrowser}`
8-
)
9-
}
10-
}
1+
module.exports = (context, badBrowser) => ({
2+
'ForOfStatement[await=true]'(node) {
3+
context.report(node, `Async Iteration is not supported in ${badBrowser}`)
114
}
12-
}
13-
14-
module.exports.schema = []
5+
})

lib/rules/no-bind-operator.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
module.exports = function(context, badBrowser) {
2-
return {
3-
BindExpression(node) {
4-
context.report(
5-
node,
6-
`The Bind Operator is not supported in ${badBrowser}`
7-
)
8-
}
1+
module.exports = (context, badBrowser) => ({
2+
BindExpression(node) {
3+
context.report(node, `The Bind Operator is not supported in ${badBrowser}`)
94
}
10-
}
11-
12-
module.exports.schema = []
5+
})

lib/rules/no-do-expression.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
module.exports = function(context, badBrowser) {
2-
return {
3-
DoExpression(node) {
4-
context.report(
5-
node,
6-
`Do Expressions are not supported in ${badBrowser}`
7-
)
8-
}
1+
module.exports = (context, badBrowser) => ({
2+
DoExpression(node) {
3+
context.report(node, `Do Expressions are not supported in ${badBrowser}`)
94
}
10-
}
11-
12-
module.exports.schema = []
5+
})
+4-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
module.exports = function(context, badBrowser) {
2-
return {
3-
BinaryExpression(node) {
4-
if (node.operator === '**') {
5-
context.report(
6-
node,
7-
`Exponentiation Operator is not supported in ${badBrowser}`
8-
)
9-
}
10-
}
1+
module.exports = (context, badBrowser) => ({
2+
'AssignmentExpression[operator="**="], BinaryExpression[operator="**"]'(node) {
3+
context.report(node, `Exponentiation Operator is not supported in ${badBrowser}`)
114
}
12-
}
13-
14-
module.exports.schema = []
5+
})

lib/rules/no-numeric-separators.js

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
const hasSeparator = RegExp.prototype.test.bind(/^\d+(?:_\d+)+\d*$/)
2-
module.exports = function(context, badBrowser) {
3-
return {
4-
Literal(node) {
5-
if (typeof node.value === 'number' && hasSeparator(node.raw)) {
6-
context.report(
7-
node,
8-
`Numeric Separators are not supported in ${badBrowser}`
9-
)
10-
}
11-
}
1+
module.exports = (context, badBrowser) => ({
2+
'Literal[raw=/_/][value>=0], Literal[raw=/_/][value<=0]'(node) {
3+
context.report(node, `Numeric Separators are not supported in ${badBrowser}`)
124
}
13-
}
14-
15-
module.exports.schema = []
5+
})

lib/rules/no-object-rest-spread.js

+15-33
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,16 @@
1-
module.exports = function(context, badBrowser) {
2-
return {
3-
SpreadElement(node) {
4-
if (!node.parent || node.parent.type !== 'ObjectExpression') return
5-
context.report(
6-
node,
7-
`Object Rest/Spread is not supported in ${badBrowser}`
8-
)
9-
},
10-
RestElement(node) {
11-
if (!node.parent || node.parent.type !== 'ObjectPattern') return
12-
context.report(
13-
node,
14-
`Object Rest/Spread is not supported in ${badBrowser}`
15-
)
16-
},
1+
module.exports = (context, badBrowser) => ({
2+
'ObjectExpression > SpreadElement'(node) {
3+
context.report(node, `Object Rest/Spread is not supported in ${badBrowser}`)
4+
},
5+
'ObjectPattern > RestElement'(node) {
6+
context.report(node, `Object Rest/Spread is not supported in ${badBrowser}`)
7+
},
178

18-
// Catch older versions of eslint and babel-eslint
19-
ExperimentalRestProperty(node) {
20-
context.report(
21-
node,
22-
`Object Rest/Spread is not supported in ${badBrowser}`
23-
)
24-
},
25-
ExperimentalSpreadProperty(node) {
26-
context.report(
27-
node,
28-
`Object Rest/Spread is not supported in ${badBrowser}`
29-
)
30-
},
31-
}
32-
}
33-
34-
module.exports.schema = []
9+
// Catch older versions of eslint and babel-eslint
10+
ExperimentalRestProperty(node) {
11+
context.report(node, `Object Rest/Spread is not supported in ${badBrowser}`)
12+
},
13+
ExperimentalSpreadProperty(node) {
14+
context.report(node, `Object Rest/Spread is not supported in ${badBrowser}`)
15+
},
16+
})

lib/rules/no-optional-catch.js

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
module.exports = function(context, badBrowser) {
2-
return {
3-
CatchClause(node) {
4-
if (!node.param) {
5-
context.report(
6-
node,
7-
`Optional Catch Parameters are not supported in ${badBrowser}`
8-
)
9-
}
10-
}
1+
module.exports = (context, badBrowser) => ({
2+
'CatchClause:not([param])'(node) {
3+
context.report(node, `Optional Catch Parameters are not supported in ${badBrowser}`)
114
}
12-
}
13-
14-
module.exports.schema = []
5+
})

lib/rules/no-optional-chaining.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
module.exports = function(context, badBrowser) {
2-
return {
3-
OptionalMemberExpression(node) {
4-
context.report(
5-
node,
6-
`Optional Chaining is not supported in ${badBrowser}`
7-
)
8-
}
1+
module.exports = (context, badBrowser) => ({
2+
OptionalMemberExpression(node) {
3+
context.report(node, `Optional Chaining is not supported in ${badBrowser}`)
94
}
10-
}
11-
12-
module.exports.schema = []
5+
})

lib/rules/no-pipeline-operator.js

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
module.exports = function(context, badBrowser) {
2-
return {
3-
BinaryExpression(node) {
4-
if (node.operator === '|>') {
5-
context.report(
6-
node,
7-
`The Pipeline Operator is not supported in ${badBrowser}`
8-
)
9-
}
10-
}
1+
module.exports = (context, badBrowser) => ({
2+
'BinaryExpression[operator="|>"]'(node) {
3+
context.report(node, `The Pipeline Operator is not supported in ${badBrowser}`)
114
}
12-
}
13-
14-
module.exports.schema = []
5+
})

lib/rules/no-private-class-fields.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
module.exports = function(context, badBrowser) {
2-
return {
3-
ClassPrivateProperty(node) {
4-
context.report(
5-
node,
6-
`Private Class Fields are not supported in ${badBrowser}`
7-
)
8-
}
1+
module.exports = (context, badBrowser) => ({
2+
ClassPrivateProperty(node) {
3+
context.report(node, `Private Class Fields are not supported in ${badBrowser}`)
94
}
10-
}
11-
12-
module.exports.schema = []
5+
})

lib/rules/no-public-class-fields.js

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
module.exports = function(context, badBrowser) {
2-
return {
3-
ClassProperty(node) {
4-
// Ignore type annotations that don't assign
5-
if (node.typeAnnotation && !node.value) return
6-
context.report(
7-
node,
8-
`Class Fields are not supported in ${badBrowser}`
9-
)
10-
}
1+
module.exports = (context, badBrowser) => ({
2+
// Ignore type annotations that don't assign
3+
'ClassProperty:not([typeAnnotation]:not([value]))'(node) {
4+
context.report(node, `Class Fields are not supported in ${badBrowser}`)
115
}
12-
}
13-
14-
module.exports.schema = []
6+
})

lib/rules/no-regexp-s-flag.js

+10-30
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,12 @@
1-
const getRegExpFlags = node => {
2-
if (node.regex) {
3-
return node.regex.flags;
4-
}
5-
if (typeof node.value === "string" &&
6-
(node.parent.type === "NewExpression" || node.parent.type === "CallExpression") &&
7-
node.parent.callee.type === "Identifier" &&
8-
node.parent.callee.name === "RegExp" &&
9-
node.parent.arguments[1] === node
10-
) {
11-
return node.value;
12-
}
13-
return null;
14-
}
15-
16-
17-
module.exports = function(context, badBrowser) {
18-
return {
19-
Literal(node) {
20-
const flags = getRegExpFlags(node)
21-
if (!flags) return
22-
if (flags.indexOf('s') !== -1) {
23-
context.report(
24-
node,
25-
`RegExp "s" flag is not supported in ${badBrowser}`
26-
)
27-
}
1+
module.exports = (context, badBrowser) => ({
2+
'Literal[regex]'(node) {
3+
if (node.regex.flags.includes('s')) {
4+
context.report(node, `RegExp "s" flag is not supported in ${badBrowser}`)
5+
}
6+
},
7+
'CallExpression[callee.name="RegExp"], NewExpression[callee.name="RegExp"]'(node) {
8+
if (node.arguments[1] && node.arguments[1].value.includes('s')) {
9+
context.report(node, `RegExp "s" flag is not supported in ${badBrowser}`)
2810
}
2911
}
30-
}
31-
32-
module.exports.schema = []
12+
})

test/no-exponentiation-operator.js

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ ruleTester.run('no-exponentiation-operator', rule, {
77
valid: [
88
{code: 'Math.pow(2, 2)'},
99
{code: '2 * 2 * 2'},
10+
{code: 'a = Math.pow(a * 2)'},
11+
{code: 'a = a * 2 * 2'},
1012
],
1113
invalid: [
1214
{
@@ -17,6 +19,15 @@ ruleTester.run('no-exponentiation-operator', rule, {
1719
'Exponentiation Operator is not supported in undefined'
1820
}
1921
]
22+
},
23+
{
24+
code: 'a **= 2',
25+
errors: [
26+
{
27+
message:
28+
'Exponentiation Operator is not supported in undefined'
29+
}
30+
]
2031
}
2132
]
2233
})

test/no-numeric-separators.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ ruleTester.run('no-numeric-separators', rule, {
88
{code: '100000000'},
99
{code: '1.00000000'},
1010
{code: '1e8'},
11+
{code: '"1_000_000"'},
12+
{code: '0'},
1113
],
1214
invalid: [
1315
{

test/no-regexp-s-flag.js

+11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ ruleTester.run('no-regexp-s-flag', rule, {
77
valid: [
88
{code: '/foo.bar/'},
99
{code: '/foo.bar/g'},
10+
{code: 'new RegExp("foo.bar")'},
11+
{code: 'new RegExp("foo.bar", "u")'},
1012
{code: 'new RegExp("foo.bar", "g")'},
13+
{code: 'RegExp("foo.bar", "g")'},
1114
],
1215
invalid: [
1316
{
@@ -26,5 +29,13 @@ ruleTester.run('no-regexp-s-flag', rule, {
2629
}
2730
]
2831
},
32+
{
33+
code: 'RegExp("foo.bar", "s")',
34+
errors: [
35+
{
36+
message: 'RegExp "s" flag is not supported in undefined'
37+
}
38+
]
39+
},
2940
]
3041
})

0 commit comments

Comments
 (0)