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(eslint-plugin): [triple-slash-reference] fix crash with external module reference #2788

Merged
merged 3 commits into from Nov 25, 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
30 changes: 24 additions & 6 deletions packages/eslint-plugin/src/rules/triple-slash-reference.ts
@@ -1,4 +1,5 @@
import {
AST_NODE_TYPES,
AST_TOKEN_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
Expand Down Expand Up @@ -60,9 +61,11 @@ export default util.createRule<Options, MessageIds>({
importName: string;
}[] = [];

function hasMatchingReference(source: TSESTree.Literal): void {
function hasMatchingReference(
value: string | number | bigint | boolean | RegExp | null,
): void {
references.forEach(reference => {
if (reference.importName === source.value) {
if (reference.importName === value) {
context.report({
node: reference.comment,
messageId: 'tripleSlashReference',
Expand All @@ -76,14 +79,29 @@ export default util.createRule<Options, MessageIds>({
return {
ImportDeclaration(node): void {
if (programNode) {
hasMatchingReference(node.source);
hasMatchingReference(node.source.value);
}
},
TSImportEqualsDeclaration(node): void {
if (programNode) {
const source = (node.moduleReference as TSESTree.TSExternalModuleReference)
.expression as TSESTree.Literal;
hasMatchingReference(source);
const source = node.moduleReference;
let value: string | number | bigint | boolean | RegExp | null;

switch (source.type) {
case AST_NODE_TYPES.Identifier:
value = source.name;
break;

case AST_NODE_TYPES.TSQualifiedName:
value = (source.left as TSESTree.Identifier).name;
break;
Copy link
Member

Choose a reason for hiding this comment

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

I think we should just completely ignore these cases by returning early instead.

There's no import source to compare against here, so doing so will likely lead to some weirdness.


case AST_NODE_TYPES.TSExternalModuleReference:
value = (source.expression as TSESTree.Literal).value;
break;
}

hasMatchingReference(value);
}
},
Program(node): void {
Expand Down
22 changes: 22 additions & 0 deletions packages/eslint-plugin/tests/rules/triple-slash-reference.test.ts
Expand Up @@ -54,6 +54,28 @@ ruleTester.run('triple-slash-reference', rule, {
`,
options: [{ path: 'always', types: 'always', lib: 'always' }],
},
{
code: `
/// <reference path="foo" />
/// <reference types="bar" />
/// <reference lib="baz" />
import foo = foo;
import bar = bar;
import baz = baz;
`,
options: [{ path: 'always', types: 'always', lib: 'always' }],
},
{
code: `
/// <reference path="foo" />
/// <reference types="bar" />
/// <reference lib="baz" />
import foo = foo.foo;
import bar = bar.bar.bar.bar;
import baz = baz.baz;
`,
options: [{ path: 'always', types: 'always', lib: 'always' }],
},
{
code: "import * as foo from 'foo';",
options: [{ path: 'never' }],
Expand Down