Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: no-underscore-dangle in private and public class fields from es2022 #15811

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion lib/rules/no-underscore-dangle.js
Expand Up @@ -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
//--------------------------------------------------------------------------
Expand All @@ -270,7 +292,7 @@ module.exports = {
VariableDeclarator: checkForDanglingUnderscoreInVariableExpression,
MemberExpression: checkForDanglingUnderscoreInMemberExpression,
MethodDefinition: checkForDanglingUnderscoreInMethod,
PropertyDefinition: checkForDanglingUnderscoreInMethod,
PropertyDefinition: checkForDanglingUnderscoreInClassProperty,
Property: checkForDanglingUnderscoreInMethod,
FunctionExpression: checkForDanglingUnderscoreInFunction,
ArrowFunctionExpression: checkForDanglingUnderscoreInFunction
Expand Down
28 changes: 25 additions & 3 deletions tests/lib/rules/no-underscore-dangle.js
Expand Up @@ -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" }] },
Expand Down Expand Up @@ -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_" } }]
}
]
});