Skip to content

Commit

Permalink
add option esmodule to no-dynamic-require
Browse files Browse the repository at this point in the history
  • Loading branch information
vikr01 committed Oct 26, 2018
1 parent db471a8 commit 836eeab
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/rules/no-dynamic-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ function isRequire(node) {
node.arguments.length >= 1
}

function isDynamicImport(node) {
return node &&
node.callee &&
node.callee.type === 'Import'
}

function isStaticValue(arg) {
return arg.type === 'Literal' ||
(arg.type === 'TemplateLiteral' && arg.expressions.length === 0)
Expand All @@ -18,17 +24,37 @@ module.exports = {
docs: {
url: docsUrl('no-dynamic-require'),
},
schema: [
{
type: 'object',
properties: {
esmodule: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
},

create: function (context) {
const options = context.options[0] || {}

return {
CallExpression(node) {
if (isRequire(node) && !isStaticValue(node.arguments[0])) {
context.report({
if(!node.arguments[0] || isStaticValue(node.arguments[0])) return
if (isRequire(node)) {
return context.report({
node,
message: 'Calls to require() should use string literals',
})
}
else if(options.esmodule && isDynamicImport(node)) {
return context.report({
node,
message: 'Calls to import() should use string literals',
})
}
},
}
},
Expand Down
74 changes: 74 additions & 0 deletions tests/src/rules/no-dynamic-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const error = {
message: 'Calls to require() should use string literals',
}

const dynamicImportError = {
ruleId: 'no-dynamic-require',
message: 'Calls to import() should use string literals',
}

ruleTester.run('no-dynamic-require', rule, {
valid: [
test({ code: 'import _ from "lodash"'}),
Expand All @@ -23,6 +28,48 @@ ruleTester.run('no-dynamic-require', rule, {
test({ code: 'var foo = require(`foo`)'}),
test({ code: 'var foo = require("./foo")'}),
test({ code: 'var foo = require("@scope/foo")'}),

//dynamic import
test({
code: 'import("foo")',
parser: 'babel-eslint',
options: [{esmodule: true}],
}),
test({
code: 'import(`foo`)',
parser: 'babel-eslint',
options: [{esmodule: true}],
}),
test({
code: 'import("./foo")',
parser: 'babel-eslint',
options: [{esmodule: true}],
}),
test({
code: 'import("@scope/foo")',
parser: 'babel-eslint',
options: [{esmodule: true}],
}),
test({
code: 'var foo = import("foo")',
parser: 'babel-eslint',
options: [{esmodule: true}],
}),
test({
code: 'var foo = import(`foo`)',
parser: 'babel-eslint',
options: [{esmodule: true}],
}),
test({
code: 'var foo = import("./foo")',
parser: 'babel-eslint',
options: [{esmodule: true}],
}),
test({
code: 'var foo = import("@scope/foo")',
parser: 'babel-eslint',
options: [{esmodule: true}],
}),
],
invalid: [
test({
Expand All @@ -44,6 +91,33 @@ ruleTester.run('no-dynamic-require', rule, {
test({
code: 'require(name + "foo", "bar")',
errors: [error],
options: [{esmodule: true}],
}),

// dynamic import
test({
code: 'import("../" + name)',
errors: [dynamicImportError],
parser: 'babel-eslint',
options: [{esmodule: true}],
}),
test({
code: 'import(`../${name}`)',
errors: [dynamicImportError],
parser: 'babel-eslint',
options: [{esmodule: true}],
}),
test({
code: 'import(name)',
errors: [dynamicImportError],
parser: 'babel-eslint',
options: [{esmodule: true}],
}),
test({
code: 'import(name())',
errors: [dynamicImportError],
parser: 'babel-eslint',
options: [{esmodule: true}],
}),
],
})

0 comments on commit 836eeab

Please sign in to comment.