Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 70a59fe

Browse files
vikr01ljharb
authored andcommittedOct 21, 2018
[fix] Fix overwriting of dynamic import() CallExpression
- fix import() to work with no-cycle - add tests using multiple imports in no-cycle Fixes import-js#1035. Fixes import-js#1166.
1 parent e4850df commit 70a59fe

File tree

6 files changed

+74
-6
lines changed

6 files changed

+74
-6
lines changed
 

‎src/rules/no-cycle.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,10 @@ module.exports = {
3131
function checkSourceValue(sourceNode, importer) {
3232
const imported = Exports.get(sourceNode.value, context)
3333

34-
if (sourceNode.parent && sourceNode.parent.importKind === 'type') {
34+
if (importer.importKind === 'type') {
3535
return // no Flow import resolution
3636
}
3737

38-
if (sourceNode._babelType === 'Literal') {
39-
return // no Flow import resolution, workaround for ESLint < 5.x
40-
}
41-
4238
if (imported == null) {
4339
return // no-unresolved territory
4440
}

‎tests/src/rules/no-cycle.js

+32
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,23 @@ ruleTester.run('no-cycle', rule, {
3636
code: 'import { foo } from "./depth-two"',
3737
options: [{ maxDepth: 1 }],
3838
}),
39+
test({
40+
code: 'import { foo, bar } from "./depth-two"',
41+
options: [{ maxDepth: 1 }],
42+
}),
43+
test({
44+
code: 'import("./depth-two").then(function({ foo }){})',
45+
options: [{ maxDepth: 1 }],
46+
parser: 'babel-eslint',
47+
}),
3948
test({
4049
code: 'import type { FooType } from "./depth-one"',
4150
parser: 'babel-eslint',
4251
}),
52+
test({
53+
code: 'import type { FooType, BarType } from "./depth-one"',
54+
parser: 'babel-eslint',
55+
}),
4356
],
4457
invalid: [
4558
test({
@@ -84,10 +97,29 @@ ruleTester.run('no-cycle', rule, {
8497
code: 'import { two } from "./depth-three-star"',
8598
errors: [error(`Dependency cycle via ./depth-two:1=>./depth-one:1`)],
8699
}),
100+
test({
101+
code: 'import one, { two, three } from "./depth-three-star"',
102+
errors: [error(`Dependency cycle via ./depth-two:1=>./depth-one:1`)],
103+
}),
87104
test({
88105
code: 'import { bar } from "./depth-three-indirect"',
89106
errors: [error(`Dependency cycle via ./depth-two:1=>./depth-one:1`)],
90107
}),
108+
test({
109+
code: 'import { bar } from "./depth-three-indirect"',
110+
errors: [error(`Dependency cycle via ./depth-two:1=>./depth-one:1`)],
111+
parser: 'babel-eslint',
112+
}),
113+
test({
114+
code: 'import("./depth-three-star")',
115+
errors: [error(`Dependency cycle via ./depth-two:1=>./depth-one:1`)],
116+
parser: 'babel-eslint',
117+
}),
118+
test({
119+
code: 'import("./depth-three-indirect")',
120+
errors: [error(`Dependency cycle via ./depth-two:1=>./depth-one:1`)],
121+
parser: 'babel-eslint',
122+
}),
91123
],
92124
})
93125
// })

‎tests/src/rules/no-relative-parent-imports.js

+8
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ ruleTester.run('no-relative-parent-imports', rule, {
9393
line: 1,
9494
column: 17
9595
}]
96+
}),
97+
test({
98+
code: 'import("../../api/service")',
99+
errors: [ {
100+
message: 'Relative imports from parent directories are not allowed. Please either pass what you\'re importing through at runtime (dependency injection), move `index.js` to same directory as `../../api/service` or consider making `../../api/service` a package.',
101+
line: 1,
102+
column: 8
103+
}],
96104
})
97105
],
98106
})

‎tests/src/rules/no-unresolved.js

+9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ function runResolverTests(resolver) {
2929
rest({ code: "import bar from './bar.js';" }),
3030
rest({ code: "import {someThing} from './test-module';" }),
3131
rest({ code: "import fs from 'fs';" }),
32+
rest({ code: "import('fs');"
33+
, parser: 'babel-eslint' }),
3234

3335
rest({ code: 'import * as foo from "a"' }),
3436

@@ -114,6 +116,13 @@ function runResolverTests(resolver) {
114116
"module 'in-alternate-root'."
115117
, type: 'Literal',
116118
}]}),
119+
rest({
120+
code: "import('in-alternate-root').then(function({DEEP}){});",
121+
errors: [{ message: 'Unable to resolve path to ' +
122+
"module 'in-alternate-root'."
123+
, type: 'Literal',
124+
}],
125+
parser: 'babel-eslint'}),
117126

118127
rest({ code: 'export { foo } from "./does-not-exist"'
119128
, errors: ["Unable to resolve path to module './does-not-exist'."] }),

‎tests/src/rules/no-useless-path-segments.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ function runResolverTests(resolver) {
1717
test({ code: 'import "."' }),
1818
test({ code: 'import ".."' }),
1919
test({ code: 'import fs from "fs"' }),
20-
test({ code: 'import fs from "fs"' }),
2120

2221
// ES modules + noUselessIndex
2322
test({ code: 'import "../index"' }), // noUselessIndex is false by default
@@ -28,6 +27,13 @@ function runResolverTests(resolver) {
2827
test({ code: 'import "./malformed.js"', options: [{ noUselessIndex: true }] }), // ./malformed directory does not exist
2928
test({ code: 'import "./malformed"', options: [{ noUselessIndex: true }] }), // ./malformed directory does not exist
3029
test({ code: 'import "./importType"', options: [{ noUselessIndex: true }] }), // ./importType.js does not exist
30+
31+
test({ code: 'import(".")'
32+
, parser: 'babel-eslint' }),
33+
test({ code: 'import("..")'
34+
, parser: 'babel-eslint' }),
35+
test({ code: 'import("fs").then(function(fs){})'
36+
, parser: 'babel-eslint' }),
3137
],
3238

3339
invalid: [
@@ -190,6 +196,21 @@ function runResolverTests(resolver) {
190196
options: [{ noUselessIndex: true }],
191197
errors: ['Useless path segments for "../index.js", should be ".."'],
192198
}),
199+
test({
200+
code: 'import("./")',
201+
errors: [ 'Useless path segments for "./", should be "."'],
202+
parser: 'babel-eslint',
203+
}),
204+
test({
205+
code: 'import("../")',
206+
errors: [ 'Useless path segments for "../", should be ".."'],
207+
parser: 'babel-eslint',
208+
}),
209+
test({
210+
code: 'import("./deep//a")',
211+
errors: [ 'Useless path segments for "./deep//a", should be "./deep/a"'],
212+
parser: 'babel-eslint',
213+
}),
193214
],
194215
})
195216
}

‎utils/moduleVisitor.js

+2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ exports.default = function visitModules(visitor, options) {
9191
}
9292

9393
if (options.commonjs || options.amd) {
94+
const currentCallExpression = visitors['CallExpression']
9495
visitors['CallExpression'] = function (call) {
96+
if (currentCallExpression) currentCallExpression(call)
9597
if (options.commonjs) checkCommon(call)
9698
if (options.amd) checkAMD(call)
9799
}

0 commit comments

Comments
 (0)