141 lines
14 KiB
JavaScript
141 lines
14 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { 'default': obj };}
|
||
|
|
||
|
var EXPORT_MESSAGE = 'Expected "export" or "export default"'; /**
|
||
|
* @fileoverview Rule to prefer ES6 to CJS
|
||
|
* @author Jamund Ferguson
|
||
|
*/var IMPORT_MESSAGE = 'Expected "import" instead of "require()"';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/HEAD/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;
|
||
|
}
|
||
|
|
||
|
function isLiteralString(node) {
|
||
|
return node.type === 'Literal' && typeof node.value === 'string' ||
|
||
|
node.type === 'TemplateLiteral' && node.expressions.length === 0;
|
||
|
}
|
||
|
|
||
|
//------------------------------------------------------------------------------
|
||
|
// Rule Definition
|
||
|
//------------------------------------------------------------------------------
|
||
|
|
||
|
var schemaString = { 'enum': ['allow-primitive-modules'] };
|
||
|
var schemaObject = {
|
||
|
type: 'object',
|
||
|
properties: {
|
||
|
allowPrimitiveModules: { type: 'boolean' },
|
||
|
allowRequire: { type: 'boolean' },
|
||
|
allowConditionalRequire: { type: 'boolean' } },
|
||
|
|
||
|
additionalProperties: false };
|
||
|
|
||
|
|
||
|
module.exports = {
|
||
|
meta: {
|
||
|
type: 'suggestion',
|
||
|
docs: {
|
||
|
category: 'Module systems',
|
||
|
description: 'Forbid CommonJS `require` calls and `module.exports` or `exports.*`.',
|
||
|
url: (0, _docsUrl2['default'])('no-commonjs') },
|
||
|
|
||
|
|
||
|
schema: {
|
||
|
anyOf: [
|
||
|
{
|
||
|
type: 'array',
|
||
|
items: [schemaString],
|
||
|
additionalItems: false },
|
||
|
|
||
|
{
|
||
|
type: 'array',
|
||
|
items: [schemaObject],
|
||
|
additionalItems: false }] } },
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
create: function () {function create(context) {
|
||
|
var options = normalizeLegacyOptions(context.options);
|
||
|
|
||
|
return {
|
||
|
|
||
|
MemberExpression: function () {function MemberExpression(node) {
|
||
|
|
||
|
// module.exports
|
||
|
if (node.object.name === 'module' && node.property.name === 'exports') {
|
||
|
if (allowPrimitive(node, options)) {return;}
|
||
|
context.report({ node: node, message: EXPORT_MESSAGE });
|
||
|
}
|
||
|
|
||
|
// exports.
|
||
|
if (node.object.name === 'exports') {
|
||
|
var isInScope = context.getScope().
|
||
|
variables.
|
||
|
some(function (variable) {return variable.name === 'exports';});
|
||
|
if (!isInScope) {
|
||
|
context.report({ node: node, message: EXPORT_MESSAGE });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}return MemberExpression;}(),
|
||
|
CallExpression: function () {function CallExpression(call) {
|
||
|
if (!validateScope(context.getScope())) {return;}
|
||
|
|
||
|
if (call.callee.type !== 'Identifier') {return;}
|
||
|
if (call.callee.name !== 'require') {return;}
|
||
|
|
||
|
if (call.arguments.length !== 1) {return;}
|
||
|
if (!isLiteralString(call.arguments[0])) {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 });
|
||
|
|
||
|
}return CallExpression;}() };
|
||
|
|
||
|
|
||
|
}return create;}() };
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1jb21tb25qcy5qcyJdLCJuYW1lcyI6WyJFWFBPUlRfTUVTU0FHRSIsIklNUE9SVF9NRVNTQUdFIiwibm9ybWFsaXplTGVnYWN5T3B0aW9ucyIsIm9wdGlvbnMiLCJpbmRleE9mIiwiYWxsb3dQcmltaXRpdmVNb2R1bGVzIiwiYWxsb3dQcmltaXRpdmUiLCJub2RlIiwicGFyZW50IiwidHlwZSIsInJpZ2h0IiwiYWxsb3dSZXF1aXJlIiwiYWxsb3dDb25kaXRpb25hbFJlcXVpcmUiLCJ2YWxpZGF0ZVNjb3BlIiwic2NvcGUiLCJ2YXJpYWJsZVNjb3BlIiwiaXNDb25kaXRpb25hbCIsImlzTGl0ZXJhbFN0cmluZyIsInZhbHVlIiwiZXhwcmVzc2lvbnMiLCJsZW5ndGgiLCJzY2hlbWFTdHJpbmciLCJzY2hlbWFPYmplY3QiLCJwcm9wZXJ0aWVzIiwiYWRkaXRpb25hbFByb3BlcnRpZXMiLCJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsImRvY3MiLCJjYXRlZ29yeSIsImRlc2NyaXB0aW9uIiwidXJsIiwic2NoZW1hIiwiYW55T2YiLCJpdGVtcyIsImFkZGl0aW9uYWxJdGVtcyIsImNyZWF0ZSIsIm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