diff --git a/lib/rules/no-async-generator.js b/lib/rules/no-async-generator.js index 2de5ef8..ffdad0c 100644 --- a/lib/rules/no-async-generator.js +++ b/lib/rules/no-async-generator.js @@ -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 = [] +}) diff --git a/lib/rules/no-async-iteration.js b/lib/rules/no-async-iteration.js index 668f95c..12ed783 100644 --- a/lib/rules/no-async-iteration.js +++ b/lib/rules/no-async-iteration.js @@ -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 = [] +}) diff --git a/lib/rules/no-bind-operator.js b/lib/rules/no-bind-operator.js index 2b72a25..6f79623 100644 --- a/lib/rules/no-bind-operator.js +++ b/lib/rules/no-bind-operator.js @@ -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 = [] +}) diff --git a/lib/rules/no-do-expression.js b/lib/rules/no-do-expression.js index fb838cf..ee42842 100644 --- a/lib/rules/no-do-expression.js +++ b/lib/rules/no-do-expression.js @@ -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 = [] +}) diff --git a/lib/rules/no-exponentiation-operator.js b/lib/rules/no-exponentiation-operator.js index 4a70e33..95750e0 100644 --- a/lib/rules/no-exponentiation-operator.js +++ b/lib/rules/no-exponentiation-operator.js @@ -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 = [] +}) diff --git a/lib/rules/no-numeric-separators.js b/lib/rules/no-numeric-separators.js index 3c21903..f9876a3 100644 --- a/lib/rules/no-numeric-separators.js +++ b/lib/rules/no-numeric-separators.js @@ -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 = [] +}) diff --git a/lib/rules/no-object-rest-spread.js b/lib/rules/no-object-rest-spread.js index 2c7f548..f3244e2 100644 --- a/lib/rules/no-object-rest-spread.js +++ b/lib/rules/no-object-rest-spread.js @@ -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}`) + }, +}) diff --git a/lib/rules/no-optional-catch.js b/lib/rules/no-optional-catch.js index 3ab981a..d444c9a 100644 --- a/lib/rules/no-optional-catch.js +++ b/lib/rules/no-optional-catch.js @@ -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 = [] +}) diff --git a/lib/rules/no-optional-chaining.js b/lib/rules/no-optional-chaining.js index 9f6c9b9..6e682ab 100644 --- a/lib/rules/no-optional-chaining.js +++ b/lib/rules/no-optional-chaining.js @@ -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 = [] +}) diff --git a/lib/rules/no-pipeline-operator.js b/lib/rules/no-pipeline-operator.js index 8f64819..c2488a3 100644 --- a/lib/rules/no-pipeline-operator.js +++ b/lib/rules/no-pipeline-operator.js @@ -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 = [] +}) diff --git a/lib/rules/no-private-class-fields.js b/lib/rules/no-private-class-fields.js index 7a2712e..1059321 100644 --- a/lib/rules/no-private-class-fields.js +++ b/lib/rules/no-private-class-fields.js @@ -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 = [] +}) diff --git a/lib/rules/no-public-class-fields.js b/lib/rules/no-public-class-fields.js index dcaca79..977899a 100644 --- a/lib/rules/no-public-class-fields.js +++ b/lib/rules/no-public-class-fields.js @@ -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 = [] +}) diff --git a/lib/rules/no-regexp-s-flag.js b/lib/rules/no-regexp-s-flag.js index e7f09a9..482abfa 100644 --- a/lib/rules/no-regexp-s-flag.js +++ b/lib/rules/no-regexp-s-flag.js @@ -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 = [] +}) diff --git a/test/no-exponentiation-operator.js b/test/no-exponentiation-operator.js index 775e164..01f14e8 100644 --- a/test/no-exponentiation-operator.js +++ b/test/no-exponentiation-operator.js @@ -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: [ { @@ -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' + } + ] } ] }) diff --git a/test/no-numeric-separators.js b/test/no-numeric-separators.js index 3587932..1610195 100644 --- a/test/no-numeric-separators.js +++ b/test/no-numeric-separators.js @@ -8,6 +8,8 @@ ruleTester.run('no-numeric-separators', rule, { {code: '100000000'}, {code: '1.00000000'}, {code: '1e8'}, + {code: '"1_000_000"'}, + {code: '0'}, ], invalid: [ { diff --git a/test/no-regexp-s-flag.js b/test/no-regexp-s-flag.js index 734bc8f..a4bbc06 100644 --- a/test/no-regexp-s-flag.js +++ b/test/no-regexp-s-flag.js @@ -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: [ { @@ -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' + } + ] + }, ] })