77 lines
7.1 KiB
JavaScript
77 lines
7.1 KiB
JavaScript
'use strict';var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
|
|
|
|
function isRequire(node) {
|
|
return node &&
|
|
node.callee &&
|
|
node.callee.type === 'Identifier' &&
|
|
node.callee.name === 'require' &&
|
|
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;
|
|
}
|
|
|
|
var dynamicImportErrorMessage = 'Calls to import() should use string literals';
|
|
|
|
module.exports = {
|
|
meta: {
|
|
type: 'suggestion',
|
|
docs: {
|
|
category: 'Static analysis',
|
|
description: 'Forbid `require()` calls with expressions.',
|
|
url: (0, _docsUrl2['default'])('no-dynamic-require') },
|
|
|
|
schema: [
|
|
{
|
|
type: 'object',
|
|
properties: {
|
|
esmodule: {
|
|
type: 'boolean' } },
|
|
|
|
|
|
additionalProperties: false }] },
|
|
|
|
|
|
|
|
|
|
create: function () {function create(context) {
|
|
var options = context.options[0] || {};
|
|
|
|
return {
|
|
CallExpression: function () {function CallExpression(node) {
|
|
if (!node.arguments[0] || isStaticValue(node.arguments[0])) {
|
|
return;
|
|
}
|
|
if (isRequire(node)) {
|
|
return context.report({
|
|
node: node,
|
|
message: 'Calls to require() should use string literals' });
|
|
|
|
}
|
|
if (options.esmodule && isDynamicImport(node)) {
|
|
return context.report({
|
|
node: node,
|
|
message: dynamicImportErrorMessage });
|
|
|
|
}
|
|
}return CallExpression;}(),
|
|
ImportExpression: function () {function ImportExpression(node) {
|
|
if (!options.esmodule || isStaticValue(node.source)) {
|
|
return;
|
|
}
|
|
return context.report({
|
|
node: node,
|
|
message: dynamicImportErrorMessage });
|
|
|
|
}return Im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