export.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. 'use strict';var _slicedToArray = function () {function sliceIterator(arr, i) {var _arr = [];var _n = true;var _d = false;var _e = undefined;try {for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {_arr.push(_s.value);if (i && _arr.length === i) break;}} catch (err) {_d = true;_e = err;} finally {try {if (!_n && _i["return"]) _i["return"]();} finally {if (_d) throw _e;}}return _arr;}return function (arr, i) {if (Array.isArray(arr)) {return arr;} else if (Symbol.iterator in Object(arr)) {return sliceIterator(arr, i);} else {throw new TypeError("Invalid attempt to destructure non-iterable instance");}};}();var _ExportMap = require('../ExportMap');var _ExportMap2 = _interopRequireDefault(_ExportMap);
  2. var _docsUrl = require('../docsUrl');var _docsUrl2 = _interopRequireDefault(_docsUrl);
  3. var _arrayIncludes = require('array-includes');var _arrayIncludes2 = _interopRequireDefault(_arrayIncludes);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
  4. /*
  5. Notes on TypeScript namespaces aka TSModuleDeclaration:
  6. There are two forms:
  7. - active namespaces: namespace Foo {} / module Foo {}
  8. - ambient modules; declare module "eslint-plugin-import" {}
  9. active namespaces:
  10. - cannot contain a default export
  11. - cannot contain an export all
  12. - cannot contain a multi name export (export { a, b })
  13. - can have active namespaces nested within them
  14. ambient namespaces:
  15. - can only be defined in .d.ts files
  16. - cannot be nested within active namespaces
  17. - have no other restrictions
  18. */
  19. const rootProgram = 'root';
  20. const tsTypePrefix = 'type:';
  21. /**
  22. * Detect function overloads like:
  23. * ```ts
  24. * export function foo(a: number);
  25. * export function foo(a: string);
  26. * export function foo(a: number|string) { return a; }
  27. * ```
  28. * @param {Set<Object>} nodes
  29. * @returns {boolean}
  30. */
  31. function isTypescriptFunctionOverloads(nodes) {
  32. const types = new Set(Array.from(nodes, node => node.parent.type));
  33. return (
  34. types.has('TSDeclareFunction') && (
  35. types.size === 1 ||
  36. types.size === 2 && types.has('FunctionDeclaration')));
  37. }
  38. module.exports = {
  39. meta: {
  40. type: 'problem',
  41. docs: {
  42. url: (0, _docsUrl2.default)('export') },
  43. schema: [] },
  44. create: function (context) {
  45. const namespace = new Map([[rootProgram, new Map()]]);
  46. function addNamed(name, node, parent, isType) {
  47. if (!namespace.has(parent)) {
  48. namespace.set(parent, new Map());
  49. }
  50. const named = namespace.get(parent);
  51. const key = isType ? `${tsTypePrefix}${name}` : name;
  52. let nodes = named.get(key);
  53. if (nodes == null) {
  54. nodes = new Set();
  55. named.set(key, nodes);
  56. }
  57. nodes.add(node);
  58. }
  59. function getParent(node) {
  60. if (node.parent && node.parent.type === 'TSModuleBlock') {
  61. return node.parent.parent;
  62. }
  63. // just in case somehow a non-ts namespace export declaration isn't directly
  64. // parented to the root Program node
  65. return rootProgram;
  66. }
  67. return {
  68. 'ExportDefaultDeclaration': node => addNamed('default', node, getParent(node)),
  69. 'ExportSpecifier': node => addNamed(
  70. node.exported.name,
  71. node.exported,
  72. getParent(node.parent)),
  73. 'ExportNamedDeclaration': function (node) {
  74. if (node.declaration == null) return;
  75. const parent = getParent(node);
  76. // support for old TypeScript versions
  77. const isTypeVariableDecl = node.declaration.kind === 'type';
  78. if (node.declaration.id != null) {
  79. if ((0, _arrayIncludes2.default)([
  80. 'TSTypeAliasDeclaration',
  81. 'TSInterfaceDeclaration'],
  82. node.declaration.type)) {
  83. addNamed(node.declaration.id.name, node.declaration.id, parent, true);
  84. } else {
  85. addNamed(node.declaration.id.name, node.declaration.id, parent, isTypeVariableDecl);
  86. }
  87. }
  88. if (node.declaration.declarations != null) {
  89. for (const declaration of node.declaration.declarations) {
  90. (0, _ExportMap.recursivePatternCapture)(declaration.id, v =>
  91. addNamed(v.name, v, parent, isTypeVariableDecl));
  92. }
  93. }
  94. },
  95. 'ExportAllDeclaration': function (node) {
  96. if (node.source == null) return; // not sure if this is ever true
  97. // `export * as X from 'path'` does not conflict
  98. if (node.exported && node.exported.name) return;
  99. const remoteExports = _ExportMap2.default.get(node.source.value, context);
  100. if (remoteExports == null) return;
  101. if (remoteExports.errors.length) {
  102. remoteExports.reportErrors(context, node);
  103. return;
  104. }
  105. const parent = getParent(node);
  106. let any = false;
  107. remoteExports.forEach((v, name) => {
  108. if (name !== 'default') {
  109. any = true; // poor man's filter
  110. addNamed(name, node, parent);
  111. }
  112. });
  113. if (!any) {
  114. context.report(
  115. node.source,
  116. `No named exports found in module '${node.source.value}'.`);
  117. }
  118. },
  119. 'Program:exit': function () {
  120. for (const _ref of namespace) {var _ref2 = _slicedToArray(_ref, 2);const named = _ref2[1];
  121. for (const _ref3 of named) {var _ref4 = _slicedToArray(_ref3, 2);const name = _ref4[0];const nodes = _ref4[1];
  122. if (nodes.size <= 1) continue;
  123. if (isTypescriptFunctionOverloads(nodes)) continue;
  124. for (const node of nodes) {
  125. if (name === 'default') {
  126. context.report(node, 'Multiple default exports.');
  127. } else {
  128. context.report(
  129. node,
  130. `Multiple exports of name '${name.replace(tsTypePrefix, '')}'.`);
  131. }
  132. }
  133. }
  134. }
  135. } };
  136. } };
  137. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ydWxlcy9leHBvcnQuanMiXSwibmFtZXMiOlsicm9vdFByb2dyYW0iLCJ0c1R5cGVQcmVmaXgiLCJpc1R5cGVzY3JpcHRGdW5jdGlvbk92ZXJsb2FkcyIsIm5vZGVzIiwidHlwZXMiLCJTZXQiLCJBcnJheSIsImZyb20iLCJub2RlIiwicGFyZW50IiwidHlwZSIsImhhcyIsInNpemUiLCJtb2R1bGUiLCJleHBvcnRzIiwibWV0YSIsImRvY3MiLCJ1cmwiLCJzY2hlbWEiLCJjcmVhdGUiLCJjb250ZXh0IiwibmFtZXNw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gICAgICAgbm9kZS5zb3VyY2UsXG4gICAgICAgICAgICBgTm8gbmFtZWQgZXhwb3J0cyBmb3VuZCBpbiBtb2R1bGUgJyR7bm9kZS5zb3VyY2UudmFsdWV9Jy5gXG4gICAgICAgICAgKTtcbiAgICAgICAgfVxuICAgICAgfSxcblxuICAgICAgJ1Byb2dyYW06ZXhpdCc6IGZ1bmN0aW9uICgpIHtcbiAgICAgICAgZm9yIChjb25zdCBbLCBuYW1lZF0gb2YgbmFtZXNwYWNlKSB7XG4gICAgICAgICAgZm9yIChjb25zdCBbbmFtZSwgbm9kZXNdIG9mIG5hbWVkKSB7XG4gICAgICAgICAgICBpZiAobm9kZXMuc2l6ZSA8PSAxKSBjb250aW51ZTtcblxuICAgICAgICAgICAgaWYgKGlzVHlwZXNjcmlwdEZ1bmN0aW9uT3ZlcmxvYWRzKG5vZGVzKSkgY29udGludWU7XG5cbiAgICAgICAgICAgIGZvciAoY29uc3Qgbm9kZSBvZiBub2Rlcykge1xuICAgICAgICAgICAgICBpZiAobmFtZSA9PT0gJ2RlZmF1bHQnKSB7XG4gICAgICAgICAgICAgICAgY29udGV4dC5yZXBvcnQobm9kZSwgJ011bHRpcGxlIGRlZmF1bHQgZXhwb3J0cy4nKTtcbiAgICAgICAgICAgICAgfSBlbHNlIHtcbiAgICAgICAgICAgICAgICBjb250ZXh0LnJlcG9ydChcbiAgICAgICAgICAgICAgICAgIG5vZGUsXG4gICAgICAgICAgICAgICAgICBgTXVsdGlwbGUgZXhwb3J0cyBvZiBuYW1lICcke25hbWUucmVwbGFjZSh0c1R5cGVQcmVmaXgsICcnKX0nLmBcbiAgICAgICAgICAgICAgICApO1xuICAgICAgICAgICAgICB9XG4gICAgICAgICAgICB9XG4gICAgICAgICAgfVxuICAgICAgICB9XG4gICAgICB9LFxuICAgIH07XG4gIH0sXG59O1xuIl19