123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- '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"';
- const 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';
- }
- 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;
- }
- 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) {
-
- if (node.object.name === 'module' && node.property.name === 'exports') {
- if (allowPrimitive(node, options)) return;
- context.report({ node, message: EXPORT_MESSAGE });
- }
-
- 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;
- if (!isLiteralString(call.arguments[0])) return;
- if (allowRequire(call, options)) return;
- if (allowConditionalRequire(call, options) && isConditional(call.parent)) return;
-
- context.report({
- node: call.callee,
- message: IMPORT_MESSAGE });
- } };
- } };
|