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 2 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
25 changes: 24 additions & 1 deletion packages/eslint-plugin/src/rules/no-use-before-define.ts
Expand Up @@ -92,6 +92,29 @@ function isOuterVariable(
);
}

/**
* Recursively checks whether or not a given reference has a type query declaration among it's parents
*/
function referenceContainsTypeQuery(node: TSESTree.Node): boolean {
if (node.type === AST_NODE_TYPES.TSTypeQuery) {
return true;
} else if (!node.parent) {
return false;
}

return referenceContainsTypeQuery(node.parent);
Copy link
Member

Choose a reason for hiding this comment

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

Almost there!

We can constrain this a little more so we don't recursively traverse up the whole tree unnecessarily.
We know that the node must always be either be a TSQualifiedName, Identifier or TSTypeQuery.

switch (node.type) {
  case AST_NODE_TYPES.TSTypeQuery:
    return true;

  case AST_NODE_TYPES.TSQualifiedName:
  case AST_NODE_TYPES.Identifier:
    return referenceContainsTypeQuery(node.parent);

  default:
    // if we find a different node, there's no chance that we're in a TSTypeQuery
    return false;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see! I wasn't sure of what you meant in your first comment.
I've changed it.

}

/**
* Checks whether or not a given reference is a type reference.
*/
function isTypeReference(reference: TSESLint.Scope.Reference): boolean {
return (
reference.isTypeReference ||
referenceContainsTypeQuery(reference.identifier)
);
}

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

const Foo = 2;
`,
options: [{ ignoreTypeReferences: true }],
},
{
code: `
interface Bar {
type: typeof Foo.FOO;
}

class Foo {
public static readonly FOO = '';
}
`,
options: [{ ignoreTypeReferences: true }],
},
{
code: `
interface Bar {
type: typeof Foo.Bar.Baz;
}

const Foo = {
Bar: {
Baz: 1,
},
};
`,
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 Expand Up @@ -864,6 +900,65 @@ for (var a of a) {
],
},

// "ignoreTypeReferences" option
{
code: `
interface Bar {
type: typeof Foo;
}

const Foo = 2;
`,
options: [{ ignoreTypeReferences: false }],
errors: [
{
messageId: 'noUseBeforeDefine',
data: { name: 'Foo' },
type: AST_NODE_TYPES.Identifier,
},
],
},
{
code: `
interface Bar {
type: typeof Foo.FOO;
}

class Foo {
public static readonly FOO = '';
}
`,
options: [{ ignoreTypeReferences: false }],
errors: [
{
messageId: 'noUseBeforeDefine',
data: { name: 'Foo' },
type: AST_NODE_TYPES.Identifier,
},
],
},
{
code: `
interface Bar {
type: typeof Foo.Bar.Baz;
}

const Foo = {
Bar: {
Baz: 1,
},
};
`,
options: [{ ignoreTypeReferences: false }],
errors: [
{
messageId: 'noUseBeforeDefine',
data: { name: 'Foo' },
type: AST_NODE_TYPES.Identifier,
},
],
},

// "variables" option
{
code: `
Expand Down