no-import-module-exports.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';var _minimatch = require('minimatch');var _minimatch2 = _interopRequireDefault(_minimatch);
  2. var _path = require('path');var _path2 = _interopRequireDefault(_path);
  3. var _pkgUp = require('pkg-up');var _pkgUp2 = _interopRequireDefault(_pkgUp);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
  4. function getEntryPoint(context) {
  5. const pkgPath = _pkgUp2.default.sync(context.getFilename());
  6. try {
  7. return require.resolve(_path2.default.dirname(pkgPath));
  8. } catch (error) {
  9. // Assume the package has no entrypoint (e.g. CLI packages)
  10. // in which case require.resolve would throw.
  11. return null;
  12. }
  13. }
  14. module.exports = {
  15. meta: {
  16. type: 'problem',
  17. docs: {
  18. description: 'Disallow import statements with module.exports',
  19. category: 'Best Practices',
  20. recommended: true },
  21. fixable: 'code',
  22. schema: [
  23. {
  24. 'type': 'object',
  25. 'properties': {
  26. 'exceptions': { 'type': 'array' } },
  27. 'additionalProperties': false }] },
  28. create(context) {
  29. const importDeclarations = [];
  30. const entryPoint = getEntryPoint(context);
  31. const options = context.options[0] || {};
  32. let alreadyReported = false;
  33. function report(node) {
  34. const fileName = context.getFilename();
  35. const isEntryPoint = entryPoint === fileName;
  36. const isIdentifier = node.object.type === 'Identifier';
  37. const hasKeywords = /^(module|exports)$/.test(node.object.name);
  38. const isException = options.exceptions &&
  39. options.exceptions.some(glob => (0, _minimatch2.default)(fileName, glob));
  40. if (isIdentifier && hasKeywords && !isEntryPoint && !isException) {
  41. importDeclarations.forEach(importDeclaration => {
  42. context.report({
  43. node: importDeclaration,
  44. message: `Cannot use import declarations in modules that export using ` +
  45. `CommonJS (module.exports = 'foo' or exports.bar = 'hi')` });
  46. });
  47. alreadyReported = true;
  48. }
  49. }
  50. return {
  51. ImportDeclaration(node) {
  52. importDeclarations.push(node);
  53. },
  54. MemberExpression(node) {
  55. if (!alreadyReported) {
  56. report(node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