Skip to content

Commit

Permalink
feat: support dynamic import properly
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Apr 29, 2022
1 parent aa7bb65 commit 660c04d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 38 deletions.
71 changes: 63 additions & 8 deletions src/rules/__tests__/utils.test.ts
Expand Up @@ -312,14 +312,6 @@ describe('reference checking', () => {
test('is not a jest function', () => {});
`,
},
{
code: dedent`
const { it } = await import('./test-utils');
it('is not a jest function', () => {});
`,
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
},
],
invalid: [
{
Expand Down Expand Up @@ -529,6 +521,69 @@ describe('reference checking', () => {
],
});

ruleTester.run('esm (dynamic)', rule, {
valid: [
{
code: dedent`
const { it } = await import('./test-utils');
it('is not a jest function', () => {});
`,
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
},
{
code: dedent`
const { it } = await import(\`./test-utils\`);
it('is not a jest function', () => {});
`,
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
},
],
invalid: [
{
code: dedent`
const { it } = await import("@jest/globals");
it('is a jest function', () => {});
`,
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
errors: [
{
messageId: 'details' as const,
data: {
callType: 'test',
numOfArgs: 2,
nodeName: 'it',
},
column: 1,
line: 3,
},
],
},
{
code: dedent`
const { it } = await import(\`@jest/globals\`);
it('is a jest function', () => {});
`,
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
errors: [
{
messageId: 'details' as const,
data: {
callType: 'test',
numOfArgs: 2,
nodeName: 'it',
},
column: 1,
line: 3,
},
],
},
],
});

ruleTester.run('cjs', rule, {
valid: [
{
Expand Down
49 changes: 19 additions & 30 deletions src/rules/utils.ts
Expand Up @@ -796,32 +796,29 @@ const describeImportDefAsImport = (
};

/**
* Attempts to get the import source from the given `node`.
* Attempts to find the node that represents the import source for the
* given expression node, if it looks like it's an import.
*
* If the `node` is a `CallExpression`, it's assumed that the first argument
* should be the import source (i.e. that it's a `require` call).
*
* If the source cannot be determined, `null` is returned.
* If no such node can be found (e.g. because the expression doesn't look
* like an import), then `null` is returned instead.
*/
const determineImportSource = (
node: TSESTree.ImportExpression | TSESTree.CallExpression,
): string | null => {
if (node.type === AST_NODE_TYPES.ImportExpression) {
if (isStringNode(node.source)) {
return getStringValue(node.source);
const findImportSourceNode = (
node: TSESTree.Expression,
): TSESTree.Node | null => {
if (node.type === AST_NODE_TYPES.AwaitExpression) {
// @ts-expect-error: https://github.com/typescript-eslint/typescript-eslint/issues/4877
if (node.argument.type === AST_NODE_TYPES.ImportExpression) {
return (node.argument as TSESTree.ImportExpression).source;
}

return null;
}

if (node.type === AST_NODE_TYPES.CallExpression) {
if (node.arguments.length === 0) {
return null;
}

if (isStringNode(node.arguments[0])) {
return getStringValue(node.arguments[0]);
}
if (
node.type === AST_NODE_TYPES.CallExpression &&
isIdentifier(node.callee, 'require')
) {
return node.arguments[0] ?? null;
}

return null;
Expand All @@ -840,17 +837,9 @@ const describeVariableDefAsImport = (
return null;
}

if (
def.node.init.type !== AST_NODE_TYPES.ImportExpression &&
(def.node.init.type !== AST_NODE_TYPES.CallExpression ||
!isIdentifier(def.node.init.callee, 'require'))
) {
return null;
}

const source = determineImportSource(def.node.init);
const sourceNode = findImportSourceNode(def.node.init);

if (!source) {
if (!sourceNode || !isStringNode(sourceNode)) {
return null;
}

Expand All @@ -863,7 +852,7 @@ const describeVariableDefAsImport = (
}

return {
source,
source: getStringValue(sourceNode),
imported: getAccessorValue(def.name.parent.key),
local: def.name.name,
};
Expand Down

0 comments on commit 660c04d

Please sign in to comment.