From 676bb772a7805abd3239fb464d8f66fa6d61ce8d Mon Sep 17 00:00:00 2001 From: Roberto Cestari Date: Tue, 26 Apr 2022 19:10:26 -0300 Subject: [PATCH] fix: add support for private and public class fields from es2022 --- lib/rules/no-underscore-dangle.js | 24 ++++++++++++++++++++- tests/lib/rules/no-underscore-dangle.js | 28 ++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/rules/no-underscore-dangle.js b/lib/rules/no-underscore-dangle.js index 0ab41feb03c..4da037b3b2e 100644 --- a/lib/rules/no-underscore-dangle.js +++ b/lib/rules/no-underscore-dangle.js @@ -261,6 +261,28 @@ module.exports = { } } + /** + * Check if property declaration has a dangling underscore + * @param {ASTNode} node node to evaluate + * @returns {void} + * @private + */ + function checkForDanglingUnderscoreInClassProperty(node) { + const identifier = node.key.name; + const isProperty = node.type === "PropertyDefinition"; + + if (typeof identifier !== "undefined" && hasDanglingUnderscore(identifier) && isProperty && !isAllowed(identifier)) { + context.report({ + node, + messageId: "unexpectedUnderscore", + data: { + identifier + } + }); + } + } + + //-------------------------------------------------------------------------- // Public API //-------------------------------------------------------------------------- @@ -270,7 +292,7 @@ module.exports = { VariableDeclarator: checkForDanglingUnderscoreInVariableExpression, MemberExpression: checkForDanglingUnderscoreInMemberExpression, MethodDefinition: checkForDanglingUnderscoreInMethod, - PropertyDefinition: checkForDanglingUnderscoreInMethod, + PropertyDefinition: checkForDanglingUnderscoreInClassProperty, Property: checkForDanglingUnderscoreInMethod, FunctionExpression: checkForDanglingUnderscoreInFunction, ArrowFunctionExpression: checkForDanglingUnderscoreInFunction diff --git a/tests/lib/rules/no-underscore-dangle.js b/tests/lib/rules/no-underscore-dangle.js index c83a5b3fe28..2571f062370 100644 --- a/tests/lib/rules/no-underscore-dangle.js +++ b/tests/lib/rules/no-underscore-dangle.js @@ -69,9 +69,7 @@ ruleTester.run("no-underscore-dangle", rule, { { code: "function foo([_bar] = []) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 } }, { code: "function foo( { _bar }) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 } }, { code: "function foo( { _bar = 0 } = {}) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 } }, - { code: "function foo(...[_bar]) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 2016 } }, - { code: "class foo { _field; }", parserOptions: { ecmaVersion: 2022 } }, - { code: "class foo { #_field; }", parserOptions: { ecmaVersion: 2022 } } + { code: "function foo(...[_bar]) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 2016 } } ], invalid: [ { code: "var _foo = 1", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" }, type: "VariableDeclarator" }] }, @@ -109,6 +107,30 @@ ruleTester.run("no-underscore-dangle", rule, { options: [{ enforceInMethodNames: true }], parserOptions: { ecmaVersion: 2022 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "#bar_" } }] + }, + { + code: "class foo { _field; }", + options: [{ enforceInMethodNames: true }], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_field" } }] + }, + { + code: "class foo { #_field; }", + options: [{ enforceInMethodNames: true }], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_field" } }] + }, + { + code: "class foo { field_; }", + options: [{ enforceInMethodNames: true }], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "field_" } }] + }, + { + code: "class foo { #field_; }", + options: [{ enforceInMethodNames: true }], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "field_" } }] } ] });