Skip to content

Commit

Permalink
feat: limit namespace import identifier in id-length rule (#18849)
Browse files Browse the repository at this point in the history
* feat: limit namespace import identifier in id-length rule

* test: add tests for valid case in 'id-length' rule

* refactor: Sort order, Change variable name to semantic name

* Update lib/rules/id-length.js

Co-authored-by: Tanuj Kanti <[email protected]>

* Update tests/lib/rules/id-length.js

Co-authored-by: Tanuj Kanti <[email protected]>

* Update tests/lib/rules/id-length.js

Co-authored-by: Tanuj Kanti <[email protected]>

* test: add test for shows that ImportSpecifiers are allowed even with default 2 and with max option

---------

Co-authored-by: Tanuj Kanti <[email protected]>
  • Loading branch information
Chaedie and Tanujkanti4441 authored Sep 6, 2024
1 parent 45c18e1 commit bcf0df5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rules/id-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ module.exports = {
return properties && !parent.computed && parent.key.name === node.name;
},
ImportDefaultSpecifier: true,
ImportNamespaceSpecifier: true,
RestElement: true,
FunctionExpression: true,
ArrowFunctionExpression: true,
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/id-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ ruleTester.run("id-length", rule, {
{ code: "({ a: obj.x.y.z } = {});", options: [{ properties: "never" }], languageOptions: { ecmaVersion: 6 } },
{ code: "import something from 'y';", languageOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "export var num = 0;", languageOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "import * as something from 'y';", languageOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "import { x } from 'y';", languageOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "import { longName } from 'y';", options: [{ max: 5 }], languageOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "({ prop: obj.x.y.something } = {});", languageOptions: { ecmaVersion: 6 } },
{ code: "({ prop: obj.longName } = {});", languageOptions: { ecmaVersion: 6 } },
{ code: "var obj = { a: 1, bc: 2 };", options: [{ properties: "never" }] },
Expand Down Expand Up @@ -246,6 +249,20 @@ ruleTester.run("id-length", rule, {
{ code: "var [i] = arr;", languageOptions: { ecmaVersion: 6 }, errors: [tooShortError] },
{ code: "var [,i,a] = arr;", languageOptions: { ecmaVersion: 6 }, errors: [tooShortError, tooShortError] },
{ code: "function foo([a]) {}", languageOptions: { ecmaVersion: 6 }, errors: [tooShortError] },
{ code: "import x from 'module';", languageOptions: { ecmaVersion: 6 }, errors: [tooShortError] },
{ code: "import * as x from 'module';", languageOptions: { ecmaVersion: 6 }, errors: [tooShortError] },
{
code: "import longName from 'module';",
options: [{ max: 5 }],
languageOptions: { ecmaVersion: 6 },
errors: [tooLongError]
},
{
code: "import * as longName from 'module';",
options: [{ max: 5 }],
languageOptions: { ecmaVersion: 6 },
errors: [tooLongError]
},
{
code: "var _$xt_$ = Foo(42)",
options: [{ min: 2, max: 4 }],
Expand Down

0 comments on commit bcf0df5

Please sign in to comment.