111 lines
11 KiB
JavaScript
111 lines
11 KiB
JavaScript
|
'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;
|
||
|
}
|
||
|
|
||
|
//------------------------------------------------------------------------------
|
||
|
// Rule Definition
|
||
|
//------------------------------------------------------------------------------
|
||
|
|
||
|
const schemaString = { enum: ['allow-primitive-modules'] };
|
||
|
const schemaObject = {
|
||
|
type: 'object',
|
||
|
properties: {
|
||
|
allowPrimitiveModules: { 'type': 'boolean' },
|
||
|
allowRequire: { '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 (context.getScope().type !== 'module') return;
|
||
|
if (call.parent.type !== 'ExpressionStatement' && call.parent.type !== 'VariableDeclarator' && call.parent.type !== 'AssignmentExpression') 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;
|
||
|
|
||
|
// 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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1jb21tb25qcy5qcyJdLCJuYW1lcyI6WyJFWFBPUlRfTUVTU0FHRSIsIklNUE9SVF9NRVNTQUdFIiwibm9ybWFsaXplTGVnYWN5T3B0aW9ucyIsIm9wdGlvbnMiLCJpbmRleE9mIiwiYWxsb3dQcmlt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