no-restricted-imports.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /**
  2. * @fileoverview Restrict usage of specified node imports.
  3. * @author Guy Ellis
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. const ignore = require("ignore");
  14. const arrayOfStringsOrObjects = {
  15. type: "array",
  16. items: {
  17. anyOf: [
  18. { type: "string" },
  19. {
  20. type: "object",
  21. properties: {
  22. name: { type: "string" },
  23. message: {
  24. type: "string",
  25. minLength: 1,
  26. },
  27. importNames: {
  28. type: "array",
  29. items: {
  30. type: "string",
  31. },
  32. },
  33. allowImportNames: {
  34. type: "array",
  35. items: {
  36. type: "string",
  37. },
  38. },
  39. allowTypeImports: {
  40. type: "boolean",
  41. description:
  42. "Whether to allow type-only imports for a path.",
  43. },
  44. },
  45. additionalProperties: false,
  46. required: ["name"],
  47. not: { required: ["importNames", "allowImportNames"] },
  48. },
  49. ],
  50. },
  51. uniqueItems: true,
  52. };
  53. const arrayOfStringsOrObjectPatterns = {
  54. anyOf: [
  55. {
  56. type: "array",
  57. items: {
  58. type: "string",
  59. },
  60. uniqueItems: true,
  61. },
  62. {
  63. type: "array",
  64. items: {
  65. type: "object",
  66. properties: {
  67. importNames: {
  68. type: "array",
  69. items: {
  70. type: "string",
  71. },
  72. minItems: 1,
  73. uniqueItems: true,
  74. },
  75. allowImportNames: {
  76. type: "array",
  77. items: {
  78. type: "string",
  79. },
  80. minItems: 1,
  81. uniqueItems: true,
  82. },
  83. group: {
  84. type: "array",
  85. items: {
  86. type: "string",
  87. },
  88. minItems: 1,
  89. uniqueItems: true,
  90. },
  91. regex: {
  92. type: "string",
  93. },
  94. importNamePattern: {
  95. type: "string",
  96. },
  97. allowImportNamePattern: {
  98. type: "string",
  99. },
  100. message: {
  101. type: "string",
  102. minLength: 1,
  103. },
  104. caseSensitive: {
  105. type: "boolean",
  106. },
  107. allowTypeImports: {
  108. type: "boolean",
  109. description:
  110. "Whether to allow type-only imports for a pattern.",
  111. },
  112. },
  113. additionalProperties: false,
  114. not: {
  115. anyOf: [
  116. { required: ["importNames", "allowImportNames"] },
  117. {
  118. required: [
  119. "importNamePattern",
  120. "allowImportNamePattern",
  121. ],
  122. },
  123. { required: ["importNames", "allowImportNamePattern"] },
  124. { required: ["importNamePattern", "allowImportNames"] },
  125. {
  126. required: [
  127. "allowImportNames",
  128. "allowImportNamePattern",
  129. ],
  130. },
  131. ],
  132. },
  133. oneOf: [{ required: ["group"] }, { required: ["regex"] }],
  134. },
  135. uniqueItems: true,
  136. },
  137. ],
  138. };
  139. /** @type {import('../types').Rule.RuleModule} */
  140. module.exports = {
  141. meta: {
  142. type: "suggestion",
  143. dialects: ["typescript", "javascript"],
  144. language: "javascript",
  145. docs: {
  146. description: "Disallow specified modules when loaded by `import`",
  147. recommended: false,
  148. url: "https://eslint.org/docs/latest/rules/no-restricted-imports",
  149. },
  150. messages: {
  151. path: "'{{importSource}}' import is restricted from being used.",
  152. pathWithCustomMessage:
  153. // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
  154. "'{{importSource}}' import is restricted from being used. {{customMessage}}",
  155. patterns:
  156. "'{{importSource}}' import is restricted from being used by a pattern.",
  157. patternWithCustomMessage:
  158. // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
  159. "'{{importSource}}' import is restricted from being used by a pattern. {{customMessage}}",
  160. patternAndImportName:
  161. "'{{importName}}' import from '{{importSource}}' is restricted from being used by a pattern.",
  162. patternAndImportNameWithCustomMessage:
  163. // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
  164. "'{{importName}}' import from '{{importSource}}' is restricted from being used by a pattern. {{customMessage}}",
  165. patternAndEverything:
  166. "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted from being used by a pattern.",
  167. patternAndEverythingWithRegexImportName:
  168. "* import is invalid because import name matching '{{importNames}}' pattern from '{{importSource}}' is restricted from being used.",
  169. patternAndEverythingWithCustomMessage:
  170. // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
  171. "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted from being used by a pattern. {{customMessage}}",
  172. patternAndEverythingWithRegexImportNameAndCustomMessage:
  173. // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
  174. "* import is invalid because import name matching '{{importNames}}' pattern from '{{importSource}}' is restricted from being used. {{customMessage}}",
  175. everything:
  176. "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted.",
  177. everythingWithCustomMessage:
  178. // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
  179. "* import is invalid because '{{importNames}}' from '{{importSource}}' is restricted. {{customMessage}}",
  180. importName:
  181. "'{{importName}}' import from '{{importSource}}' is restricted.",
  182. importNameWithCustomMessage:
  183. // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
  184. "'{{importName}}' import from '{{importSource}}' is restricted. {{customMessage}}",
  185. allowedImportName:
  186. "'{{importName}}' import from '{{importSource}}' is restricted because only '{{allowedImportNames}}' import(s) is/are allowed.",
  187. allowedImportNameWithCustomMessage:
  188. // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
  189. "'{{importName}}' import from '{{importSource}}' is restricted because only '{{allowedImportNames}}' import(s) is/are allowed. {{customMessage}}",
  190. everythingWithAllowImportNames:
  191. "* import is invalid because only '{{allowedImportNames}}' from '{{importSource}}' is/are allowed.",
  192. everythingWithAllowImportNamesAndCustomMessage:
  193. // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
  194. "* import is invalid because only '{{allowedImportNames}}' from '{{importSource}}' is/are allowed. {{customMessage}}",
  195. allowedImportNamePattern:
  196. "'{{importName}}' import from '{{importSource}}' is restricted because only imports that match the pattern '{{allowedImportNamePattern}}' are allowed from '{{importSource}}'.",
  197. allowedImportNamePatternWithCustomMessage:
  198. // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
  199. "'{{importName}}' import from '{{importSource}}' is restricted because only imports that match the pattern '{{allowedImportNamePattern}}' are allowed from '{{importSource}}'. {{customMessage}}",
  200. everythingWithAllowedImportNamePattern:
  201. "* import is invalid because only imports that match the pattern '{{allowedImportNamePattern}}' from '{{importSource}}' are allowed.",
  202. everythingWithAllowedImportNamePatternWithCustomMessage:
  203. // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
  204. "* import is invalid because only imports that match the pattern '{{allowedImportNamePattern}}' from '{{importSource}}' are allowed. {{customMessage}}",
  205. },
  206. schema: {
  207. anyOf: [
  208. arrayOfStringsOrObjects,
  209. {
  210. type: "array",
  211. items: [
  212. {
  213. type: "object",
  214. properties: {
  215. paths: arrayOfStringsOrObjects,
  216. patterns: arrayOfStringsOrObjectPatterns,
  217. },
  218. additionalProperties: false,
  219. },
  220. ],
  221. additionalItems: false,
  222. },
  223. ],
  224. },
  225. },
  226. create(context) {
  227. const sourceCode = context.sourceCode;
  228. const options = Array.isArray(context.options) ? context.options : [];
  229. const isPathAndPatternsObject =
  230. typeof options[0] === "object" &&
  231. (Object.hasOwn(options[0], "paths") ||
  232. Object.hasOwn(options[0], "patterns"));
  233. const restrictedPaths =
  234. (isPathAndPatternsObject ? options[0].paths : context.options) ||
  235. [];
  236. const groupedRestrictedPaths = restrictedPaths.reduce(
  237. (memo, importSource) => {
  238. const path =
  239. typeof importSource === "string"
  240. ? importSource
  241. : importSource.name;
  242. if (!memo[path]) {
  243. memo[path] = [];
  244. }
  245. if (typeof importSource === "string") {
  246. memo[path].push({});
  247. } else {
  248. memo[path].push({
  249. message: importSource.message,
  250. importNames: importSource.importNames,
  251. allowImportNames: importSource.allowImportNames,
  252. allowTypeImports: importSource.allowTypeImports,
  253. });
  254. }
  255. return memo;
  256. },
  257. Object.create(null),
  258. );
  259. // Handle patterns too, either as strings or groups
  260. let restrictedPatterns =
  261. (isPathAndPatternsObject ? options[0].patterns : []) || [];
  262. // standardize to array of objects if we have an array of strings
  263. if (
  264. restrictedPatterns.length > 0 &&
  265. typeof restrictedPatterns[0] === "string"
  266. ) {
  267. restrictedPatterns = [{ group: restrictedPatterns }];
  268. }
  269. // relative paths are supported for this rule
  270. const restrictedPatternGroups = restrictedPatterns.map(
  271. ({
  272. group,
  273. regex,
  274. message,
  275. caseSensitive,
  276. importNames,
  277. importNamePattern,
  278. allowImportNames,
  279. allowImportNamePattern,
  280. allowTypeImports,
  281. }) => ({
  282. ...(group
  283. ? {
  284. matcher: ignore({
  285. allowRelativePaths: true,
  286. ignorecase: !caseSensitive,
  287. }).add(group),
  288. }
  289. : {}),
  290. ...(typeof regex === "string"
  291. ? {
  292. regexMatcher: new RegExp(
  293. regex,
  294. caseSensitive ? "u" : "iu",
  295. ),
  296. }
  297. : {}),
  298. customMessage: message,
  299. importNames,
  300. importNamePattern,
  301. allowImportNames,
  302. allowImportNamePattern,
  303. allowTypeImports,
  304. }),
  305. );
  306. // if no imports are restricted we don't need to check
  307. if (
  308. Object.keys(restrictedPaths).length === 0 &&
  309. restrictedPatternGroups.length === 0
  310. ) {
  311. return {};
  312. }
  313. /**
  314. * Check if the node is a type-only import
  315. * @param {ASTNode} node The node to check
  316. * @returns {boolean} Whether the node is a type-only import
  317. */
  318. function isTypeOnlyImport(node) {
  319. return (
  320. node.importKind === "type" ||
  321. (node.specifiers?.length > 0 &&
  322. node.specifiers.every(
  323. specifier => specifier.importKind === "type",
  324. ))
  325. );
  326. }
  327. /**
  328. * Check if a specifier is type-only
  329. * @param {ASTNode} specifier The specifier to check
  330. * @returns {boolean} Whether the specifier is type-only
  331. */
  332. function isTypeOnlySpecifier(specifier) {
  333. return (
  334. specifier.importKind === "type" ||
  335. specifier.exportKind === "type"
  336. );
  337. }
  338. /**
  339. * Check if the node is a type-only export
  340. * @param {ASTNode} node The node to check
  341. * @returns {boolean} Whether the node is a type-only export
  342. */
  343. function isTypeOnlyExport(node) {
  344. return (
  345. node.exportKind === "type" ||
  346. (node.specifiers?.length > 0 &&
  347. node.specifiers.every(
  348. specifier => specifier.exportKind === "type",
  349. ))
  350. );
  351. }
  352. /**
  353. * Report a restricted path.
  354. * @param {string} importSource path of the import
  355. * @param {Map<string,Object[]>} importNames Map of import names that are being imported
  356. * @param {node} node representing the restricted path reference
  357. * @returns {void}
  358. * @private
  359. */
  360. function checkRestrictedPathAndReport(importSource, importNames, node) {
  361. if (!Object.hasOwn(groupedRestrictedPaths, importSource)) {
  362. return;
  363. }
  364. groupedRestrictedPaths[importSource].forEach(
  365. restrictedPathEntry => {
  366. const customMessage = restrictedPathEntry.message;
  367. const restrictedImportNames =
  368. restrictedPathEntry.importNames;
  369. const allowedImportNames =
  370. restrictedPathEntry.allowImportNames;
  371. const allowTypeImports =
  372. restrictedPathEntry.allowTypeImports;
  373. // Skip if this is a type-only import and it's allowed for this specific entry
  374. if (
  375. allowTypeImports &&
  376. (node.type === "ImportDeclaration" ||
  377. node.type === "TSImportEqualsDeclaration") &&
  378. isTypeOnlyImport(node)
  379. ) {
  380. return;
  381. }
  382. // Skip if this is a type-only export and it's allowed for this specific entry
  383. if (
  384. allowTypeImports &&
  385. (node.type === "ExportNamedDeclaration" ||
  386. node.type === "ExportAllDeclaration") &&
  387. isTypeOnlyExport(node)
  388. ) {
  389. return;
  390. }
  391. if (!restrictedImportNames && !allowedImportNames) {
  392. context.report({
  393. node,
  394. messageId: customMessage
  395. ? "pathWithCustomMessage"
  396. : "path",
  397. data: {
  398. importSource,
  399. customMessage,
  400. },
  401. });
  402. return;
  403. }
  404. importNames.forEach((specifiers, importName) => {
  405. if (importName === "*") {
  406. const [specifier] = specifiers;
  407. if (restrictedImportNames) {
  408. context.report({
  409. node,
  410. messageId: customMessage
  411. ? "everythingWithCustomMessage"
  412. : "everything",
  413. loc: specifier.loc,
  414. data: {
  415. importSource,
  416. importNames: restrictedImportNames,
  417. customMessage,
  418. },
  419. });
  420. } else if (allowedImportNames) {
  421. context.report({
  422. node,
  423. messageId: customMessage
  424. ? "everythingWithAllowImportNamesAndCustomMessage"
  425. : "everythingWithAllowImportNames",
  426. loc: specifier.loc,
  427. data: {
  428. importSource,
  429. allowedImportNames,
  430. customMessage,
  431. },
  432. });
  433. }
  434. return;
  435. }
  436. if (
  437. restrictedImportNames &&
  438. restrictedImportNames.includes(importName)
  439. ) {
  440. specifiers.forEach(specifier => {
  441. // Skip if this is a type-only import specifier and type imports are allowed
  442. if (
  443. allowTypeImports &&
  444. isTypeOnlySpecifier(specifier.specifier)
  445. ) {
  446. return;
  447. }
  448. context.report({
  449. node,
  450. messageId: customMessage
  451. ? "importNameWithCustomMessage"
  452. : "importName",
  453. loc: specifier.loc,
  454. data: {
  455. importSource,
  456. customMessage,
  457. importName,
  458. },
  459. });
  460. });
  461. }
  462. if (
  463. allowedImportNames &&
  464. !allowedImportNames.includes(importName)
  465. ) {
  466. specifiers.forEach(specifier => {
  467. // Skip if this is a type-only import specifier and type imports are allowed
  468. if (
  469. allowTypeImports &&
  470. isTypeOnlySpecifier(specifier.specifier)
  471. ) {
  472. return;
  473. }
  474. context.report({
  475. node,
  476. loc: specifier.loc,
  477. messageId: customMessage
  478. ? "allowedImportNameWithCustomMessage"
  479. : "allowedImportName",
  480. data: {
  481. importSource,
  482. customMessage,
  483. importName,
  484. allowedImportNames,
  485. },
  486. });
  487. });
  488. }
  489. });
  490. },
  491. );
  492. }
  493. /**
  494. * Report a restricted path specifically for patterns.
  495. * @param {node} node representing the restricted path reference
  496. * @param {Object} group contains an Ignore instance for paths, the customMessage to show on failure,
  497. * and any restricted import names that have been specified in the config
  498. * @param {Map<string,Object[]>} importNames Map of import names that are being imported
  499. * @param {string} importSource the import source string
  500. * @returns {void}
  501. * @private
  502. */
  503. function reportPathForPatterns(node, group, importNames, importSource) {
  504. // Skip if this is a type-only import and it's allowed
  505. if (
  506. group.allowTypeImports &&
  507. (node.type === "ImportDeclaration" ||
  508. node.type === "TSImportEqualsDeclaration") &&
  509. isTypeOnlyImport(node)
  510. ) {
  511. return;
  512. }
  513. // Skip if this is a type-only export and it's allowed
  514. if (
  515. group.allowTypeImports &&
  516. (node.type === "ExportNamedDeclaration" ||
  517. node.type === "ExportAllDeclaration") &&
  518. isTypeOnlyExport(node)
  519. ) {
  520. return;
  521. }
  522. const customMessage = group.customMessage;
  523. const restrictedImportNames = group.importNames;
  524. const restrictedImportNamePattern = group.importNamePattern
  525. ? new RegExp(group.importNamePattern, "u")
  526. : null;
  527. const allowedImportNames = group.allowImportNames;
  528. const allowedImportNamePattern = group.allowImportNamePattern
  529. ? new RegExp(group.allowImportNamePattern, "u")
  530. : null;
  531. /**
  532. * If we are not restricting to any specific import names and just the pattern itself,
  533. * report the error and move on
  534. */
  535. if (
  536. !restrictedImportNames &&
  537. !allowedImportNames &&
  538. !restrictedImportNamePattern &&
  539. !allowedImportNamePattern
  540. ) {
  541. context.report({
  542. node,
  543. messageId: customMessage
  544. ? "patternWithCustomMessage"
  545. : "patterns",
  546. data: {
  547. importSource,
  548. customMessage,
  549. },
  550. });
  551. return;
  552. }
  553. importNames.forEach((specifiers, importName) => {
  554. if (importName === "*") {
  555. const [specifier] = specifiers;
  556. if (restrictedImportNames) {
  557. context.report({
  558. node,
  559. messageId: customMessage
  560. ? "patternAndEverythingWithCustomMessage"
  561. : "patternAndEverything",
  562. loc: specifier.loc,
  563. data: {
  564. importSource,
  565. importNames: restrictedImportNames,
  566. customMessage,
  567. },
  568. });
  569. } else if (allowedImportNames) {
  570. context.report({
  571. node,
  572. messageId: customMessage
  573. ? "everythingWithAllowImportNamesAndCustomMessage"
  574. : "everythingWithAllowImportNames",
  575. loc: specifier.loc,
  576. data: {
  577. importSource,
  578. allowedImportNames,
  579. customMessage,
  580. },
  581. });
  582. } else if (allowedImportNamePattern) {
  583. context.report({
  584. node,
  585. messageId: customMessage
  586. ? "everythingWithAllowedImportNamePatternWithCustomMessage"
  587. : "everythingWithAllowedImportNamePattern",
  588. loc: specifier.loc,
  589. data: {
  590. importSource,
  591. allowedImportNamePattern,
  592. customMessage,
  593. },
  594. });
  595. } else {
  596. context.report({
  597. node,
  598. messageId: customMessage
  599. ? "patternAndEverythingWithRegexImportNameAndCustomMessage"
  600. : "patternAndEverythingWithRegexImportName",
  601. loc: specifier.loc,
  602. data: {
  603. importSource,
  604. importNames: restrictedImportNamePattern,
  605. customMessage,
  606. },
  607. });
  608. }
  609. return;
  610. }
  611. if (
  612. (restrictedImportNames &&
  613. restrictedImportNames.includes(importName)) ||
  614. (restrictedImportNamePattern &&
  615. restrictedImportNamePattern.test(importName))
  616. ) {
  617. specifiers.forEach(specifier => {
  618. // Skip if this is a type-only import specifier and type imports are allowed
  619. if (
  620. group.allowTypeImports &&
  621. isTypeOnlySpecifier(specifier.specifier)
  622. ) {
  623. return;
  624. }
  625. context.report({
  626. node,
  627. messageId: customMessage
  628. ? "patternAndImportNameWithCustomMessage"
  629. : "patternAndImportName",
  630. loc: specifier.loc,
  631. data: {
  632. importSource,
  633. customMessage,
  634. importName,
  635. },
  636. });
  637. });
  638. }
  639. if (
  640. allowedImportNames &&
  641. !allowedImportNames.includes(importName)
  642. ) {
  643. specifiers.forEach(specifier => {
  644. // Skip if this is a type-only import specifier and type imports are allowed
  645. if (
  646. group.allowTypeImports &&
  647. isTypeOnlySpecifier(specifier.specifier)
  648. ) {
  649. return;
  650. }
  651. context.report({
  652. node,
  653. messageId: customMessage
  654. ? "allowedImportNameWithCustomMessage"
  655. : "allowedImportName",
  656. loc: specifier.loc,
  657. data: {
  658. importSource,
  659. customMessage,
  660. importName,
  661. allowedImportNames,
  662. },
  663. });
  664. });
  665. } else if (
  666. allowedImportNamePattern &&
  667. !allowedImportNamePattern.test(importName)
  668. ) {
  669. specifiers.forEach(specifier => {
  670. // Skip if this is a type-only import specifier and type imports are allowed
  671. if (
  672. group.allowTypeImports &&
  673. isTypeOnlySpecifier(specifier.specifier)
  674. ) {
  675. return;
  676. }
  677. context.report({
  678. node,
  679. messageId: customMessage
  680. ? "allowedImportNamePatternWithCustomMessage"
  681. : "allowedImportNamePattern",
  682. loc: specifier.loc,
  683. data: {
  684. importSource,
  685. customMessage,
  686. importName,
  687. allowedImportNamePattern,
  688. },
  689. });
  690. });
  691. }
  692. });
  693. }
  694. /**
  695. * Check if the given importSource is restricted by a pattern.
  696. * @param {string} importSource path of the import
  697. * @param {Object} group contains a Ignore instance for paths, and the customMessage to show if it fails
  698. * @returns {boolean} whether the variable is a restricted pattern or not
  699. * @private
  700. */
  701. function isRestrictedPattern(importSource, group) {
  702. return group.regexMatcher
  703. ? group.regexMatcher.test(importSource)
  704. : group.matcher.ignores(importSource);
  705. }
  706. /**
  707. * Checks a node to see if any problems should be reported.
  708. * @param {ASTNode} node The node to check.
  709. * @returns {void}
  710. * @private
  711. */
  712. function checkNode(node) {
  713. const importSource = node.source.value.trim();
  714. const importNames = new Map();
  715. if (node.type === "ExportAllDeclaration") {
  716. const starToken = sourceCode.getFirstToken(node, 1);
  717. importNames.set("*", [{ loc: starToken.loc }]);
  718. } else if (node.specifiers) {
  719. for (const specifier of node.specifiers) {
  720. let name;
  721. const specifierData = { loc: specifier.loc, specifier };
  722. if (specifier.type === "ImportDefaultSpecifier") {
  723. name = "default";
  724. } else if (specifier.type === "ImportNamespaceSpecifier") {
  725. name = "*";
  726. } else if (specifier.imported) {
  727. name = astUtils.getModuleExportName(specifier.imported);
  728. } else if (specifier.local) {
  729. name = astUtils.getModuleExportName(specifier.local);
  730. }
  731. if (typeof name === "string") {
  732. if (importNames.has(name)) {
  733. importNames.get(name).push(specifierData);
  734. } else {
  735. importNames.set(name, [specifierData]);
  736. }
  737. }
  738. }
  739. }
  740. checkRestrictedPathAndReport(importSource, importNames, node);
  741. restrictedPatternGroups.forEach(group => {
  742. if (isRestrictedPattern(importSource, group)) {
  743. reportPathForPatterns(
  744. node,
  745. group,
  746. importNames,
  747. importSource,
  748. );
  749. }
  750. });
  751. }
  752. return {
  753. ImportDeclaration: checkNode,
  754. ExportNamedDeclaration(node) {
  755. if (node.source) {
  756. checkNode(node);
  757. }
  758. },
  759. ExportAllDeclaration: checkNode,
  760. // Add support for TypeScript import equals declarations
  761. TSImportEqualsDeclaration(node) {
  762. if (node.moduleReference.type === "TSExternalModuleReference") {
  763. const importSource = node.moduleReference.expression.value;
  764. const importNames = new Map();
  765. // Use existing logic with the actual node
  766. checkRestrictedPathAndReport(
  767. importSource,
  768. importNames,
  769. node,
  770. );
  771. restrictedPatternGroups.forEach(group => {
  772. if (isRestrictedPattern(importSource, group)) {
  773. reportPathForPatterns(
  774. node,
  775. group,
  776. importNames,
  777. importSource,
  778. );
  779. }
  780. });
  781. }
  782. },
  783. };
  784. },
  785. };