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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [no-use-before-define] correctly handle typeof type references #2623

Merged
merged 3 commits into from Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 11 additions & 1 deletion packages/eslint-plugin/src/rules/no-use-before-define.ts
Expand Up @@ -92,6 +92,16 @@ function isOuterVariable(
);
}

/**
* Checks whether or not a given reference is a type reference.
*/
function isTypeReference(reference: TSESLint.Scope.Reference): boolean {
return (
reference.isTypeReference ||
reference.identifier.parent?.parent?.type === AST_NODE_TYPES.TSTypeQuery
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost! but this will only match one case.

We need to match all of these cases, and more:

  • typeof a
  • typeof a.b
  • typeof a.b.c

Instead an easier way to do this would be with a loop.
Based on the defined types for TSTypeQuery, we know that the exprName will either be an Identifier or a TSQualifiedName.

export interface TSTypeQuery extends BaseNode {
type: AST_NODE_TYPES.TSTypeQuery;
exprName: EntityName;
}

Based on the defined types for TSQualifiedName, we know the left will always be an Identifier or a TSQualifiedName.

export interface TSQualifiedName extends BaseNode {
type: AST_NODE_TYPES.TSQualifiedName;
left: EntityName;
right: Identifier;
}

This means we can simply loop and look at the parents
The reason I gave you the above is it'll help you constrain the loop so it won't iterate forever

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

);
}

/**
* Checks whether or not a given location is inside of the range of a given node.
*/
Expand Down Expand Up @@ -219,7 +229,7 @@ export default util.createRule<Options, MessageIds>({
variable: TSESLint.Scope.Variable,
reference: TSESLint.Scope.Reference,
): boolean {
if (reference.isTypeReference && options.ignoreTypeReferences) {
if (isTypeReference(reference) && options.ignoreTypeReferences) {
return false;
}
if (isFunction(variable)) {
Expand Down
22 changes: 22 additions & 0 deletions packages/eslint-plugin/tests/rules/no-use-before-define.test.ts
Expand Up @@ -219,7 +219,29 @@ type Foo = string | number;
`,
options: [{ typedefs: false }],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/2572
{
code: `
interface Bar {
type: typeof Foo.FOO;
}

class Foo {
public static readonly FOO = '';
}
`,
options: [{ ignoreTypeReferences: true }],
},
{
code: `
const foo = 2;

interface Bar {
type: typeof foo;
}
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
`,
options: [{ ignoreTypeReferences: true }],
},
// https://github.com/bradzacher/eslint-plugin-typescript/issues/141
{
code: `
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
Expand Down