Skip to content

feat: add no import expression eslint rule #22507

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ module.exports = {
new: [],
},
],
'react-internal/no-dynamic-import-in-literal': ERROR,
},

overrides: [
Expand Down Expand Up @@ -198,6 +199,7 @@ module.exports = {
'jest/no-focused-tests': ERROR,
'jest/valid-expect': ERROR,
'jest/valid-expect-in-promise': ERROR,
'react-internal/no-dynamic-import-in-literal': OFF,
},
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/

'use strict';

const rule = require('../no-dynamic-import-in-literal');
const {RuleTester} = require('eslint');
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 8,
sourceType: 'module',
},
});

ruleTester.run('eslint-rules/no-dynamic-import-in-literal', rule, {
valid: [
`console.log(
'import react from "react"'
)`,
`console.log(
'should work for non-import expression'
)`,
],
invalid: [
{
code: `console.log(
'import("react")'
)`,
errors: [
{
message: 'Possible dynamic import expression in literal',
},
],
},
{
code: `console.log(
'const MyComponent = lazy(() => import("./MyComponent"))'
)`,
errors: [
{
message: 'Possible dynamic import expression in literal',
},
],
},
],
});
1 change: 1 addition & 0 deletions scripts/eslint-rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ module.exports = {
'no-cross-fork-imports': require('./no-cross-fork-imports'),
'no-cross-fork-types': require('./no-cross-fork-types'),
'safe-string-coercion': require('./safe-string-coercion'),
'no-dynamic-import-in-literal': require('./no-dynamic-import-in-literal'),
},
};
34 changes: 34 additions & 0 deletions scripts/eslint-rules/no-dynamic-import-in-literal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/

'use strict';

const IMPORT_PATTERN = /import\(([`'"]([^`'"]+)[`'"])*\)/;

module.exports = {
meta: {
schema: [],
},
create(context) {
function checkIsImportExpression(node) {
const {type: nodeType} = node;
const content =
(nodeType === 'Literal' && node.value) ||
(nodeType === 'TemplateLiteral' && node.quasis[0].value.raw);
const isPossibleImportExpression = IMPORT_PATTERN.test(content);
if (isPossibleImportExpression) {
context.report(node, 'Possible dynamic import expression in literal');
}
}
return {
Literal: checkIsImportExpression,
TemplateLiteral: checkIsImportExpression,
};
},
};