'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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9uby1jb21tb25qcy5qcyJdLCJuYW1lcyI6WyJFWFBPUlRfTUVTU0FHRSIsIklNUE9SVF9NRVNTQUdFIiwibm9ybWFsaXplTGVnYWN5T3B0aW9ucyIsIm9wdGlvbnMiLCJpbmRleE9mIiwiYWxsb3dQcmltaXRpdmVNb2R1bGVzIiwiYWxsb3dQcmltaXRpdmUiLCJub2RlIiwicGFyZW50IiwidHlwZSIsInJpZ2h0IiwiYWxsb3dSZXF1aXJlIiwic2NoZW1hU3RyaW5nIiwiZW51bSIsInNjaGVtYU9iamVjdCIsInByb3BlcnRpZXMiLCJhZGRpdGlvbmFsUHJvcGVydGllcyIsIm1vZHVsZSIsImV4cG9ydHMiLCJtZXRhIiwiZG9jcyIsInVybCIsInNjaGVtYSIsImFueU9mIiwiaXRlbXMiLCJhZGRpdGlvbmFsSXRlbXMiLCJjcmVhdGUiLCJjb250ZXh0Iiwib2JqZWN0IiwibmFtZSIsInByb3BlcnR5IiwicmVwb3J0IiwibWVzc2FnZSIsImlzSW5TY29wZSIsImdldFNjb3BlIiwidmFyaWFibGVzIiwic29tZSIsInZhcmlhYmxlIiwiY2FsbCIsImNhbGxlZSIsImFyZ3VtZW50cyIsImxlbmd0aCIsInZhbHVlIl0sIm1hcHBpbmdzIjoiOztBQUtBOzs7Ozs7QUFFQSxNQUFNQSxpQkFBaUIsdUNBQXZCO0FBQUEsTUFDTUMsaUJBQWlCLDBDQUR2QixDLENBUEE7Ozs7O0FBVUEsU0FBU0Msc0JBQVQsQ0FBZ0NDLE9BQWhDLEVBQXlDO0FBQ3ZDLE1BQUlBLFFBQVFDLE9BQVIsQ0FBZ0IseUJBQWhCLEtBQThDLENBQWxELEVBQXFEO0FBQ25ELFdBQU8sRUFBRUMsdUJBQXVCLElBQX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HBvcnRzJykge1xuICAgICAgICAgIGlmIChhbGxvd1ByaW1pdGl2ZShub2RlLCBvcHRpb25zKSkgcmV0dXJuXG4gICAgICAgICAgY29udGV4dC5yZXBvcnQoeyBub2RlLCBtZXNzYWdlOiBFWFBPUlRfTUVTU0FHRSB9KVxuICAgICAgICB9XG5cbiAgICAgICAgLy8gZXhwb3J0cy5cbiAgICAgICAgaWYgKG5vZGUub2JqZWN0Lm5hbWUgPT09ICdleHBvcnRzJykge1xuICAgICAgICAgIGNvbnN0IGlzSW5TY29wZSA9IGNvbnRleHQuZ2V0U2NvcGUoKVxuICAgICAgICAgICAgLnZhcmlhYmxlc1xuICAgICAgICAgICAgLnNvbWUodmFyaWFibGUgPT4gdmFyaWFibGUubmFtZSA9PT0gJ2V4cG9ydHMnKVxuICAgICAgICAgIGlmICghIGlzSW5TY29wZSkge1xuICAgICAgICAgICAgY29udGV4dC5yZXBvcnQoeyBub2RlLCBtZXNzYWdlOiBFWFBPUlRfTUVTU0FHRSB9KVxuICAgICAgICAgIH1cbiAgICAgICAgfVxuXG4gICAgICB9LFxuICAgICAgJ0NhbGxFeHByZXNzaW9uJzogZnVuY3Rpb24gKGNhbGwpIHtcbiAgICAgICAgaWYgKGNvbnRleHQuZ2V0U2NvcGUoKS50eXBlICE9PSAnbW9kdWxlJykgcmV0dXJuXG4gICAgICAgIGlmIChcbiAgICAgICAgICBjYWxsLnBhcmVudC50eXBlICE9PSAnRXhwcmVzc2lvblN0YXRlbWVudCdcbiAgICAgICAgICAmJiBjYWxsLnBhcmVudC50eXBlICE9PSAnVmFyaWFibGVEZWNsYXJhdG9yJ1xuICAgICAgICAgICYmIGNhbGwucGFyZW50LnR5cGUgIT09ICdBc3NpZ25tZW50RXhwcmVzc2lvbidcbiAgICAgICAgKSByZXR1cm5cblxuICAgICAgICBpZiAoY2FsbC5jYWxsZWUudHlwZSAhPT0gJ0lkZW50aWZpZXInKSByZXR1cm5cbiAgICAgICAgaWYgKGNhbGwuY2FsbGVl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