odin-javascript-exercises/node_modules/eslint-plugin-import/lib/rules/no-commonjs.js

65 lines
6.3 KiB
JavaScript
Raw Normal View History

2017-12-15 10:56:14 -08:00
'use strict';
/**
* @fileoverview Rule to prefer ES6 to CJS
* @author Jamund Ferguson
*/
const EXPORT_MESSAGE = 'Expected "export" or "export default"',
IMPORT_MESSAGE = 'Expected "import" instead of "require()"';
function allowPrimitive(node, context) {
if (context.options.indexOf('allow-primitive-modules') < 0) return false;
if (node.parent.type !== 'AssignmentExpression') return false;
return node.parent.right.type !== 'ObjectExpression';
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {}
},
create: function (context) {
return {
'MemberExpression': function (node) {
// module.exports
if (node.object.name === 'module' && node.property.name === 'exports') {
if (allowPrimitive(node, context)) return;
context.report({ node, message: EXPORT_MESSAGE });
}
// exports.
if (node.object.name === 'exports') {
context.report({ node, message: EXPORT_MESSAGE });
}
},
'CallExpression': function (call) {
if (context.getScope().type !== 'module') 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;
// 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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJ1bGVzL25vLWNvbW1vbmpzLmpzIl0sIm5hbWVzIjpbIkVYUE9SVF9NRVNTQUdFIiwiSU1QT1JUX01FU1NBR0UiLCJhbGxvd1ByaW1pdGl2ZSIsIm5vZGUiLCJjb250ZXh0Iiwib3B0aW9ucyIsImluZGV4T2YiLCJwYXJlbnQiLCJ0eXBlIiwicmlnaHQiLCJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsImRvY3MiLCJjcmVhdGUiLCJvYmplY3QiLCJuYW1lIiwicHJvcGVydHkiLCJy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