Skip to content

Commit

Permalink
fix: refine rules using selector syntax. Fix edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Sep 20, 2019
1 parent 576d29d commit f7b44d2
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 199 deletions.
17 changes: 4 additions & 13 deletions lib/rules/no-async-generator.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
module.exports = function(context, badBrowser) {
return {
FunctionDeclaration(node) {
if (node.async && node.generator) {
context.report(
node,
`Async Generators are not supported in ${badBrowser}`
)
}
}
module.exports = (context, badBrowser) => ({
':function[async=true][generator=true]'(node) {
context.report(node, `Async Generators are not supported in ${badBrowser}`)
}
}

module.exports.schema = []
})
17 changes: 4 additions & 13 deletions lib/rules/no-async-iteration.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
module.exports = function(context, badBrowser) {
return {
ForOfStatement(node) {
if (node.await) {
context.report(
node,
`Async Iteration is not supported in ${badBrowser}`
)
}
}
module.exports = (context, badBrowser) => ({
'ForOfStatement[await=true]'(node) {
context.report(node, `Async Iteration is not supported in ${badBrowser}`)
}
}

module.exports.schema = []
})
15 changes: 4 additions & 11 deletions lib/rules/no-bind-operator.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
module.exports = function(context, badBrowser) {
return {
BindExpression(node) {
context.report(
node,
`The Bind Operator is not supported in ${badBrowser}`
)
}
module.exports = (context, badBrowser) => ({
BindExpression(node) {
context.report(node, `The Bind Operator is not supported in ${badBrowser}`)
}
}

module.exports.schema = []
})
15 changes: 4 additions & 11 deletions lib/rules/no-do-expression.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
module.exports = function(context, badBrowser) {
return {
DoExpression(node) {
context.report(
node,
`Do Expressions are not supported in ${badBrowser}`
)
}
module.exports = (context, badBrowser) => ({
DoExpression(node) {
context.report(node, `Do Expressions are not supported in ${badBrowser}`)
}
}

module.exports.schema = []
})
17 changes: 4 additions & 13 deletions lib/rules/no-exponentiation-operator.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
module.exports = function(context, badBrowser) {
return {
BinaryExpression(node) {
if (node.operator === '**') {
context.report(
node,
`Exponentiation Operator is not supported in ${badBrowser}`
)
}
}
module.exports = (context, badBrowser) => ({
'AssignmentExpression[operator="**="], BinaryExpression[operator="**"]'(node) {
context.report(node, `Exponentiation Operator is not supported in ${badBrowser}`)
}
}

module.exports.schema = []
})
18 changes: 4 additions & 14 deletions lib/rules/no-numeric-separators.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
const hasSeparator = RegExp.prototype.test.bind(/^\d+(?:_\d+)+\d*$/)
module.exports = function(context, badBrowser) {
return {
Literal(node) {
if (typeof node.value === 'number' && hasSeparator(node.raw)) {
context.report(
node,
`Numeric Separators are not supported in ${badBrowser}`
)
}
}
module.exports = (context, badBrowser) => ({
'Literal[raw=/_/][value>=0], Literal[raw=/_/][value<=0]'(node) {
context.report(node, `Numeric Separators are not supported in ${badBrowser}`)
}
}

module.exports.schema = []
})
48 changes: 15 additions & 33 deletions lib/rules/no-object-rest-spread.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
module.exports = function(context, badBrowser) {
return {
SpreadElement(node) {
if (!node.parent || node.parent.type !== 'ObjectExpression') return
context.report(
node,
`Object Rest/Spread is not supported in ${badBrowser}`
)
},
RestElement(node) {
if (!node.parent || node.parent.type !== 'ObjectPattern') return
context.report(
node,
`Object Rest/Spread is not supported in ${badBrowser}`
)
},
module.exports = (context, badBrowser) => ({
'ObjectExpression > SpreadElement'(node) {
context.report(node, `Object Rest/Spread is not supported in ${badBrowser}`)
},
'ObjectPattern > RestElement'(node) {
context.report(node, `Object Rest/Spread is not supported in ${badBrowser}`)
},

// Catch older versions of eslint and babel-eslint
ExperimentalRestProperty(node) {
context.report(
node,
`Object Rest/Spread is not supported in ${badBrowser}`
)
},
ExperimentalSpreadProperty(node) {
context.report(
node,
`Object Rest/Spread is not supported in ${badBrowser}`
)
},
}
}

module.exports.schema = []
// Catch older versions of eslint and babel-eslint
ExperimentalRestProperty(node) {
context.report(node, `Object Rest/Spread is not supported in ${badBrowser}`)
},
ExperimentalSpreadProperty(node) {
context.report(node, `Object Rest/Spread is not supported in ${badBrowser}`)
},
})
17 changes: 4 additions & 13 deletions lib/rules/no-optional-catch.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
module.exports = function(context, badBrowser) {
return {
CatchClause(node) {
if (!node.param) {
context.report(
node,
`Optional Catch Parameters are not supported in ${badBrowser}`
)
}
}
module.exports = (context, badBrowser) => ({
'CatchClause:not([param])'(node) {
context.report(node, `Optional Catch Parameters are not supported in ${badBrowser}`)
}
}

module.exports.schema = []
})
15 changes: 4 additions & 11 deletions lib/rules/no-optional-chaining.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
module.exports = function(context, badBrowser) {
return {
OptionalMemberExpression(node) {
context.report(
node,
`Optional Chaining is not supported in ${badBrowser}`
)
}
module.exports = (context, badBrowser) => ({
OptionalMemberExpression(node) {
context.report(node, `Optional Chaining is not supported in ${badBrowser}`)
}
}

module.exports.schema = []
})
17 changes: 4 additions & 13 deletions lib/rules/no-pipeline-operator.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
module.exports = function(context, badBrowser) {
return {
BinaryExpression(node) {
if (node.operator === '|>') {
context.report(
node,
`The Pipeline Operator is not supported in ${badBrowser}`
)
}
}
module.exports = (context, badBrowser) => ({
'BinaryExpression[operator="|>"]'(node) {
context.report(node, `The Pipeline Operator is not supported in ${badBrowser}`)
}
}

module.exports.schema = []
})
15 changes: 4 additions & 11 deletions lib/rules/no-private-class-fields.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
module.exports = function(context, badBrowser) {
return {
ClassPrivateProperty(node) {
context.report(
node,
`Private Class Fields are not supported in ${badBrowser}`
)
}
module.exports = (context, badBrowser) => ({
ClassPrivateProperty(node) {
context.report(node, `Private Class Fields are not supported in ${badBrowser}`)
}
}

module.exports.schema = []
})
18 changes: 5 additions & 13 deletions lib/rules/no-public-class-fields.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
module.exports = function(context, badBrowser) {
return {
ClassProperty(node) {
// Ignore type annotations that don't assign
if (node.typeAnnotation && !node.value) return
context.report(
node,
`Class Fields are not supported in ${badBrowser}`
)
}
module.exports = (context, badBrowser) => ({
// Ignore type annotations that don't assign
'ClassProperty:not([typeAnnotation]:not([value]))'(node) {
context.report(node, `Class Fields are not supported in ${badBrowser}`)
}
}

module.exports.schema = []
})
40 changes: 10 additions & 30 deletions lib/rules/no-regexp-s-flag.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,12 @@
const getRegExpFlags = node => {
if (node.regex) {
return node.regex.flags;
}
if (typeof node.value === "string" &&
(node.parent.type === "NewExpression" || node.parent.type === "CallExpression") &&
node.parent.callee.type === "Identifier" &&
node.parent.callee.name === "RegExp" &&
node.parent.arguments[1] === node
) {
return node.value;
}
return null;
}


module.exports = function(context, badBrowser) {
return {
Literal(node) {
const flags = getRegExpFlags(node)
if (!flags) return
if (flags.indexOf('s') !== -1) {
context.report(
node,
`RegExp "s" flag is not supported in ${badBrowser}`
)
}
module.exports = (context, badBrowser) => ({
'Literal[regex]'(node) {
if (node.regex.flags.includes('s')) {
context.report(node, `RegExp "s" flag is not supported in ${badBrowser}`)
}
},
'CallExpression[callee.name="RegExp"], NewExpression[callee.name="RegExp"]'(node) {
if (node.arguments[1] && node.arguments[1].value.includes('s')) {
context.report(node, `RegExp "s" flag is not supported in ${badBrowser}`)
}
}
}

module.exports.schema = []
})
11 changes: 11 additions & 0 deletions test/no-exponentiation-operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ ruleTester.run('no-exponentiation-operator', rule, {
valid: [
{code: 'Math.pow(2, 2)'},
{code: '2 * 2 * 2'},
{code: 'a = Math.pow(a * 2)'},
{code: 'a = a * 2 * 2'},
],
invalid: [
{
Expand All @@ -17,6 +19,15 @@ ruleTester.run('no-exponentiation-operator', rule, {
'Exponentiation Operator is not supported in undefined'
}
]
},
{
code: 'a **= 2',
errors: [
{
message:
'Exponentiation Operator is not supported in undefined'
}
]
}
]
})
2 changes: 2 additions & 0 deletions test/no-numeric-separators.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ ruleTester.run('no-numeric-separators', rule, {
{code: '100000000'},
{code: '1.00000000'},
{code: '1e8'},
{code: '"1_000_000"'},
{code: '0'},
],
invalid: [
{
Expand Down
11 changes: 11 additions & 0 deletions test/no-regexp-s-flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ ruleTester.run('no-regexp-s-flag', rule, {
valid: [
{code: '/foo.bar/'},
{code: '/foo.bar/g'},
{code: 'new RegExp("foo.bar")'},
{code: 'new RegExp("foo.bar", "u")'},
{code: 'new RegExp("foo.bar", "g")'},
{code: 'RegExp("foo.bar", "g")'},
],
invalid: [
{
Expand All @@ -26,5 +29,13 @@ ruleTester.run('no-regexp-s-flag', rule, {
}
]
},
{
code: 'RegExp("foo.bar", "s")',
errors: [
{
message: 'RegExp "s" flag is not supported in undefined'
}
]
},
]
})

0 comments on commit f7b44d2

Please sign in to comment.