Skip to content

Commit 908ba82

Browse files
committed
feat: add no import expression eslint rule
1 parent f2c3811 commit 908ba82

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ module.exports = {
124124
new: [],
125125
},
126126
],
127+
'react-internal/no-dynamic-import-in-literal': ERROR,
127128
},
128129

129130
overrides: [
@@ -198,6 +199,7 @@ module.exports = {
198199
'jest/no-focused-tests': ERROR,
199200
'jest/valid-expect': ERROR,
200201
'jest/valid-expect-in-promise': ERROR,
202+
'react-internal/no-dynamic-import-in-literal': OFF,
201203
},
202204
},
203205
{
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails react-core
8+
*/
9+
10+
'use strict';
11+
12+
const rule = require('../no-dynamic-import-in-literal');
13+
const {RuleTester} = require('eslint');
14+
const ruleTester = new RuleTester({
15+
parserOptions: {
16+
ecmaVersion: 8,
17+
sourceType: 'module',
18+
},
19+
});
20+
21+
ruleTester.run('eslint-rules/no-dynamic-import-in-literal', rule, {
22+
valid: [
23+
`console.log(
24+
'import react from "react"'
25+
)`,
26+
`console.log(
27+
'should work for non-import expression'
28+
)`,
29+
],
30+
invalid: [
31+
{
32+
code: `console.log(
33+
'import("react")'
34+
)`,
35+
errors: [
36+
{
37+
message: 'Possible dynamic import expression in literal',
38+
},
39+
],
40+
},
41+
{
42+
code: `console.log(
43+
'const MyComponent = lazy(() => import("./MyComponent"))'
44+
)`,
45+
errors: [
46+
{
47+
message: 'Possible dynamic import expression in literal',
48+
},
49+
],
50+
},
51+
],
52+
});

scripts/eslint-rules/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ module.exports = {
1111
'no-cross-fork-imports': require('./no-cross-fork-imports'),
1212
'no-cross-fork-types': require('./no-cross-fork-types'),
1313
'safe-string-coercion': require('./safe-string-coercion'),
14+
'no-dynamic-import-in-literal': require('./no-dynamic-import-in-literal'),
1415
},
1516
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails react-core
8+
*/
9+
10+
'use strict';
11+
12+
const IMPORT_PATTERN = /import\(([`'"]([^`'"]+)[`'"])*\)/;
13+
14+
module.exports = {
15+
meta: {
16+
schema: [],
17+
},
18+
create(context) {
19+
function checkIsImportExpression(node) {
20+
const {type: nodeType} = node;
21+
const content =
22+
(nodeType === 'Literal' && node.value) ||
23+
(nodeType === 'TemplateLiteral' && node.quasis[0].value.raw);
24+
const isPossibleImportExpression = IMPORT_PATTERN.test(content);
25+
if (isPossibleImportExpression) {
26+
context.report(node, 'Possible dynamic import expression in literal');
27+
}
28+
}
29+
return {
30+
Literal: checkIsImportExpression,
31+
TemplateLiteral: checkIsImportExpression,
32+
};
33+
},
34+
};

0 commit comments

Comments
 (0)