|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {RuleFailure, WalkContext} from 'tslint/lib'; |
| 10 | +import {AbstractRule} from 'tslint/lib/rules'; |
| 11 | +import ts from 'typescript'; |
| 12 | + |
| 13 | +// TODO(devversion): move this rule into dev-infra. |
| 14 | + |
| 15 | +const noNamedExportsError = |
| 16 | + 'Named import is not allowed. The module does not expose named exports when ' + |
| 17 | + 'imported in an ES module. Use a default import instead.'; |
| 18 | + |
| 19 | +const noDefaultExportError = |
| 20 | + 'Default import is not allowed. The module does not expose a default export at ' + |
| 21 | + 'runtime. Use a named import instead.'; |
| 22 | + |
| 23 | +interface RuleOptions { |
| 24 | + /** |
| 25 | + * List of modules without any named exports that NodeJS can statically detect when the |
| 26 | + * CommonJS module is imported from ESM. Node only exposes named exports which are |
| 27 | + * statically discoverable: https://nodejs.org/api/esm.html#esm_import_statements. |
| 28 | + */ |
| 29 | + noNamedExports?: string[]; |
| 30 | + /** |
| 31 | + * List of modules which appear to have named exports in the typings but do |
| 32 | + * not have any at runtime due to NodeJS not being able to discover these |
| 33 | + * through static analysis: https://nodejs.org/api/esm.html#esm_import_statements. |
| 34 | + * */ |
| 35 | + noDefaultExport?: string[]; |
| 36 | + /** |
| 37 | + * List of modules which are always incompatible. The rule allows for a custom |
| 38 | + * message to be provided when it discovers an import to such a module. |
| 39 | + */ |
| 40 | + incompatibleModules?: Record<string, string>; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Rule that blocks named imports from being used for certain configured module |
| 45 | + * specifiers. This is helpful for enforcing an ESM-compatible interop with CommonJS |
| 46 | + * modules which do not expose named bindings at runtime. |
| 47 | + * |
| 48 | + * For example, consider the `typescript` module. It does not statically expose named |
| 49 | + * exports even though the type definition suggests it. An import like the following |
| 50 | + * will break at runtime when the `typescript` CommonJS module is imported inside an ESM. |
| 51 | + * |
| 52 | + * ``` |
| 53 | + * import * as ts from 'typescript'; |
| 54 | + * console.log(ts.SyntaxKind.CallExpression); // `SyntaxKind is undefined`. |
| 55 | + * ``` |
| 56 | + * |
| 57 | + * More details here: https://nodejs.org/api/esm.html#esm_import_statements. |
| 58 | + */ |
| 59 | +export class Rule extends AbstractRule { |
| 60 | + override apply(sourceFile: ts.SourceFile): RuleFailure[] { |
| 61 | + const options = this.getOptions().ruleArguments[0]; |
| 62 | + return this.applyWithFunction(sourceFile, (ctx) => visitNode(sourceFile, ctx, options)); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +function visitNode(node: ts.Node, ctx: WalkContext, options: RuleOptions) { |
| 67 | + if (options.incompatibleModules && ts.isImportDeclaration(node)) { |
| 68 | + const specifier = node.moduleSpecifier as ts.StringLiteral; |
| 69 | + const failureMsg = options.incompatibleModules[specifier.text]; |
| 70 | + |
| 71 | + if (failureMsg !== undefined) { |
| 72 | + ctx.addFailureAtNode(node, failureMsg); |
| 73 | + return; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (options.noNamedExports && isNamedImportToDisallowedModule(node, options.noNamedExports)) { |
| 78 | + ctx.addFailureAtNode(node, noNamedExportsError); |
| 79 | + } |
| 80 | + |
| 81 | + if (options.noDefaultExport && isDefaultImportToDisallowedModule(node, options.noDefaultExport)) { |
| 82 | + ctx.addFailureAtNode(node, noDefaultExportError); |
| 83 | + } |
| 84 | + |
| 85 | + ts.forEachChild(node, (node) => visitNode(node, ctx, options)); |
| 86 | +} |
| 87 | + |
| 88 | +function isNamedImportToDisallowedModule(node: ts.Node, disallowed: string[]): boolean { |
| 89 | + if (!ts.isImportDeclaration(node) || node.importClause === undefined) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + const specifier = node.moduleSpecifier as ts.StringLiteral; |
| 93 | + return !!node.importClause.namedBindings && disallowed.includes(specifier.text); |
| 94 | +} |
| 95 | + |
| 96 | +function isDefaultImportToDisallowedModule(node: ts.Node, disallowed: string[]) { |
| 97 | + if (!ts.isImportDeclaration(node) || node.importClause === undefined) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + const specifier = node.moduleSpecifier as ts.StringLiteral; |
| 101 | + |
| 102 | + return node.importClause.name !== undefined && disallowed.includes(specifier.text); |
| 103 | +} |
0 commit comments