From 0f42f021049ab2a5145c0a9568a9ce4dc8233f04 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 29 Nov 2021 22:11:10 -0800 Subject: [PATCH] feat(eslint-plugin): [no-shadow] support TS4.5 inline import specifiers --- package.json | 4 +- packages/eslint-plugin/src/rules/no-shadow.ts | 4 +- .../tests/rules/naming-convention.test.ts | 4 +- .../tests/rules/no-shadow.test.ts | 85 ++++++++++++++++++- yarn.lock | 10 +-- 5 files changed, 96 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index fe8d5e25da31..7c532099e9bb 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,7 @@ "@types/marked": "^3.0.2", "@types/ncp": "^2.0.5", "@types/node": "^16.11.4", - "@types/prettier": "^2.3.2", + "@types/prettier": "^2.4.2", "@types/rimraf": "^3.0.2", "@types/semver": "^7.3.9", "@types/tmp": "^0.2.2", @@ -102,7 +102,7 @@ "markdownlint-cli": "^0.29.0", "ncp": "^2.0.0", "node-fetch": "^3.0.0", - "prettier": "2.4.1", + "prettier": "^2.5.0", "pretty-format": "^27.3.1", "rimraf": "^3.0.2", "tmp": "^0.2.1", diff --git a/packages/eslint-plugin/src/rules/no-shadow.ts b/packages/eslint-plugin/src/rules/no-shadow.ts index dfba42cf301e..68214ddd3383 100644 --- a/packages/eslint-plugin/src/rules/no-shadow.ts +++ b/packages/eslint-plugin/src/rules/no-shadow.ts @@ -98,7 +98,9 @@ export default util.createRule({ ): definition is ImportBindingDefinition { return ( definition?.type === DefinitionType.ImportBinding && - definition.parent.importKind === 'type' + (definition.parent.importKind === 'type' || + (definition.node.type === AST_NODE_TYPES.ImportSpecifier && + definition.node.importKind === 'type')) ); } diff --git a/packages/eslint-plugin/tests/rules/naming-convention.test.ts b/packages/eslint-plugin/tests/rules/naming-convention.test.ts index d35afe5ed30c..b8ad03a53a61 100644 --- a/packages/eslint-plugin/tests/rules/naming-convention.test.ts +++ b/packages/eslint-plugin/tests/rules/naming-convention.test.ts @@ -1222,7 +1222,7 @@ ruleTester.run('naming-convention', rule, { { code: ` class Ignored { - private static abstract readonly some_name = 1; + private static abstract readonly some_name; IgnoredDueToModifiers = 1; } `, @@ -2060,7 +2060,7 @@ ruleTester.run('naming-convention', rule, { { code: ` class Ignored { - private static abstract readonly some_name = 1; + private static abstract readonly some_name; IgnoredDueToModifiers = 1; } `, diff --git a/packages/eslint-plugin/tests/rules/no-shadow.test.ts b/packages/eslint-plugin/tests/rules/no-shadow.test.ts index bedb825df704..929ddf9d389e 100644 --- a/packages/eslint-plugin/tests/rules/no-shadow.test.ts +++ b/packages/eslint-plugin/tests/rules/no-shadow.test.ts @@ -193,6 +193,15 @@ function doThing(foo: number, bar: number) {} `, options: [{ ignoreTypeValueShadow: true }], }, + { + code: ` +import { type foo } from './foo'; + +// 'foo' is already declared in the upper scope +function doThing(foo: number) {} + `, + options: [{ ignoreTypeValueShadow: true }], + }, ], invalid: [ { @@ -1422,7 +1431,23 @@ function foo(cb) { { code: ` import type { foo } from './foo'; -function doThing(foo: number, bar: number) {} +function doThing(foo: number) {} + `, + options: [{ ignoreTypeValueShadow: false }], + errors: [ + { + messageId: 'noShadow', + data: { name: 'foo' }, + type: AST_NODE_TYPES.Identifier, + line: 3, + column: 18, + }, + ], + }, + { + code: ` +import { type foo } from './foo'; +function doThing(foo: number) {} `, options: [{ ignoreTypeValueShadow: false }], errors: [ @@ -1513,6 +1538,64 @@ declare module 'bar' { code: ` import type { Foo } from 'bar'; +declare module 'bar' { + interface Foo { + x: string; + } +} + `, + errors: [ + { + messageId: 'noShadow', + data: { name: 'Foo' }, + type: AST_NODE_TYPES.Identifier, + line: 5, + column: 13, + }, + ], + }, + { + code: ` +import { type Foo } from 'bar'; + +declare module 'baz' { + export interface Foo { + x: string; + } +} + `, + errors: [ + { + messageId: 'noShadow', + data: { name: 'Foo' }, + type: AST_NODE_TYPES.Identifier, + line: 5, + column: 20, + }, + ], + }, + { + code: ` +import { type Foo } from 'bar'; + +declare module 'bar' { + export type Foo = string; +} + `, + errors: [ + { + messageId: 'noShadow', + data: { name: 'Foo' }, + type: AST_NODE_TYPES.Identifier, + line: 5, + column: 15, + }, + ], + }, + { + code: ` +import { type Foo } from 'bar'; + declare module 'bar' { interface Foo { x: string; diff --git a/yarn.lock b/yarn.lock index cf83b08f8e63..889f67f81324 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3770,7 +3770,7 @@ resolved "https://registry.yarnpkg.com/@types/parse5/-/parse5-5.0.3.tgz#e7b5aebbac150f8b5fdd4a46e7f0bd8e65e19109" integrity sha512-kUNnecmtkunAoQ3CnjmMkzNU/gtxG8guhi+Fk2U/kOpIKjIMKnXGp4IJCgQJrXSgMsWYimYG4TGjz/UzbGEBTw== -"@types/prettier@*", "@types/prettier@^2.1.5", "@types/prettier@^2.3.2": +"@types/prettier@*", "@types/prettier@^2.1.5", "@types/prettier@^2.4.2": version "2.4.2" resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.4.2.tgz#4c62fae93eb479660c3bd93f9d24d561597a8281" integrity sha512-ekoj4qOQYp7CvjX8ZDBgN86w3MqQhLE1hczEJbEIjgFEumDy+na/4AJAbLXfgEWFNB2pKadM5rPFtuSGMWK7xA== @@ -11706,10 +11706,10 @@ prepend-http@^2.0.0: resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-2.0.0.tgz#e92434bfa5ea8c19f41cdfd401d741a3c819d897" integrity sha1-6SQ0v6XqjBn0HN/UAddBo8gZ2Jc= -prettier@*, prettier@2.4.1: - version "2.4.1" - resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.4.1.tgz#671e11c89c14a4cfc876ce564106c4a6726c9f5c" - integrity sha512-9fbDAXSBcc6Bs1mZrDYb3XKzDLm4EXXL9sC1LqKP5rZkT6KRr/rf9amVUcODVXgguK/isJz0d0hP72WeaKWsvA== +prettier@*, prettier@^2.5.0: + version "2.5.0" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.5.0.tgz#a6370e2d4594e093270419d9cc47f7670488f893" + integrity sha512-FM/zAKgWTxj40rH03VxzIPdXmj39SwSjwG0heUcNFwI+EMZJnY93yAiKXM3dObIKAM5TA88werc8T/EwhB45eg== pretty-error@^4.0.0: version "4.0.0"