katoikia-app/web-ui/web-react/node_modules/eslint-plugin-import/lib/rules/no-commonjs.js

128 lines
13 KiB
JavaScript
Raw Normal View History

2022-07-06 04:15:11 +00:00
'use strict';
var _docsUrl = require('../docsUrl');
var _docsUrl2 = _interopRequireDefault(_docsUrl);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const EXPORT_MESSAGE = 'Expected "export" or "export default"',
IMPORT_MESSAGE = 'Expected "import" instead of "require()"'; /**
* @fileoverview Rule to prefer ES6 to CJS
* @author Jamund Ferguson
*/
function normalizeLegacyOptions(options) {
if (options.indexOf('allow-primitive-modules') >= 0) {
return { allowPrimitiveModules: true };
}
return options[0] || {};
}
function allowPrimitive(node, options) {
if (!options.allowPrimitiveModules) return false;
if (node.parent.type !== 'AssignmentExpression') return false;
return node.parent.right.type !== 'ObjectExpression';
}
function allowRequire(node, options) {
return options.allowRequire;
}
function allowConditionalRequire(node, options) {
return options.allowConditionalRequire !== false;
}
function validateScope(scope) {
return scope.variableScope.type === 'module';
}
// https://github.com/estree/estree/blob/master/es5.md
function isConditional(node) {
if (node.type === 'IfStatement' || node.type === 'TryStatement' || node.type === 'LogicalExpression' || node.type === 'ConditionalExpression') return true;
if (node.parent) return isConditional(node.parent);
return false;
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const schemaString = { enum: ['allow-primitive-modules'] };
const schemaObject = {
type: 'object',
properties: {
allowPrimitiveModules: { 'type': 'boolean' },
allowRequire: { 'type': 'boolean' },
allowConditionalRequire: { 'type': 'boolean' }
},
additionalProperties: false
};
module.exports = {
meta: {
type: 'suggestion',
docs: {
url: (0, _docsUrl2.default)('no-commonjs')
},
schema: {
anyOf: [{
type: 'array',
items: [schemaString],
additionalItems: false
}, {
type: 'array',
items: [schemaObject],
additionalItems: false
}]
}
},
create: function (context) {
const options = normalizeLegacyOptions(context.options);
return {
'MemberExpression': function (node) {
// module.exports
if (node.object.name === 'module' && node.property.name === 'exports') {
if (allowPrimitive(node, options)) return;
context.report({ node, message: EXPORT_MESSAGE });
}
// exports.
if (node.object.name === 'exports') {
const isInScope = context.getScope().variables.some(variable => variable.name === 'exports');
if (!isInScope) {
context.report({ node, message: EXPORT_MESSAGE });
}
}
},
'CallExpression': function (call) {
if (!validateScope(context.getScope())) return;
if (call.callee.type !== 'Identifier') return;
if (call.callee.name !== 'require') return;
if (call.arguments.length !== 1) return;
var module = call.arguments[0];
if (module.type !== 'Literal') return;
if (typeof module.value !== 'string') return;
if (allowRequire(call, options)) return;
if (allowConditionalRequire(call, options) && isConditional(call.parent)) return;
// keeping it simple: all 1-string-arg `require` calls are reported
context.report({
node: call.callee,
message: IMPORT_MESSAGE
});
}
};
}
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1jb21tb25qcy5qcyJdLCJuYW1lcyI6WyJFWFBPUlRfTUVTU0FHRSIsIklNUE9SVF9NRVNTQUdFIiwibm9ybWFsaXplTGVnYWN5T3B0aW9ucyIsIm9wdGlvbnMiLCJpbmRleE9mIiwiYWxsb3dQcmltaXRpdmVNb2R1bGVzIiwiYWxsb3dQcmltaXRpdmUiLCJub2RlIiwicGFyZW50IiwidHlwZSIsInJpZ2h0IiwiYWxsb3dSZXF1aXJlIiwiYWxsb3dDb25kaXRpb25hbFJlcXVpcmUiLCJ2YWxpZGF0ZVNjb3BlIiwic2NvcGUiLCJ2YXJpYWJsZVNjb3BlIiwiaXNDb25kaXRpb25hbCIsInNjaGVtYVN0cmluZyIsImVudW0iLCJzY2hlbWFPYmplY3QiLCJwcm9wZXJ0aWVzIiwiYWRkaXRpb25hbFByb3BlcnRpZXMiLCJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsImRvY3MiLCJ1cmwiLCJzY2hlbWEiLCJhbnlPZiIsIml0ZW1zIiwiYWRkaXRpb25hbEl0ZW1zIiwiY3JlYXRlIiwiY29udGV4dCIsIm9i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