Skip to content

Commit c5e66d8

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

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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:
33+
`console.log(
34+
'import("react")'
35+
)`,
36+
errors: [
37+
{
38+
message: 'Possible dynamic import expression in literal',
39+
},
40+
],
41+
},
42+
{
43+
code:
44+
`console.log(
45+
'const MyComponent = lazy(() => import("./MyComponent"))'
46+
)`,
47+
errors: [
48+
{
49+
message: 'Possible dynamic import expression in literal',
50+
},
51+
],
52+
},
53+
],
54+
});

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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 = (nodeType === 'Literal' && node.value) || (nodeType === 'TemplateLiteral' && node.quasis[0].value.raw);
22+
const isPossibleImportExpression = IMPORT_PATTERN.test(content);
23+
if (isPossibleImportExpression) {
24+
context.report(
25+
node,
26+
'Possible dynamic import expression in literal'
27+
);
28+
}
29+
}
30+
return {
31+
Literal: checkIsImportExpression,
32+
TemplateLiteral: checkIsImportExpression,
33+
};
34+
},
35+
};

0 commit comments

Comments
 (0)