'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: { 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') 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 });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3NhZ2U6IEVYUE9SVF9NRVNTQUdFIH0pXG4gICAgICAgIH1cblxuICAgICAgICAvLyBleHBvcnRzLlxuICAgICAgICBpZiAobm9kZS5vYmplY3QubmFtZSA9PT0gJ2V4cG9ydHMnKSB7XG4gICAgICAgICAgY29uc3QgaXNJblNjb3BlID0gY29udGV4dC5nZXRTY29wZSgpXG4gICAgICAgICAgICAudmFyaWFibGVzXG4gICAgICAgICAgICAuc29tZSh2YXJpYWJsZSA9PiB2YXJpYWJsZS5uYW1lID09PSAnZXhwb3J0cycpXG4gICAgICAgICAgaWYgKCEgaXNJblNjb3BlKSB7XG4gICAgICAgICAgICBjb250ZXh0LnJlcG9ydCh7IG5vZGUsIG1lc3NhZ2U6IEVYUE9SVF9NRVNTQUdFIH0pXG4gICAgICAgICAgfVxuICAgICAgICB9XG5cbiAgICAgIH0sXG4gICAgICAnQ2FsbEV4cHJlc3Npb24nOiBmdW5jdGlvbiAoY2FsbCkge1xuICAgICAgICBpZiAoY29udGV4dC5nZXRTY29wZSgpLnR5cGUgIT09ICdtb2R1bGUnKSByZXR1cm5cbiAgICAgICAgaWYgKFxuICAgICAgICAgIGNhbGwucGFyZW50LnR5cGUgIT09ICdFeHByZXNzaW9uU3RhdGVtZW50J1xuICAgICAgICAgICYmIGNhbGwucGFyZW50LnR5cGUgIT09ICdWYXJpYWJsZURlY2xhcmF0b3InXG4gICAgICAgICkgcmV0dXJuXG5cbiAgICAgICAgaWYgKGNhbGwuY2FsbGVlLnR5cGUgIT09ICdJZGVudGlmaWVyJykgcmV0dXJuXG4gICAgICAgIGlmIChjYWxsLmNhbGxlZS5uYW1lICE9PSAncmVxdWlyZScpIHJldHVyblxuXG4gICAgICAgIGlmIChjYWxsLmFyZ3VtZW50cy5sZW5ndGggIT09IDEpIHJldHVyblxuICAgICAgICB2YXIgbW9kdWxlID0gY2FsbC5hcmd1bWVudHNbMF1cblxuICAgICAgICBpZiAobW9kdWxlLnR5cGUgIT09ICdMaXRlcmFsJykgcmV0dXJuXG4gICAgICAgIGlmICh0eXBlb2YgbW9kdWxlLnZhbHVlICE9PSAnc3RyaW5nJykgcmV0dXJuXG5cbiAgICAgICAgaWYgKGFsbG93UmVxdWlyZShjYWxsLCBvcHRpb25zKSkgcmV0dXJuXG5cbiAgICAgICAgLy8ga2VlcGluZyBpdCBzaW1wbGU6IGFsbCAxLXN0cmluZy1hcmcgYHJlcXVpcmVgIGNhbGxzIGFyZSByZXBvcnRlZFxuICAgICAgICBjb250ZXh0LnJlcG9ydCh7XG4gICAgICAgICAgbm9kZTogY2FsbC5jYWxsZWUsXG4gICAgICAgICAgbWVzc2FnZTogSU1QT1JUX01FU1NBR0UsXG4gICAgICAgIH0pXG4gICAgICB9LFxuICAgIH1cblxuICB9LFxufVxuIl19