tsc-ts 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -0
- package/dist/api/compiler/ast-utilities.d.ts +82 -0
- package/dist/api/compiler/ast-utilities.js +462 -0
- package/dist/api/compiler/ast.d.ts +2350 -0
- package/dist/api/compiler/ast.js +11206 -0
- package/dist/api/compiler/astnav.d.ts +37 -0
- package/dist/api/compiler/astnav.js +767 -0
- package/dist/api/compiler/binder-utilities.d.ts +182 -0
- package/dist/api/compiler/binder-utilities.js +1240 -0
- package/dist/api/compiler/binder.d.ts +33 -0
- package/dist/api/compiler/binder.js +2358 -0
- package/dist/api/compiler/character-codes.d.ts +161 -0
- package/dist/api/compiler/character-codes.js +160 -0
- package/dist/api/compiler/checker-calls.d.ts +46 -0
- package/dist/api/compiler/checker-calls.js +1640 -0
- package/dist/api/compiler/checker-classes.d.ts +30 -0
- package/dist/api/compiler/checker-classes.js +582 -0
- package/dist/api/compiler/checker-contextual.d.ts +77 -0
- package/dist/api/compiler/checker-contextual.js +1136 -0
- package/dist/api/compiler/checker-declaration-order.d.ts +12 -0
- package/dist/api/compiler/checker-declaration-order.js +356 -0
- package/dist/api/compiler/checker-declarations.d.ts +33 -0
- package/dist/api/compiler/checker-declarations.js +888 -0
- package/dist/api/compiler/checker-declared-types.d.ts +48 -0
- package/dist/api/compiler/checker-declared-types.js +787 -0
- package/dist/api/compiler/checker-decorators.d.ts +26 -0
- package/dist/api/compiler/checker-decorators.js +562 -0
- package/dist/api/compiler/checker-deprecation.d.ts +33 -0
- package/dist/api/compiler/checker-deprecation.js +315 -0
- package/dist/api/compiler/checker-destructuring.d.ts +34 -0
- package/dist/api/compiler/checker-destructuring.js +584 -0
- package/dist/api/compiler/checker-elaboration.d.ts +15 -0
- package/dist/api/compiler/checker-elaboration.js +290 -0
- package/dist/api/compiler/checker-emit-helpers.d.ts +58 -0
- package/dist/api/compiler/checker-emit-helpers.js +154 -0
- package/dist/api/compiler/checker-emit-resolver.d.ts +75 -0
- package/dist/api/compiler/checker-emit-resolver.js +579 -0
- package/dist/api/compiler/checker-evaluator.d.ts +27 -0
- package/dist/api/compiler/checker-evaluator.js +188 -0
- package/dist/api/compiler/checker-expressions.d.ts +67 -0
- package/dist/api/compiler/checker-expressions.js +1351 -0
- package/dist/api/compiler/checker-features.d.ts +6 -0
- package/dist/api/compiler/checker-features.js +354 -0
- package/dist/api/compiler/checker-flow.d.ts +86 -0
- package/dist/api/compiler/checker-flow.js +169 -0
- package/dist/api/compiler/checker-functions.d.ts +43 -0
- package/dist/api/compiler/checker-functions.js +1002 -0
- package/dist/api/compiler/checker-generated-code.d.ts +25 -0
- package/dist/api/compiler/checker-generated-code.js +168 -0
- package/dist/api/compiler/checker-globals.d.ts +14 -0
- package/dist/api/compiler/checker-globals.js +37 -0
- package/dist/api/compiler/checker-grammar.d.ts +35 -0
- package/dist/api/compiler/checker-grammar.js +2573 -0
- package/dist/api/compiler/checker-heritage.d.ts +28 -0
- package/dist/api/compiler/checker-heritage.js +900 -0
- package/dist/api/compiler/checker-inference.d.ts +179 -0
- package/dist/api/compiler/checker-inference.js +2105 -0
- package/dist/api/compiler/checker-iteration.d.ts +115 -0
- package/dist/api/compiler/checker-iteration.js +857 -0
- package/dist/api/compiler/checker-javascript.d.ts +42 -0
- package/dist/api/compiler/checker-javascript.js +441 -0
- package/dist/api/compiler/checker-jsdoc.d.ts +30 -0
- package/dist/api/compiler/checker-jsdoc.js +320 -0
- package/dist/api/compiler/checker-jsx.d.ts +94 -0
- package/dist/api/compiler/checker-jsx.js +1433 -0
- package/dist/api/compiler/checker-literals.d.ts +6 -0
- package/dist/api/compiler/checker-literals.js +43 -0
- package/dist/api/compiler/checker-members.d.ts +198 -0
- package/dist/api/compiler/checker-members.js +2126 -0
- package/dist/api/compiler/checker-modules.d.ts +85 -0
- package/dist/api/compiler/checker-modules.js +2141 -0
- package/dist/api/compiler/checker-narrowing.d.ts +120 -0
- package/dist/api/compiler/checker-narrowing.js +2797 -0
- package/dist/api/compiler/checker-object-literals.d.ts +14 -0
- package/dist/api/compiler/checker-object-literals.js +479 -0
- package/dist/api/compiler/checker-operators.d.ts +33 -0
- package/dist/api/compiler/checker-operators.js +1040 -0
- package/dist/api/compiler/checker-overloads.d.ts +7 -0
- package/dist/api/compiler/checker-overloads.js +270 -0
- package/dist/api/compiler/checker-property-access.d.ts +34 -0
- package/dist/api/compiler/checker-property-access.js +672 -0
- package/dist/api/compiler/checker-services.d.ts +49 -0
- package/dist/api/compiler/checker-services.js +995 -0
- package/dist/api/compiler/checker-signatures.d.ts +55 -0
- package/dist/api/compiler/checker-signatures.js +788 -0
- package/dist/api/compiler/checker-source-file.d.ts +27 -0
- package/dist/api/compiler/checker-source-file.js +619 -0
- package/dist/api/compiler/checker-statements.d.ts +18 -0
- package/dist/api/compiler/checker-statements.js +226 -0
- package/dist/api/compiler/checker-super.d.ts +13 -0
- package/dist/api/compiler/checker-super.js +241 -0
- package/dist/api/compiler/checker-symbol-types.d.ts +67 -0
- package/dist/api/compiler/checker-symbol-types.js +1885 -0
- package/dist/api/compiler/checker-symbols.d.ts +181 -0
- package/dist/api/compiler/checker-symbols.js +1782 -0
- package/dist/api/compiler/checker-type-factory.d.ts +142 -0
- package/dist/api/compiler/checker-type-factory.js +2501 -0
- package/dist/api/compiler/checker-type-nodes.d.ts +26 -0
- package/dist/api/compiler/checker-type-nodes.js +828 -0
- package/dist/api/compiler/checker-type-operators.d.ts +109 -0
- package/dist/api/compiler/checker-type-operators.js +1842 -0
- package/dist/api/compiler/checker-types.d.ts +1027 -0
- package/dist/api/compiler/checker-types.js +1697 -0
- package/dist/api/compiler/checker-unused.d.ts +19 -0
- package/dist/api/compiler/checker-unused.js +524 -0
- package/dist/api/compiler/checker-utilities.d.ts +33 -0
- package/dist/api/compiler/checker-utilities.js +141 -0
- package/dist/api/compiler/checker.d.ts +491 -0
- package/dist/api/compiler/checker.js +912 -0
- package/dist/api/compiler/command-line.d.ts +32 -0
- package/dist/api/compiler/command-line.js +383 -0
- package/dist/api/compiler/compile-program.d.ts +118 -0
- package/dist/api/compiler/compile-program.js +384 -0
- package/dist/api/compiler/diagnostic-locales.d.ts +22 -0
- package/dist/api/compiler/diagnostic-locales.js +64 -0
- package/dist/api/compiler/diagnostic-messages.d.ts +2166 -0
- package/dist/api/compiler/diagnostic-messages.js +184 -0
- package/dist/api/compiler/diagnostics.d.ts +167 -0
- package/dist/api/compiler/diagnostics.js +820 -0
- package/dist/api/compiler/emitter.d.ts +71 -0
- package/dist/api/compiler/emitter.js +327 -0
- package/dist/api/compiler/file-include.d.ts +117 -0
- package/dist/api/compiler/file-include.js +536 -0
- package/dist/api/compiler/file-loader.d.ts +261 -0
- package/dist/api/compiler/file-loader.js +1257 -0
- package/dist/api/compiler/file-system.d.ts +51 -0
- package/dist/api/compiler/file-system.js +132 -0
- package/dist/api/compiler/flow.d.ts +60 -0
- package/dist/api/compiler/flow.js +45 -0
- package/dist/api/compiler/instantiation.d.ts +74 -0
- package/dist/api/compiler/instantiation.js +799 -0
- package/dist/api/compiler/jsdoc.d.ts +75 -0
- package/dist/api/compiler/jsdoc.js +1229 -0
- package/dist/api/compiler/link-tables.d.ts +36 -0
- package/dist/api/compiler/link-tables.js +83 -0
- package/dist/api/compiler/locale-data.d.ts +24 -0
- package/dist/api/compiler/locale-data.js +183 -0
- package/dist/api/compiler/locale.d.ts +4 -0
- package/dist/api/compiler/locale.js +317 -0
- package/dist/api/compiler/locales/cs-CZ.d.ts +2 -0
- package/dist/api/compiler/locales/cs-CZ.js +3 -0
- package/dist/api/compiler/locales/de-DE.d.ts +2 -0
- package/dist/api/compiler/locales/de-DE.js +3 -0
- package/dist/api/compiler/locales/es-ES.d.ts +2 -0
- package/dist/api/compiler/locales/es-ES.js +3 -0
- package/dist/api/compiler/locales/fr-FR.d.ts +2 -0
- package/dist/api/compiler/locales/fr-FR.js +3 -0
- package/dist/api/compiler/locales/it-IT.d.ts +2 -0
- package/dist/api/compiler/locales/it-IT.js +3 -0
- package/dist/api/compiler/locales/ja-JP.d.ts +2 -0
- package/dist/api/compiler/locales/ja-JP.js +3 -0
- package/dist/api/compiler/locales/ko-KR.d.ts +2 -0
- package/dist/api/compiler/locales/ko-KR.js +3 -0
- package/dist/api/compiler/locales/pl-PL.d.ts +2 -0
- package/dist/api/compiler/locales/pl-PL.js +3 -0
- package/dist/api/compiler/locales/pt-BR.d.ts +2 -0
- package/dist/api/compiler/locales/pt-BR.js +3 -0
- package/dist/api/compiler/locales/ru-RU.d.ts +2 -0
- package/dist/api/compiler/locales/ru-RU.js +3 -0
- package/dist/api/compiler/locales/tr-TR.d.ts +2 -0
- package/dist/api/compiler/locales/tr-TR.js +3 -0
- package/dist/api/compiler/locales/zh-CN.d.ts +2 -0
- package/dist/api/compiler/locales/zh-CN.js +3 -0
- package/dist/api/compiler/locales/zh-TW.d.ts +2 -0
- package/dist/api/compiler/locales/zh-TW.js +3 -0
- package/dist/api/compiler/module-resolution-types.d.ts +83 -0
- package/dist/api/compiler/module-resolution-types.js +176 -0
- package/dist/api/compiler/module-resolution.d.ts +71 -0
- package/dist/api/compiler/module-resolution.js +1598 -0
- package/dist/api/compiler/module-specifiers.d.ts +64 -0
- package/dist/api/compiler/module-specifiers.js +1101 -0
- package/dist/api/compiler/name-resolver.d.ts +43 -0
- package/dist/api/compiler/name-resolver.js +509 -0
- package/dist/api/compiler/node-builder-names.d.ts +32 -0
- package/dist/api/compiler/node-builder-names.js +854 -0
- package/dist/api/compiler/node-builder-pseudo.d.ts +36 -0
- package/dist/api/compiler/node-builder-pseudo.js +744 -0
- package/dist/api/compiler/node-builder-reuse.d.ts +4 -0
- package/dist/api/compiler/node-builder-reuse.js +647 -0
- package/dist/api/compiler/node-builder.d.ts +270 -0
- package/dist/api/compiler/node-builder.js +2645 -0
- package/dist/api/compiler/option-declarations.d.ts +90 -0
- package/dist/api/compiler/option-declarations.js +1460 -0
- package/dist/api/compiler/options.d.ts +289 -0
- package/dist/api/compiler/options.js +1197 -0
- package/dist/api/compiler/output-paths.d.ts +42 -0
- package/dist/api/compiler/output-paths.js +195 -0
- package/dist/api/compiler/package-json.d.ts +175 -0
- package/dist/api/compiler/package-json.js +1015 -0
- package/dist/api/compiler/parallel-parse.d.ts +219 -0
- package/dist/api/compiler/parallel-parse.js +537 -0
- package/dist/api/compiler/parser.d.ts +587 -0
- package/dist/api/compiler/parser.js +6407 -0
- package/dist/api/compiler/path.d.ts +91 -0
- package/dist/api/compiler/path.js +705 -0
- package/dist/api/compiler/printer/async-helpers.d.ts +22 -0
- package/dist/api/compiler/printer/async-helpers.js +91 -0
- package/dist/api/compiler/printer/auto-generate.d.ts +13 -0
- package/dist/api/compiler/printer/auto-generate.js +20 -0
- package/dist/api/compiler/printer/decorator-helpers.d.ts +24 -0
- package/dist/api/compiler/printer/decorator-helpers.js +118 -0
- package/dist/api/compiler/printer/emit-context.d.ts +154 -0
- package/dist/api/compiler/printer/emit-context.js +576 -0
- package/dist/api/compiler/printer/emit-flags.d.ts +60 -0
- package/dist/api/compiler/printer/emit-flags.js +2 -0
- package/dist/api/compiler/printer/factory-base.d.ts +171 -0
- package/dist/api/compiler/printer/factory-base.js +1505 -0
- package/dist/api/compiler/printer/factory.d.ts +147 -0
- package/dist/api/compiler/printer/factory.js +542 -0
- package/dist/api/compiler/printer/helpers.d.ts +27 -0
- package/dist/api/compiler/printer/helpers.js +164 -0
- package/dist/api/compiler/printer/name-generator.d.ts +41 -0
- package/dist/api/compiler/printer/name-generator.js +283 -0
- package/dist/api/compiler/printer/printer.d.ts +411 -0
- package/dist/api/compiler/printer/printer.js +4517 -0
- package/dist/api/compiler/printer/text-writer.d.ts +46 -0
- package/dist/api/compiler/printer/text-writer.js +172 -0
- package/dist/api/compiler/printer/utilities.d.ts +82 -0
- package/dist/api/compiler/printer/utilities.js +817 -0
- package/dist/api/compiler/program-checker.d.ts +156 -0
- package/dist/api/compiler/program-checker.js +526 -0
- package/dist/api/compiler/program-utilities.d.ts +52 -0
- package/dist/api/compiler/program-utilities.js +365 -0
- package/dist/api/compiler/program.d.ts +180 -0
- package/dist/api/compiler/program.js +1011 -0
- package/dist/api/compiler/pseudo-bigint.d.ts +5 -0
- package/dist/api/compiler/pseudo-bigint.js +61 -0
- package/dist/api/compiler/regexp-checker.d.ts +38 -0
- package/dist/api/compiler/regexp-checker.js +942 -0
- package/dist/api/compiler/relations.d.ts +263 -0
- package/dist/api/compiler/relations.js +4002 -0
- package/dist/api/compiler/reparser.d.ts +48 -0
- package/dist/api/compiler/reparser.js +1323 -0
- package/dist/api/compiler/scanner.d.ts +256 -0
- package/dist/api/compiler/scanner.js +2853 -0
- package/dist/api/compiler/side-state.d.ts +16 -0
- package/dist/api/compiler/side-state.js +89 -0
- package/dist/api/compiler/sourcemap.d.ts +55 -0
- package/dist/api/compiler/sourcemap.js +340 -0
- package/dist/api/compiler/spelling.d.ts +14 -0
- package/dist/api/compiler/spelling.js +128 -0
- package/dist/api/compiler/symbol-accessibility.d.ts +57 -0
- package/dist/api/compiler/symbol-accessibility.js +894 -0
- package/dist/api/compiler/symbol.d.ts +240 -0
- package/dist/api/compiler/symbol.js +228 -0
- package/dist/api/compiler/symlinks.d.ts +32 -0
- package/dist/api/compiler/symlinks.js +114 -0
- package/dist/api/compiler/syntax-kind.d.ts +389 -0
- package/dist/api/compiler/syntax-kind.js +2 -0
- package/dist/api/compiler/timeline.d.ts +9 -0
- package/dist/api/compiler/timeline.js +54 -0
- package/dist/api/compiler/transformers/alias-references.d.ts +39 -0
- package/dist/api/compiler/transformers/alias-references.js +676 -0
- package/dist/api/compiler/transformers/async-utilities.d.ts +51 -0
- package/dist/api/compiler/transformers/async-utilities.js +260 -0
- package/dist/api/compiler/transformers/async.d.ts +53 -0
- package/dist/api/compiler/transformers/async.js +645 -0
- package/dist/api/compiler/transformers/class-fields.d.ts +206 -0
- package/dist/api/compiler/transformers/class-fields.js +2418 -0
- package/dist/api/compiler/transformers/class-utilities.d.ts +46 -0
- package/dist/api/compiler/transformers/class-utilities.js +261 -0
- package/dist/api/compiler/transformers/commonjs-module.d.ts +103 -0
- package/dist/api/compiler/transformers/commonjs-module.js +1180 -0
- package/dist/api/compiler/transformers/const-enum-inliner.d.ts +11 -0
- package/dist/api/compiler/transformers/const-enum-inliner.js +36 -0
- package/dist/api/compiler/transformers/declarations-diagnostics.d.ts +52 -0
- package/dist/api/compiler/transformers/declarations-diagnostics.js +538 -0
- package/dist/api/compiler/transformers/declarations-pseudo.d.ts +160 -0
- package/dist/api/compiler/transformers/declarations-pseudo.js +797 -0
- package/dist/api/compiler/transformers/declarations-resolver.d.ts +152 -0
- package/dist/api/compiler/transformers/declarations-resolver.js +35 -0
- package/dist/api/compiler/transformers/declarations-syntactic.d.ts +119 -0
- package/dist/api/compiler/transformers/declarations-syntactic.js +1582 -0
- package/dist/api/compiler/transformers/declarations-tracker.d.ts +46 -0
- package/dist/api/compiler/transformers/declarations-tracker.js +197 -0
- package/dist/api/compiler/transformers/declarations.d.ts +175 -0
- package/dist/api/compiler/transformers/declarations.js +2380 -0
- package/dist/api/compiler/transformers/decorator-metadata.d.ts +54 -0
- package/dist/api/compiler/transformers/decorator-metadata.js +554 -0
- package/dist/api/compiler/transformers/decorator-utilities.d.ts +38 -0
- package/dist/api/compiler/transformers/decorator-utilities.js +238 -0
- package/dist/api/compiler/transformers/destructuring.d.ts +25 -0
- package/dist/api/compiler/transformers/destructuring.js +626 -0
- package/dist/api/compiler/transformers/downlevel.d.ts +6 -0
- package/dist/api/compiler/transformers/downlevel.js +45 -0
- package/dist/api/compiler/transformers/emit-resolver.d.ts +61 -0
- package/dist/api/compiler/transformers/emit-resolver.js +197 -0
- package/dist/api/compiler/transformers/es-decorators.d.ts +118 -0
- package/dist/api/compiler/transformers/es-decorators.js +1901 -0
- package/dist/api/compiler/transformers/exponentiation.d.ts +11 -0
- package/dist/api/compiler/transformers/exponentiation.js +76 -0
- package/dist/api/compiler/transformers/external-module-info.d.ts +35 -0
- package/dist/api/compiler/transformers/external-module-info.js +351 -0
- package/dist/api/compiler/transformers/for-await.d.ts +43 -0
- package/dist/api/compiler/transformers/for-await.js +469 -0
- package/dist/api/compiler/transformers/import-elision.d.ts +16 -0
- package/dist/api/compiler/transformers/import-elision.js +124 -0
- package/dist/api/compiler/transformers/jsx-entities.d.ts +1 -0
- package/dist/api/compiler/transformers/jsx-entities.js +256 -0
- package/dist/api/compiler/transformers/jsx.d.ts +71 -0
- package/dist/api/compiler/transformers/jsx.js +700 -0
- package/dist/api/compiler/transformers/legacy-decorators.d.ts +56 -0
- package/dist/api/compiler/transformers/legacy-decorators.js +564 -0
- package/dist/api/compiler/transformers/logical-assignment.d.ts +14 -0
- package/dist/api/compiler/transformers/logical-assignment.js +62 -0
- package/dist/api/compiler/transformers/module-utilities.d.ts +9 -0
- package/dist/api/compiler/transformers/module-utilities.js +36 -0
- package/dist/api/compiler/transformers/module.d.ts +49 -0
- package/dist/api/compiler/transformers/module.js +296 -0
- package/dist/api/compiler/transformers/named-evaluation.d.ts +26 -0
- package/dist/api/compiler/transformers/named-evaluation.js +266 -0
- package/dist/api/compiler/transformers/nullish-coalescing.d.ts +9 -0
- package/dist/api/compiler/transformers/nullish-coalescing.js +26 -0
- package/dist/api/compiler/transformers/object-rest-spread.d.ts +34 -0
- package/dist/api/compiler/transformers/object-rest-spread.js +411 -0
- package/dist/api/compiler/transformers/optional-catch.d.ts +9 -0
- package/dist/api/compiler/transformers/optional-catch.js +20 -0
- package/dist/api/compiler/transformers/optional-chain.d.ts +14 -0
- package/dist/api/compiler/transformers/optional-chain.js +188 -0
- package/dist/api/compiler/transformers/reference-resolver.d.ts +128 -0
- package/dist/api/compiler/transformers/reference-resolver.js +1155 -0
- package/dist/api/compiler/transformers/runtime-syntax.d.ts +71 -0
- package/dist/api/compiler/transformers/runtime-syntax.js +753 -0
- package/dist/api/compiler/transformers/subtree-facts.d.ts +9 -0
- package/dist/api/compiler/transformers/subtree-facts.js +203 -0
- package/dist/api/compiler/transformers/syntax-facts.d.ts +51 -0
- package/dist/api/compiler/transformers/syntax-facts.js +254 -0
- package/dist/api/compiler/transformers/tagged-template.d.ts +17 -0
- package/dist/api/compiler/transformers/tagged-template.js +148 -0
- package/dist/api/compiler/transformers/transformer.d.ts +17 -0
- package/dist/api/compiler/transformers/transformer.js +54 -0
- package/dist/api/compiler/transformers/type-eraser.d.ts +21 -0
- package/dist/api/compiler/transformers/type-eraser.js +346 -0
- package/dist/api/compiler/transformers/type-serialization.d.ts +20 -0
- package/dist/api/compiler/transformers/type-serialization.js +347 -0
- package/dist/api/compiler/transformers/using.d.ts +56 -0
- package/dist/api/compiler/transformers/using.js +453 -0
- package/dist/api/compiler/transformers/utilities.d.ts +50 -0
- package/dist/api/compiler/transformers/utilities.js +347 -0
- package/dist/api/compiler/transformers/visitor-base.d.ts +6 -0
- package/dist/api/compiler/transformers/visitor-base.js +817 -0
- package/dist/api/compiler/transformers/visitor-hooks.d.ts +29 -0
- package/dist/api/compiler/transformers/visitor-hooks.js +189 -0
- package/dist/api/compiler/tsconfig.d.ts +177 -0
- package/dist/api/compiler/tsconfig.js +1311 -0
- package/dist/api/compiler/type-printer.d.ts +15 -0
- package/dist/api/compiler/type-printer.js +74 -0
- package/dist/api/compiler/types.d.ts +87 -0
- package/dist/api/compiler/types.js +23 -0
- package/dist/api/compiler/unicode-identifiers.d.ts +2 -0
- package/dist/api/compiler/unicode-identifiers.js +209 -0
- package/dist/api/compiler/unicode-properties.d.ts +8 -0
- package/dist/api/compiler/unicode-properties.js +531 -0
- package/dist/api/compiler/vfs-match.d.ts +24 -0
- package/dist/api/compiler/vfs-match.js +587 -0
- package/dist/api/library/ast.d.ts +74 -0
- package/dist/api/library/ast.generated.d.ts +1145 -0
- package/dist/api/library/ast.generated.js +3 -0
- package/dist/api/library/ast.js +5 -0
- package/dist/api/library/checker.d.ts +51 -0
- package/dist/api/library/checker.js +318 -0
- package/dist/api/library/compiler-options.generated.d.ts +108 -0
- package/dist/api/library/compiler-options.generated.js +3 -0
- package/dist/api/library/diagnostics.d.ts +13 -0
- package/dist/api/library/diagnostics.js +50 -0
- package/dist/api/library/disk.d.ts +13 -0
- package/dist/api/library/disk.js +66 -0
- package/dist/api/library/enums.generated.d.ts +891 -0
- package/dist/api/library/enums.generated.js +916 -0
- package/dist/api/library/errors.d.ts +13 -0
- package/dist/api/library/errors.js +24 -0
- package/dist/api/library/host.d.ts +24 -0
- package/dist/api/library/host.js +52 -0
- package/dist/api/library/index.d.ts +14 -0
- package/dist/api/library/index.js +15 -0
- package/dist/api/library/nodes.d.ts +51 -0
- package/dist/api/library/nodes.js +196 -0
- package/dist/api/library/options.d.ts +144 -0
- package/dist/api/library/options.js +308 -0
- package/dist/api/library/parse.d.ts +19 -0
- package/dist/api/library/parse.js +41 -0
- package/dist/api/library/program.d.ts +21 -0
- package/dist/api/library/program.js +234 -0
- package/dist/api/library/symbols.d.ts +8 -0
- package/dist/api/library/symbols.js +11 -0
- package/dist/api/library/types.d.ts +313 -0
- package/dist/api/library/types.js +6 -0
- package/package.json +22 -8
- package/src/compiler/ast-utilities.ts +548 -0
- package/src/compiler/ast.ts +12862 -0
- package/src/compiler/astnav.ts +799 -0
- package/src/compiler/binder-utilities.ts +1392 -0
- package/src/compiler/binder.ts +2703 -0
- package/src/compiler/build-info.ts +1197 -0
- package/src/compiler/character-codes.ts +323 -0
- package/src/compiler/checker-calls.ts +2155 -0
- package/src/compiler/checker-classes.ts +706 -0
- package/src/compiler/checker-contextual.ts +1457 -0
- package/src/compiler/checker-declaration-order.ts +438 -0
- package/src/compiler/checker-declarations.ts +1194 -0
- package/src/compiler/checker-declared-types.ts +919 -0
- package/src/compiler/checker-decorators.ts +694 -0
- package/src/compiler/checker-deprecation.ts +359 -0
- package/src/compiler/checker-destructuring.ts +805 -0
- package/src/compiler/checker-elaboration.ts +477 -0
- package/src/compiler/checker-emit-helpers.ts +224 -0
- package/src/compiler/checker-emit-resolver.ts +769 -0
- package/src/compiler/checker-evaluator.ts +243 -0
- package/src/compiler/checker-expressions.ts +1831 -0
- package/src/compiler/checker-features.ts +361 -0
- package/src/compiler/checker-flow.ts +394 -0
- package/src/compiler/checker-functions.ts +1296 -0
- package/src/compiler/checker-generated-code.ts +227 -0
- package/src/compiler/checker-globals.ts +52 -0
- package/src/compiler/checker-grammar.ts +3584 -0
- package/src/compiler/checker-heritage.ts +1233 -0
- package/src/compiler/checker-inference.ts +2576 -0
- package/src/compiler/checker-iteration.ts +1206 -0
- package/src/compiler/checker-javascript.ts +502 -0
- package/src/compiler/checker-jsdoc.ts +360 -0
- package/src/compiler/checker-jsx.ts +1924 -0
- package/src/compiler/checker-literals.ts +61 -0
- package/src/compiler/checker-members.ts +2436 -0
- package/src/compiler/checker-modules.ts +2886 -0
- package/src/compiler/checker-narrowing.ts +3230 -0
- package/src/compiler/checker-object-literals.ts +575 -0
- package/src/compiler/checker-operators.ts +1422 -0
- package/src/compiler/checker-overloads.ts +328 -0
- package/src/compiler/checker-property-access.ts +906 -0
- package/src/compiler/checker-services.ts +1149 -0
- package/src/compiler/checker-signatures.ts +1032 -0
- package/src/compiler/checker-source-file.ts +732 -0
- package/src/compiler/checker-statements.ts +292 -0
- package/src/compiler/checker-super.ts +261 -0
- package/src/compiler/checker-symbol-types.ts +2107 -0
- package/src/compiler/checker-symbols.ts +2150 -0
- package/src/compiler/checker-type-factory.ts +2579 -0
- package/src/compiler/checker-type-nodes.ts +1033 -0
- package/src/compiler/checker-type-operators.ts +2502 -0
- package/src/compiler/checker-types.ts +2340 -0
- package/src/compiler/checker-unused.ts +642 -0
- package/src/compiler/checker-utilities.ts +161 -0
- package/src/compiler/checker.ts +1116 -0
- package/src/compiler/command-line.ts +436 -0
- package/src/compiler/compile-program.ts +439 -0
- package/src/compiler/decoder.ts +1209 -0
- package/src/compiler/diagnostic-locales.ts +86 -0
- package/src/compiler/diagnostic-messages.ts +2349 -0
- package/src/compiler/diagnostics.ts +897 -0
- package/src/compiler/emitter.ts +434 -0
- package/src/compiler/encoder.ts +1945 -0
- package/src/compiler/file-include.ts +673 -0
- package/src/compiler/file-loader.ts +1528 -0
- package/src/compiler/file-system.ts +143 -0
- package/src/compiler/flow.ts +89 -0
- package/src/compiler/help.ts +473 -0
- package/src/compiler/incremental.ts +2784 -0
- package/src/compiler/init.ts +141 -0
- package/src/compiler/instantiation.ts +894 -0
- package/src/compiler/jsdoc.ts +1386 -0
- package/src/compiler/link-tables.ts +98 -0
- package/src/compiler/locale-data.ts +203 -0
- package/src/compiler/locale.ts +307 -0
- package/src/compiler/locales/cs-CZ.ts +5 -0
- package/src/compiler/locales/de-DE.ts +5 -0
- package/src/compiler/locales/es-ES.ts +5 -0
- package/src/compiler/locales/fr-FR.ts +5 -0
- package/src/compiler/locales/it-IT.ts +5 -0
- package/src/compiler/locales/ja-JP.ts +5 -0
- package/src/compiler/locales/ko-KR.ts +5 -0
- package/src/compiler/locales/pl-PL.ts +5 -0
- package/src/compiler/locales/pt-BR.ts +5 -0
- package/src/compiler/locales/ru-RU.ts +5 -0
- package/src/compiler/locales/tr-TR.ts +5 -0
- package/src/compiler/locales/zh-CN.ts +5 -0
- package/src/compiler/locales/zh-TW.ts +5 -0
- package/src/compiler/module-resolution-types.ts +243 -0
- package/src/compiler/module-resolution.ts +2072 -0
- package/src/compiler/module-specifiers.ts +1491 -0
- package/src/compiler/name-resolver.ts +646 -0
- package/src/compiler/node-builder-names.ts +1069 -0
- package/src/compiler/node-builder-pseudo.ts +892 -0
- package/src/compiler/node-builder-reuse.ts +764 -0
- package/src/compiler/node-builder.ts +3425 -0
- package/src/compiler/option-declarations.ts +1560 -0
- package/src/compiler/options.ts +1576 -0
- package/src/compiler/output-paths.ts +334 -0
- package/src/compiler/package-json.ts +1083 -0
- package/src/compiler/parallel-check.ts +1951 -0
- package/src/compiler/parallel-parse.ts +590 -0
- package/src/compiler/parse-thread.ts +140 -0
- package/src/compiler/parser.ts +7716 -0
- package/src/compiler/path.ts +717 -0
- package/src/compiler/printer/async-helpers.ts +183 -0
- package/src/compiler/printer/auto-generate.ts +27 -0
- package/src/compiler/printer/decorator-helpers.ts +218 -0
- package/src/compiler/printer/emit-context.ts +679 -0
- package/src/compiler/printer/emit-flags.ts +64 -0
- package/src/compiler/printer/factory-base.ts +2577 -0
- package/src/compiler/printer/factory.ts +764 -0
- package/src/compiler/printer/helpers.ts +291 -0
- package/src/compiler/printer/name-generator.ts +372 -0
- package/src/compiler/printer/printer.ts +5072 -0
- package/src/compiler/printer/text-writer.ts +183 -0
- package/src/compiler/printer/utilities.ts +884 -0
- package/src/compiler/program-checker.ts +592 -0
- package/src/compiler/program-utilities.ts +435 -0
- package/src/compiler/program.ts +1419 -0
- package/src/compiler/pseudo-bigint.ts +62 -0
- package/src/compiler/regexp-checker.ts +1142 -0
- package/src/compiler/relations.ts +5149 -0
- package/src/compiler/reparser.ts +1566 -0
- package/src/compiler/scanner.ts +2951 -0
- package/src/compiler/show-config.ts +239 -0
- package/src/compiler/side-state.ts +125 -0
- package/src/compiler/sourcemap.ts +366 -0
- package/src/compiler/spelling.ts +122 -0
- package/src/compiler/symbol-accessibility.ts +1071 -0
- package/src/compiler/symbol.ts +439 -0
- package/src/compiler/symlinks.ts +137 -0
- package/src/compiler/syntax-kind.ts +391 -0
- package/src/compiler/thread-entry.ts +191 -0
- package/src/compiler/timeline.ts +53 -0
- package/src/compiler/transformers/alias-references.ts +757 -0
- package/src/compiler/transformers/async-utilities.ts +335 -0
- package/src/compiler/transformers/async.ts +838 -0
- package/src/compiler/transformers/class-fields.ts +2967 -0
- package/src/compiler/transformers/class-utilities.ts +324 -0
- package/src/compiler/transformers/commonjs-module.ts +1621 -0
- package/src/compiler/transformers/const-enum-inliner.ts +50 -0
- package/src/compiler/transformers/declarations-diagnostics.ts +832 -0
- package/src/compiler/transformers/declarations-pseudo.ts +966 -0
- package/src/compiler/transformers/declarations-resolver.ts +249 -0
- package/src/compiler/transformers/declarations-syntactic.ts +1859 -0
- package/src/compiler/transformers/declarations-tracker.ts +269 -0
- package/src/compiler/transformers/declarations.ts +3120 -0
- package/src/compiler/transformers/decorator-metadata.ts +697 -0
- package/src/compiler/transformers/decorator-utilities.ts +283 -0
- package/src/compiler/transformers/destructuring.ts +708 -0
- package/src/compiler/transformers/downlevel.ts +50 -0
- package/src/compiler/transformers/emit-resolver.ts +258 -0
- package/src/compiler/transformers/es-decorators.ts +2682 -0
- package/src/compiler/transformers/exponentiation.ts +80 -0
- package/src/compiler/transformers/external-module-info.ts +412 -0
- package/src/compiler/transformers/for-await.ts +687 -0
- package/src/compiler/transformers/import-elision.ts +152 -0
- package/src/compiler/transformers/jsx-entities.ts +256 -0
- package/src/compiler/transformers/jsx.ts +824 -0
- package/src/compiler/transformers/legacy-decorators.ts +726 -0
- package/src/compiler/transformers/logical-assignment.ts +75 -0
- package/src/compiler/transformers/module-utilities.ts +42 -0
- package/src/compiler/transformers/module.ts +474 -0
- package/src/compiler/transformers/named-evaluation.ts +389 -0
- package/src/compiler/transformers/nullish-coalescing.ts +35 -0
- package/src/compiler/transformers/object-rest-spread.ts +577 -0
- package/src/compiler/transformers/optional-catch.ts +29 -0
- package/src/compiler/transformers/optional-chain.ts +246 -0
- package/src/compiler/transformers/reference-resolver.ts +1201 -0
- package/src/compiler/transformers/runtime-syntax.ts +942 -0
- package/src/compiler/transformers/subtree-facts.ts +245 -0
- package/src/compiler/transformers/syntax-facts.ts +353 -0
- package/src/compiler/transformers/tagged-template.ts +167 -0
- package/src/compiler/transformers/transformer.ts +56 -0
- package/src/compiler/transformers/type-eraser.ts +520 -0
- package/src/compiler/transformers/type-serialization.ts +374 -0
- package/src/compiler/transformers/using.ts +613 -0
- package/src/compiler/transformers/utilities.ts +419 -0
- package/src/compiler/transformers/visitor-base.ts +1601 -0
- package/src/compiler/transformers/visitor-hooks.ts +252 -0
- package/src/compiler/tsc-build.ts +1418 -0
- package/src/compiler/tsc-cli.ts +666 -0
- package/src/compiler/tsc-compile.ts +437 -0
- package/src/compiler/tsc-watch.ts +854 -0
- package/src/compiler/tsconfig.ts +1574 -0
- package/src/compiler/type-printer.ts +110 -0
- package/src/compiler/types.ts +115 -0
- package/src/compiler/unicode-identifiers.ts +212 -0
- package/src/compiler/unicode-properties.ts +537 -0
- package/src/compiler/vfs-match.ts +597 -0
- package/src/compiler/xxh3.ts +650 -0
- package/src/library/ast.generated.ts +1685 -0
- package/src/library/ast.ts +104 -0
- package/src/library/checker.ts +384 -0
- package/src/library/compiler-options.generated.ts +114 -0
- package/src/library/diagnostics.ts +72 -0
- package/src/library/disk.ts +73 -0
- package/src/library/enums.generated.ts +1024 -0
- package/src/library/errors.ts +26 -0
- package/src/library/host.ts +63 -0
- package/src/library/index.ts +71 -0
- package/src/library/nodes.ts +232 -0
- package/src/library/options.ts +449 -0
- package/src/library/parse.ts +63 -0
- package/src/library/program.ts +293 -0
- package/src/library/symbols.ts +15 -0
- package/src/library/types.ts +359 -0
|
@@ -0,0 +1,2886 @@
|
|
|
1
|
+
// External modules: resolving module specifiers, alias targets of imports and exports, module
|
|
2
|
+
// exports, import types, dynamic imports, and the semantic checks of import and export declarations.
|
|
3
|
+
//
|
|
4
|
+
// Module-not-found errors are reported on every resolution with an error node, as tsgo does: an
|
|
5
|
+
// unresolved `import { a } from "m"` reports once for the declaration and again for each use of `a`.
|
|
6
|
+
import { checkCollisionsForDeclarationName } from './checker-generated-code.js';
|
|
7
|
+
import { checkDeprecatedImportSpecifier } from './checker-deprecation.js';
|
|
8
|
+
import {
|
|
9
|
+
type BinaryExpression,
|
|
10
|
+
type CallExpression,
|
|
11
|
+
type ExportAssignment,
|
|
12
|
+
type ExportDeclaration,
|
|
13
|
+
type HeritageClause,
|
|
14
|
+
type Identifier,
|
|
15
|
+
type ImportAttribute,
|
|
16
|
+
type ImportAttributes,
|
|
17
|
+
type ImportClause,
|
|
18
|
+
type ImportDeclaration,
|
|
19
|
+
type ImportEqualsDeclaration,
|
|
20
|
+
type ImportTypeNode,
|
|
21
|
+
type LiteralTypeNode,
|
|
22
|
+
type MetaProperty,
|
|
23
|
+
type ModuleDeclaration,
|
|
24
|
+
type NamedExports,
|
|
25
|
+
type NamedImports,
|
|
26
|
+
type Node,
|
|
27
|
+
NodeFlags,
|
|
28
|
+
type ObjectLiteralExpression,
|
|
29
|
+
type QualifiedName,
|
|
30
|
+
type SourceFile,
|
|
31
|
+
type VariableDeclaration,
|
|
32
|
+
ModifierFlags,
|
|
33
|
+
getExpression,
|
|
34
|
+
getInitializer,
|
|
35
|
+
getIsTypeOnly,
|
|
36
|
+
getLocals,
|
|
37
|
+
getModuleSpecifier,
|
|
38
|
+
getName,
|
|
39
|
+
getPropertyName,
|
|
40
|
+
getSymbol,
|
|
41
|
+
getText,
|
|
42
|
+
getType,
|
|
43
|
+
} from './ast.js';
|
|
44
|
+
import {
|
|
45
|
+
getExternalModuleName,
|
|
46
|
+
isAmbientModule,
|
|
47
|
+
isImportCall,
|
|
48
|
+
isFunctionLike,
|
|
49
|
+
isGlobalScopeAugmentation,
|
|
50
|
+
isInJSFile,
|
|
51
|
+
isLiteralImportTypeNode,
|
|
52
|
+
isRequireCall,
|
|
53
|
+
isStringLiteralLike,
|
|
54
|
+
isTypeOnly,
|
|
55
|
+
nodeIsMissing,
|
|
56
|
+
} from './ast-utilities.js';
|
|
57
|
+
import {
|
|
58
|
+
declarationNameToString,
|
|
59
|
+
getDeclarationOfKind,
|
|
60
|
+
getModifierFlags,
|
|
61
|
+
isAccessExpression,
|
|
62
|
+
getRootDeclaration,
|
|
63
|
+
getSourceTextOfNode,
|
|
64
|
+
getThisContainer,
|
|
65
|
+
getSourceFileOfNode,
|
|
66
|
+
hasSyntacticModifier,
|
|
67
|
+
isGlobalSourceFile,
|
|
68
|
+
isJsonSourceFile,
|
|
69
|
+
isModuleAugmentationExternal,
|
|
70
|
+
isPartOfTypeQuery,
|
|
71
|
+
isVariableDeclarationInitializedToRequire,
|
|
72
|
+
moduleExportNameIsDefault,
|
|
73
|
+
} from './binder-utilities.js';
|
|
74
|
+
import {
|
|
75
|
+
type Diagnostic,
|
|
76
|
+
type DiagnosticMessage,
|
|
77
|
+
Diagnostics,
|
|
78
|
+
chainDiagnostic,
|
|
79
|
+
RepopulateInfo,
|
|
80
|
+
RepopulateKind,
|
|
81
|
+
} from './diagnostics.js';
|
|
82
|
+
import {
|
|
83
|
+
JsxEmit,
|
|
84
|
+
ModuleKind,
|
|
85
|
+
ModuleResolutionKind,
|
|
86
|
+
getAllowImportingTsExtensions,
|
|
87
|
+
getEmitModuleKind,
|
|
88
|
+
getModuleResolutionKind,
|
|
89
|
+
getResolveJsonModule,
|
|
90
|
+
} from './options.js';
|
|
91
|
+
import {
|
|
92
|
+
type ResolvedModule,
|
|
93
|
+
getResolutionDiagnostic,
|
|
94
|
+
getTypesPackageName,
|
|
95
|
+
mangleScopedPackageName,
|
|
96
|
+
} from './module-resolution-types.js';
|
|
97
|
+
import { getResolutionModeOverride, moduleKindName } from './program-utilities.js';
|
|
98
|
+
import { hasExtension } from './vfs-match.js';
|
|
99
|
+
import { isNodeCoreModule } from './parser.js';
|
|
100
|
+
import {
|
|
101
|
+
combinePaths,
|
|
102
|
+
extensionIsTs,
|
|
103
|
+
fileExtensionIsOneOf,
|
|
104
|
+
getDeclarationFileExtension,
|
|
105
|
+
getDirectoryPath,
|
|
106
|
+
getNormalizedAbsolutePath,
|
|
107
|
+
isDeclarationFileName,
|
|
108
|
+
isExternalModuleNameRelative,
|
|
109
|
+
pathIsRelative,
|
|
110
|
+
removeExtension,
|
|
111
|
+
supportedTSExtensionsFlat,
|
|
112
|
+
tryExtractTSExtension,
|
|
113
|
+
tryGetExtensionFromPath,
|
|
114
|
+
} from './path.js';
|
|
115
|
+
import { InternalSymbolName, type Symbol, SymbolFlags, type SymbolTable, getSymbolId } from './symbol.js';
|
|
116
|
+
import { SyntaxKind } from './syntax-kind.js';
|
|
117
|
+
import { isExpressionNode } from './transformers/alias-references.js';
|
|
118
|
+
import { type Checker, type CheckerHost, createDiagnosticForNode } from './checker.js';
|
|
119
|
+
import { ExternalEmitHelpers, checkExternalEmitHelpers } from './checker-emit-helpers.js';
|
|
120
|
+
import { ObjectFlags, type Type, TypeFlags, SignatureKind } from './checker-types.js';
|
|
121
|
+
import {
|
|
122
|
+
createAnonymousType,
|
|
123
|
+
getRegularTypeOfLiteralType,
|
|
124
|
+
getStringLiteralType,
|
|
125
|
+
isErrorType,
|
|
126
|
+
} from './checker-type-factory.js';
|
|
127
|
+
import {
|
|
128
|
+
getFullyQualifiedName,
|
|
129
|
+
getMergedSymbol,
|
|
130
|
+
getSpellingSuggestionForName,
|
|
131
|
+
getSymbolFlags,
|
|
132
|
+
getSymbolOfDeclaration,
|
|
133
|
+
getSymbolOfMeaning,
|
|
134
|
+
getDeclarationOfAliasSymbol,
|
|
135
|
+
getExportSymbolOfValueSymbolIfExported,
|
|
136
|
+
getExportsOfModule,
|
|
137
|
+
getExportsOfSymbol,
|
|
138
|
+
resolveAlias,
|
|
139
|
+
resolveEntityName,
|
|
140
|
+
resolveName,
|
|
141
|
+
resolveSymbol,
|
|
142
|
+
resolveSymbolEx,
|
|
143
|
+
isNonLocalAlias,
|
|
144
|
+
getCachedGlobalType,
|
|
145
|
+
} from './checker-symbols.js';
|
|
146
|
+
import { getTypeOfSymbol, getNullableType } from './checker-symbol-types.js';
|
|
147
|
+
import {
|
|
148
|
+
getPropertyOfType,
|
|
149
|
+
getPropertyOfTypeEx,
|
|
150
|
+
getSignaturesOfStructuredType,
|
|
151
|
+
resolveStructuredTypeMembers,
|
|
152
|
+
} from './checker-members.js';
|
|
153
|
+
import { getTypeFromTypeNode, getTypeReferenceType } from './checker-type-nodes.js';
|
|
154
|
+
import { checkExpression, checkExpressionCached, getResolvedSymbol } from './checker-expressions.js';
|
|
155
|
+
import { isTypeAssignableTo, checkTypeAssignableTo } from './relations.js';
|
|
156
|
+
import { getSpreadType } from './checker-object-literals.js';
|
|
157
|
+
import { checkExternalModuleExports, shouldCheckErasableSyntax } from './checker-declarations.js';
|
|
158
|
+
import { checkTypeNameIsReserved } from './checker-classes.js';
|
|
159
|
+
import { getDeclaredTypeOfSymbol } from './checker-declared-types.js';
|
|
160
|
+
import { isValidSpreadType } from './checker-destructuring.js';
|
|
161
|
+
import { createPromiseType } from './checker-iteration.js';
|
|
162
|
+
import { symbolToString, typeToString } from './type-printer.js';
|
|
163
|
+
import { checkTypeAssignableToAndOptionallyElaborate } from './checker-expressions.js';
|
|
164
|
+
import { grammarErrorOnNode } from './checker-operators.js';
|
|
165
|
+
|
|
166
|
+
// ---- Resolution
|
|
167
|
+
|
|
168
|
+
/** The module symbol `moduleReference` names, reporting it when it isn't found unless `ignoreErrors`. */
|
|
169
|
+
export function resolveExternalModuleName(
|
|
170
|
+
c: Checker,
|
|
171
|
+
location: Node,
|
|
172
|
+
moduleReference: Node | undefined,
|
|
173
|
+
ignoreErrors: boolean,
|
|
174
|
+
): Symbol | undefined {
|
|
175
|
+
return resolveExternalModuleNameWorker(
|
|
176
|
+
c,
|
|
177
|
+
location,
|
|
178
|
+
moduleReference,
|
|
179
|
+
ignoreErrors ? undefined : getCannotResolveModuleNameError(c, moduleReference),
|
|
180
|
+
ignoreErrors,
|
|
181
|
+
false,
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Node core modules suggest installing `@types/node`. */
|
|
186
|
+
function getCannotResolveModuleNameError(c: Checker, moduleReference: Node | undefined): DiagnosticMessage {
|
|
187
|
+
if (
|
|
188
|
+
moduleReference !== undefined &&
|
|
189
|
+
moduleReference.kind === SyntaxKind.StringLiteral &&
|
|
190
|
+
isNodeCoreModule(getText(moduleReference))
|
|
191
|
+
) {
|
|
192
|
+
const types = c.compilerOptions.types;
|
|
193
|
+
return types !== undefined && types.includes('*')
|
|
194
|
+
? Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode
|
|
195
|
+
: Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode_and_then_add_node_to_the_types_field_in_your_tsconfig;
|
|
196
|
+
}
|
|
197
|
+
return Diagnostics.Cannot_find_module_0_or_its_corresponding_type_declarations;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function resolveExternalModuleNameWorker(
|
|
201
|
+
c: Checker,
|
|
202
|
+
location: Node,
|
|
203
|
+
moduleReference: Node | undefined,
|
|
204
|
+
moduleNotFoundError: DiagnosticMessage | undefined,
|
|
205
|
+
ignoreErrors: boolean,
|
|
206
|
+
isForAugmentation: boolean,
|
|
207
|
+
): Symbol | undefined {
|
|
208
|
+
if (moduleReference === undefined || !isStringLiteralLike(moduleReference)) return undefined;
|
|
209
|
+
return resolveExternalModule(
|
|
210
|
+
c,
|
|
211
|
+
location,
|
|
212
|
+
getText(moduleReference),
|
|
213
|
+
moduleNotFoundError,
|
|
214
|
+
ignoreErrors ? undefined : moduleReference,
|
|
215
|
+
isForAugmentation,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function resolveExternalModule(
|
|
220
|
+
c: Checker,
|
|
221
|
+
location: Node,
|
|
222
|
+
moduleReference: string,
|
|
223
|
+
moduleNotFoundError: DiagnosticMessage | undefined,
|
|
224
|
+
errorNode: Node | undefined,
|
|
225
|
+
isForAugmentation: boolean,
|
|
226
|
+
): Symbol | undefined {
|
|
227
|
+
if (errorNode !== undefined && moduleReference.startsWith('@types/'))
|
|
228
|
+
c.error(
|
|
229
|
+
errorNode,
|
|
230
|
+
Diagnostics.Cannot_import_type_declaration_files_Consider_importing_0_instead_of_1,
|
|
231
|
+
moduleReference.substring(7),
|
|
232
|
+
moduleReference,
|
|
233
|
+
);
|
|
234
|
+
const ambient = tryFindAmbientModule(c, moduleReference, true);
|
|
235
|
+
if (ambient !== undefined) return ambient;
|
|
236
|
+
const importingFile = getSourceFileOfNode(location);
|
|
237
|
+
const contextSpecifier = getContextSpecifier(location);
|
|
238
|
+
const mode =
|
|
239
|
+
contextSpecifier !== undefined && isStringLiteralLike(contextSpecifier)
|
|
240
|
+
? c.host.getModeForUsageLocation(importingFile, contextSpecifier)
|
|
241
|
+
: c.host.getDefaultResolutionModeForFile(importingFile);
|
|
242
|
+
const resolved = c.host.getResolvedModule(importingFile, moduleReference, mode);
|
|
243
|
+
const isResolved = resolved !== undefined && resolved.resolvedFileName !== '';
|
|
244
|
+
const resolutionDiagnostic =
|
|
245
|
+
errorNode !== undefined && isResolved
|
|
246
|
+
? getResolutionDiagnostic(c.compilerOptions, resolved, importingFile.isDeclarationFile)
|
|
247
|
+
: undefined;
|
|
248
|
+
const file =
|
|
249
|
+
isResolved &&
|
|
250
|
+
(resolutionDiagnostic === undefined ||
|
|
251
|
+
resolutionDiagnostic === Diagnostics.Module_0_was_resolved_to_1_but_jsx_is_not_set)
|
|
252
|
+
? c.host.getSourceFileForResolvedModule(resolved.resolvedFileName)
|
|
253
|
+
: undefined;
|
|
254
|
+
if (file !== undefined) {
|
|
255
|
+
if (errorNode !== undefined) {
|
|
256
|
+
// A resolution error is reported even though the file was found.
|
|
257
|
+
if (resolutionDiagnostic !== undefined)
|
|
258
|
+
c.error(errorNode, resolutionDiagnostic, moduleReference, resolved!.resolvedFileName);
|
|
259
|
+
checkResolvedModuleExtension(c, location, moduleReference, resolved!, importingFile, mode, errorNode);
|
|
260
|
+
}
|
|
261
|
+
if (file.symbol !== undefined) {
|
|
262
|
+
if (errorNode !== undefined) {
|
|
263
|
+
if (resolved!.isExternalLibraryImport && !resolutionExtensionIsTSOrJson(resolved!.extension))
|
|
264
|
+
errorOnImplicitAnyModule(c, false, errorNode, mode, resolved!, moduleReference);
|
|
265
|
+
checkSyncImportOfESModule(c, location, moduleReference, importingFile, file, errorNode);
|
|
266
|
+
}
|
|
267
|
+
return getMergedSymbol(c, file.symbol);
|
|
268
|
+
}
|
|
269
|
+
if (errorNode !== undefined && moduleNotFoundError !== undefined && !isSideEffectImport(errorNode))
|
|
270
|
+
c.error(errorNode, Diagnostics.File_0_is_not_a_module, resolved!.resolvedFileName);
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|
|
273
|
+
// The pattern with the longest prefix wins.
|
|
274
|
+
let best: Symbol | undefined;
|
|
275
|
+
let bestPrefix = -1;
|
|
276
|
+
for (const pattern of c.patternAmbientModules) {
|
|
277
|
+
if (pattern.matches(moduleReference) && pattern.starIndex > bestPrefix) {
|
|
278
|
+
bestPrefix = pattern.starIndex;
|
|
279
|
+
best = pattern.symbol;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (best !== undefined) return getMergedSymbol(c, c.patternAmbientModuleAugmentations.get(moduleReference) ?? best);
|
|
283
|
+
if (errorNode === undefined) return undefined;
|
|
284
|
+
// A module that resolved to JavaScript is untyped.
|
|
285
|
+
if (
|
|
286
|
+
(isResolved && !resolutionExtensionIsTSOrJson(resolved.extension) && resolutionDiagnostic === undefined) ||
|
|
287
|
+
resolutionDiagnostic === Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type
|
|
288
|
+
) {
|
|
289
|
+
if (isForAugmentation)
|
|
290
|
+
c.error(
|
|
291
|
+
errorNode,
|
|
292
|
+
Diagnostics.Invalid_module_name_in_augmentation_Module_0_resolves_to_an_untyped_module_at_1_which_cannot_be_augmented,
|
|
293
|
+
moduleReference,
|
|
294
|
+
resolved!.resolvedFileName,
|
|
295
|
+
);
|
|
296
|
+
else
|
|
297
|
+
errorOnImplicitAnyModule(
|
|
298
|
+
c,
|
|
299
|
+
c.noImplicitAny && moduleNotFoundError !== undefined,
|
|
300
|
+
errorNode,
|
|
301
|
+
mode,
|
|
302
|
+
resolved!,
|
|
303
|
+
moduleReference,
|
|
304
|
+
);
|
|
305
|
+
return undefined;
|
|
306
|
+
}
|
|
307
|
+
if (moduleNotFoundError === undefined) return undefined;
|
|
308
|
+
if (isResolved) {
|
|
309
|
+
// A source file of a referenced project whose output hasn't been built.
|
|
310
|
+
const outputDts = c.host.getProjectReferenceOutputDts(resolved.resolvedFileName);
|
|
311
|
+
if (outputDts !== '') {
|
|
312
|
+
c.error(
|
|
313
|
+
errorNode,
|
|
314
|
+
Diagnostics.Output_file_0_has_not_been_built_from_source_file_1,
|
|
315
|
+
outputDts,
|
|
316
|
+
resolved.resolvedFileName,
|
|
317
|
+
);
|
|
318
|
+
return undefined;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (resolutionDiagnostic !== undefined) {
|
|
322
|
+
c.error(errorNode, resolutionDiagnostic, moduleReference, resolved!.resolvedFileName);
|
|
323
|
+
return undefined;
|
|
324
|
+
}
|
|
325
|
+
const resolution = getModuleResolutionKind(c.compilerOptions);
|
|
326
|
+
if (!getResolveJsonModule(c.compilerOptions) && moduleReference.endsWith('.json'))
|
|
327
|
+
c.error(
|
|
328
|
+
errorNode,
|
|
329
|
+
Diagnostics.Cannot_find_module_0_Consider_using_resolveJsonModule_to_import_module_with_json_extension,
|
|
330
|
+
moduleReference,
|
|
331
|
+
);
|
|
332
|
+
else if (
|
|
333
|
+
mode === ModuleKind.ESNext &&
|
|
334
|
+
(resolution === ModuleResolutionKind.Node16 || resolution === ModuleResolutionKind.NodeNext) &&
|
|
335
|
+
pathIsRelative(moduleReference) &&
|
|
336
|
+
!hasExtension(moduleReference)
|
|
337
|
+
) {
|
|
338
|
+
const absolute = getNormalizedAbsolutePath(moduleReference, getDirectoryPath(importingFile.fileName));
|
|
339
|
+
const extension = getSuggestedImportExtension(c, absolute);
|
|
340
|
+
if (extension !== '')
|
|
341
|
+
c.error(
|
|
342
|
+
errorNode,
|
|
343
|
+
Diagnostics.Relative_import_paths_need_explicit_file_extensions_in_ECMAScript_imports_when_moduleResolution_is_node16_or_nodenext_Did_you_mean_0,
|
|
344
|
+
moduleReference + extension,
|
|
345
|
+
);
|
|
346
|
+
else
|
|
347
|
+
c.error(
|
|
348
|
+
errorNode,
|
|
349
|
+
Diagnostics.Relative_import_paths_need_explicit_file_extensions_in_ECMAScript_imports_when_moduleResolution_is_node16_or_nodenext_Consider_adding_an_extension_to_the_import_path,
|
|
350
|
+
);
|
|
351
|
+
} else if (resolved !== undefined && resolved.alternateResult !== '')
|
|
352
|
+
c.addDiagnostic(
|
|
353
|
+
chainDiagnostic(
|
|
354
|
+
createModuleNotFoundChain(c, errorNode, moduleReference, mode, moduleReference),
|
|
355
|
+
moduleNotFoundError,
|
|
356
|
+
moduleReference,
|
|
357
|
+
),
|
|
358
|
+
);
|
|
359
|
+
else c.error(errorNode, moduleNotFoundError, moduleReference);
|
|
360
|
+
return undefined;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** The module specifier that decides the resolution mode of a module reference at `location`. */
|
|
364
|
+
function getContextSpecifier(location: Node): Node | undefined {
|
|
365
|
+
const parent = location.parent;
|
|
366
|
+
if (
|
|
367
|
+
isStringLiteralLike(location) ||
|
|
368
|
+
(parent !== undefined && parent.kind === SyntaxKind.ModuleDeclaration && getName(parent) === location)
|
|
369
|
+
)
|
|
370
|
+
return location;
|
|
371
|
+
if (location.kind === SyntaxKind.ModuleDeclaration) return getName(location);
|
|
372
|
+
if (isLiteralImportTypeNode(location)) return ((location as ImportTypeNode).argument as LiteralTypeNode).literal;
|
|
373
|
+
const requireSpecifier = getModuleSpecifierOfBareOrAccessedRequire(location);
|
|
374
|
+
if (requireSpecifier !== undefined) return requireSpecifier;
|
|
375
|
+
for (let current: Node | undefined = location; current !== undefined; current = current.parent)
|
|
376
|
+
if (isImportCall(current)) return (current as CallExpression).arguments.nodes[0];
|
|
377
|
+
for (let current: Node | undefined = location; current !== undefined; current = current.parent)
|
|
378
|
+
if (current.kind === SyntaxKind.ImportDeclaration || current.kind === SyntaxKind.JSImportDeclaration)
|
|
379
|
+
return getModuleSpecifier(current);
|
|
380
|
+
for (let current: Node | undefined = location; current !== undefined; current = current.parent)
|
|
381
|
+
if (current.kind === SyntaxKind.ExportDeclaration) return getModuleSpecifier(current);
|
|
382
|
+
for (let current: Node | undefined = location; current !== undefined; current = current.parent)
|
|
383
|
+
if (current.kind === SyntaxKind.ImportEqualsDeclaration) {
|
|
384
|
+
const reference = (current as ImportEqualsDeclaration).moduleReference;
|
|
385
|
+
return reference.kind === SyntaxKind.ExternalModuleReference ? getExpression(reference) : undefined;
|
|
386
|
+
}
|
|
387
|
+
return undefined;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** The specifier of `const x = require("m")` or `const x = require("m").a.b` in JavaScript. */
|
|
391
|
+
function getModuleSpecifierOfBareOrAccessedRequire(node: Node): Node | undefined {
|
|
392
|
+
if (node.kind !== SyntaxKind.VariableDeclaration || !isInJSFile(node)) return undefined;
|
|
393
|
+
const declaration = node as VariableDeclaration;
|
|
394
|
+
let initializer = declaration.initializer;
|
|
395
|
+
if (initializer === undefined || declaration.type !== undefined) return undefined;
|
|
396
|
+
if ((getModifierFlags(declaration.parent!.parent!) & ModifierFlags.Export) !== 0) return undefined;
|
|
397
|
+
while (isAccessExpression(initializer)) initializer = getExpression(initializer)!;
|
|
398
|
+
return isRequireCall(initializer, true) ? (initializer as CallExpression).arguments.nodes[0] : undefined;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function resolutionExtensionIsTSOrJson(extension: string): boolean {
|
|
402
|
+
return extensionIsTs(extension) || extension === '.json';
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** Imports that name a `.ts` file need `allowImportingTsExtensions`, and declaration files can't be imported. */
|
|
406
|
+
function checkResolvedModuleExtension(
|
|
407
|
+
c: Checker,
|
|
408
|
+
location: Node,
|
|
409
|
+
moduleReference: string,
|
|
410
|
+
resolved: ResolvedModule,
|
|
411
|
+
importingFile: SourceFile,
|
|
412
|
+
mode: ModuleKind,
|
|
413
|
+
errorNode: Node,
|
|
414
|
+
): void {
|
|
415
|
+
if (!resolved.resolvedUsingTsExtension) return;
|
|
416
|
+
if (isDeclarationFileName(moduleReference)) {
|
|
417
|
+
if (isInEmittableImport(location))
|
|
418
|
+
c.error(
|
|
419
|
+
errorNode,
|
|
420
|
+
Diagnostics.A_declaration_file_cannot_be_imported_without_import_type_Did_you_mean_to_import_an_implementation_file_0_instead,
|
|
421
|
+
getSuggestedImportSource(c, moduleReference, tryExtractTSExtension(moduleReference), mode),
|
|
422
|
+
);
|
|
423
|
+
} else if (
|
|
424
|
+
!getAllowImportingTsExtensions(c.compilerOptions) &&
|
|
425
|
+
!isDeclarationFileName(importingFile.fileName) &&
|
|
426
|
+
isInEmittableImport(location)
|
|
427
|
+
) {
|
|
428
|
+
let extension = tryExtractTSExtension(moduleReference);
|
|
429
|
+
// A wildcard pattern can match a `.ts` extension that doesn't end the specifier.
|
|
430
|
+
if (extension === '')
|
|
431
|
+
for (const candidate of supportedTSExtensionsFlat)
|
|
432
|
+
if (moduleReference.includes(candidate)) {
|
|
433
|
+
extension = candidate;
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
c.error(
|
|
437
|
+
errorNode,
|
|
438
|
+
Diagnostics.An_import_path_can_only_end_with_a_0_extension_when_allowImportingTsExtensions_is_enabled,
|
|
439
|
+
extension,
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function isInEmittableImport(location: Node): boolean {
|
|
445
|
+
for (let current: Node | undefined = location; current !== undefined; current = current.parent) {
|
|
446
|
+
switch (current.kind) {
|
|
447
|
+
case SyntaxKind.ImportDeclaration: {
|
|
448
|
+
const clause = (current as ImportDeclaration).importClause;
|
|
449
|
+
if (clause !== undefined && !isTypeOnly(clause)) return true;
|
|
450
|
+
break;
|
|
451
|
+
}
|
|
452
|
+
case SyntaxKind.ExportDeclaration:
|
|
453
|
+
case SyntaxKind.ImportEqualsDeclaration:
|
|
454
|
+
if (!getIsTypeOnly(current)) return true;
|
|
455
|
+
break;
|
|
456
|
+
case SyntaxKind.CallExpression:
|
|
457
|
+
if (isImportCall(current)) return true;
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
return false;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/** The specifier to import instead of a declaration file: its `.js` counterpart when emitting ES modules. */
|
|
465
|
+
function getSuggestedImportSource(c: Checker, moduleReference: string, tsExtension: string, mode: ModuleKind): string {
|
|
466
|
+
const withoutExtension = removeExtension(moduleReference, tsExtension);
|
|
467
|
+
const moduleKind = getEmitModuleKind(c.compilerOptions);
|
|
468
|
+
if ((ModuleKind.ES2015 <= moduleKind && moduleKind <= ModuleKind.ESNext) || mode === ModuleKind.ESNext) {
|
|
469
|
+
const preferTs = isDeclarationFileName(moduleReference) && getAllowImportingTsExtensions(c.compilerOptions);
|
|
470
|
+
if (tsExtension === '.mts' || tsExtension === '.d.mts') return withoutExtension + (preferTs ? '.mts' : '.mjs');
|
|
471
|
+
if (tsExtension === '.cts' || tsExtension === '.d.cts') return withoutExtension + (preferTs ? '.cts' : '.cjs');
|
|
472
|
+
return withoutExtension + (preferTs ? '.ts' : '.js');
|
|
473
|
+
}
|
|
474
|
+
return withoutExtension;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function getSuggestedImportExtension(c: Checker, extensionlessPath: string): string {
|
|
478
|
+
const host = c.host;
|
|
479
|
+
if (host.fileExists(extensionlessPath + '.mts')) return '.mjs';
|
|
480
|
+
if (host.fileExists(extensionlessPath + '.ts')) return '.js';
|
|
481
|
+
if (host.fileExists(extensionlessPath + '.cts')) return '.cjs';
|
|
482
|
+
if (host.fileExists(extensionlessPath + '.mjs')) return '.mjs';
|
|
483
|
+
if (host.fileExists(extensionlessPath + '.js')) return '.js';
|
|
484
|
+
if (host.fileExists(extensionlessPath + '.cjs')) return '.cjs';
|
|
485
|
+
if (host.fileExists(extensionlessPath + '.tsx')) return c.compilerOptions.jsx === JsxEmit.Preserve ? '.jsx' : '.js';
|
|
486
|
+
if (host.fileExists(extensionlessPath + '.jsx')) return '.jsx';
|
|
487
|
+
if (host.fileExists(extensionlessPath + '.json')) return '.json';
|
|
488
|
+
return '';
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/** Under node16 and node18, a synchronous import of an ES module from a CommonJS file fails at run time. */
|
|
492
|
+
function checkSyncImportOfESModule(
|
|
493
|
+
c: Checker,
|
|
494
|
+
location: Node,
|
|
495
|
+
moduleReference: string,
|
|
496
|
+
importingFile: SourceFile,
|
|
497
|
+
file: SourceFile,
|
|
498
|
+
errorNode: Node,
|
|
499
|
+
): void {
|
|
500
|
+
const moduleKind = getEmitModuleKind(c.compilerOptions);
|
|
501
|
+
if (moduleKind !== ModuleKind.Node16 && moduleKind !== ModuleKind.Node18) return;
|
|
502
|
+
const importEquals = findAncestorOfKind(location, SyntaxKind.ImportEqualsDeclaration);
|
|
503
|
+
const isSyncImport =
|
|
504
|
+
(c.host.getDefaultResolutionModeForFile(importingFile) === ModuleKind.CommonJS && !isInImportCall(location)) ||
|
|
505
|
+
importEquals !== undefined;
|
|
506
|
+
if (!isSyncImport || c.host.getDefaultResolutionModeForFile(file) !== ModuleKind.ESNext) return;
|
|
507
|
+
const overrideHost = findResolutionModeOverrideHost(location);
|
|
508
|
+
if (overrideHost !== undefined && hasResolutionModeOverride(overrideHost)) return;
|
|
509
|
+
if (importEquals !== undefined) {
|
|
510
|
+
c.error(
|
|
511
|
+
errorNode,
|
|
512
|
+
Diagnostics.Module_0_cannot_be_imported_using_this_construct_The_specifier_only_resolves_to_an_ES_module_which_cannot_be_imported_with_require_Use_an_ECMAScript_import_instead,
|
|
513
|
+
moduleReference,
|
|
514
|
+
);
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
let details: Diagnostic | undefined;
|
|
518
|
+
const extension = tryGetExtensionFromPath(importingFile.fileName);
|
|
519
|
+
if (extension === '.ts' || extension === '.js' || extension === '.tsx' || extension === '.jsx') {
|
|
520
|
+
details = detailsAt(errorNode, modeMismatchDetails(c.host, importingFile));
|
|
521
|
+
details.repopulateInfo = new RepopulateInfo(RepopulateKind.ModeMismatch);
|
|
522
|
+
}
|
|
523
|
+
let message =
|
|
524
|
+
Diagnostics.The_current_file_is_a_CommonJS_module_whose_imports_will_produce_require_calls_however_the_referenced_file_is_an_ECMAScript_module_and_cannot_be_imported_with_require_Consider_writing_a_dynamic_import_0_call_instead;
|
|
525
|
+
if (overrideHost !== undefined && overrideHost.kind === SyntaxKind.ImportDeclaration) {
|
|
526
|
+
const clause = (overrideHost as ImportDeclaration).importClause;
|
|
527
|
+
if (clause !== undefined && isTypeOnly(clause))
|
|
528
|
+
message =
|
|
529
|
+
Diagnostics.Type_only_import_of_an_ECMAScript_module_from_a_CommonJS_module_must_have_a_resolution_mode_attribute;
|
|
530
|
+
} else if (overrideHost !== undefined && overrideHost.kind === SyntaxKind.ImportType)
|
|
531
|
+
message =
|
|
532
|
+
Diagnostics.Type_import_of_an_ECMAScript_module_from_a_CommonJS_module_must_have_a_resolution_mode_attribute;
|
|
533
|
+
c.addDiagnostic(
|
|
534
|
+
details !== undefined
|
|
535
|
+
? chainDiagnostic(details, message, moduleReference)
|
|
536
|
+
: createDiagnosticForNode(errorNode, message, [moduleReference]),
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function findAncestorOfKind(node: Node, kind: SyntaxKind): Node | undefined {
|
|
541
|
+
for (let current: Node | undefined = node; current !== undefined; current = current.parent)
|
|
542
|
+
if (current.kind === kind) return current;
|
|
543
|
+
return undefined;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
function isInImportCall(node: Node): boolean {
|
|
547
|
+
for (let current: Node | undefined = node; current !== undefined; current = current.parent)
|
|
548
|
+
if (isImportCall(current)) return true;
|
|
549
|
+
return false;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function findResolutionModeOverrideHost(node: Node): Node | undefined {
|
|
553
|
+
for (let current: Node | undefined = node; current !== undefined; current = current.parent)
|
|
554
|
+
switch (current.kind) {
|
|
555
|
+
case SyntaxKind.ImportType:
|
|
556
|
+
case SyntaxKind.ExportDeclaration:
|
|
557
|
+
case SyntaxKind.ImportDeclaration:
|
|
558
|
+
case SyntaxKind.JSImportDeclaration:
|
|
559
|
+
return current;
|
|
560
|
+
}
|
|
561
|
+
return undefined;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function hasResolutionModeOverride(node: Node): boolean {
|
|
565
|
+
let attributes: Node | undefined;
|
|
566
|
+
if (node.kind === SyntaxKind.ImportType) attributes = (node as ImportTypeNode).attributes;
|
|
567
|
+
else if (node.kind === SyntaxKind.ExportDeclaration) attributes = (node as ExportDeclaration).attributes;
|
|
568
|
+
else attributes = (node as ImportDeclaration).attributes;
|
|
569
|
+
return attributes !== undefined && getResolutionModeOverride(attributes as ImportAttributes) !== ModuleKind.None;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/** A message with its arguments, the elaboration of a diagnostic before it has a location. */
|
|
573
|
+
export class DiagnosticDetails {
|
|
574
|
+
constructor(
|
|
575
|
+
readonly message: DiagnosticMessage,
|
|
576
|
+
readonly args: string[],
|
|
577
|
+
) {}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function detailsAt(node: Node, details: DiagnosticDetails): Diagnostic {
|
|
581
|
+
return createDiagnosticForNode(node, details.message, details.args);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* How to make the CommonJS file `file` an ES module, elaborating an error about importing one from
|
|
586
|
+
* it; also what an incremental build recomputes for a stored diagnostic (`RepopulateKind.ModeMismatch`).
|
|
587
|
+
*/
|
|
588
|
+
export function modeMismatchDetails(host: CheckerHost, file: SourceFile): DiagnosticDetails {
|
|
589
|
+
const extension = tryGetExtensionFromPath(file.fileName);
|
|
590
|
+
const targetExtension = extension === '.ts' ? '.mts' : extension === '.js' ? '.mjs' : '';
|
|
591
|
+
const metadata = host.getSourceFileMetadata(file);
|
|
592
|
+
const packageJsonDirectory = metadata !== undefined ? metadata.packageJsonDirectory : '';
|
|
593
|
+
if (packageJsonDirectory !== '' && metadata!.packageJsonType === '') {
|
|
594
|
+
const packageJson = combinePaths(packageJsonDirectory, 'package.json');
|
|
595
|
+
return targetExtension !== ''
|
|
596
|
+
? new DiagnosticDetails(
|
|
597
|
+
Diagnostics.To_convert_this_file_to_an_ECMAScript_module_change_its_file_extension_to_0_or_add_the_field_type_Colon_module_to_1,
|
|
598
|
+
[targetExtension, packageJson],
|
|
599
|
+
)
|
|
600
|
+
: new DiagnosticDetails(
|
|
601
|
+
Diagnostics.To_convert_this_file_to_an_ECMAScript_module_add_the_field_type_Colon_module_to_0,
|
|
602
|
+
[packageJson],
|
|
603
|
+
);
|
|
604
|
+
}
|
|
605
|
+
return targetExtension !== ''
|
|
606
|
+
? new DiagnosticDetails(
|
|
607
|
+
Diagnostics.To_convert_this_file_to_an_ECMAScript_module_change_its_file_extension_to_0_or_create_a_local_package_json_file_with_type_Colon_module,
|
|
608
|
+
[targetExtension],
|
|
609
|
+
)
|
|
610
|
+
: new DiagnosticDetails(
|
|
611
|
+
Diagnostics.To_convert_this_file_to_an_ECMAScript_module_create_a_local_package_json_file_with_type_Colon_module,
|
|
612
|
+
[],
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/** Reports a module without types: an error under `noImplicitAny`, otherwise a suggestion. */
|
|
617
|
+
function errorOnImplicitAnyModule(
|
|
618
|
+
c: Checker,
|
|
619
|
+
isError: boolean,
|
|
620
|
+
errorNode: Node,
|
|
621
|
+
mode: ModuleKind,
|
|
622
|
+
resolved: ResolvedModule,
|
|
623
|
+
moduleReference: string,
|
|
624
|
+
): void {
|
|
625
|
+
if (isSideEffectImport(errorNode)) return;
|
|
626
|
+
const packageName = resolved.packageId !== undefined ? resolved.packageId.name : '';
|
|
627
|
+
const chain =
|
|
628
|
+
!isExternalModuleNameRelative(moduleReference) && packageName !== ''
|
|
629
|
+
? createModuleNotFoundChain(c, errorNode, moduleReference, mode, packageName)
|
|
630
|
+
: undefined;
|
|
631
|
+
const message = Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type;
|
|
632
|
+
c.addErrorOrSuggestion(
|
|
633
|
+
isError,
|
|
634
|
+
chain !== undefined
|
|
635
|
+
? chainDiagnostic(chain, message, moduleReference, resolved.resolvedFileName)
|
|
636
|
+
: createDiagnosticForNode(errorNode, message, [moduleReference, resolved.resolvedFileName]),
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/** Where types for a module might come from, elaborating an error about a module without them. */
|
|
641
|
+
function createModuleNotFoundChain(
|
|
642
|
+
c: Checker,
|
|
643
|
+
errorNode: Node,
|
|
644
|
+
moduleReference: string,
|
|
645
|
+
mode: ModuleKind,
|
|
646
|
+
packageName: string,
|
|
647
|
+
): Diagnostic {
|
|
648
|
+
const file = getSourceFileOfNode(errorNode);
|
|
649
|
+
const result = detailsAt(errorNode, moduleNotFoundDetails(c.host, file, moduleReference, mode, packageName));
|
|
650
|
+
result.repopulateInfo = new RepopulateInfo(
|
|
651
|
+
RepopulateKind.ModuleNotFound,
|
|
652
|
+
moduleReference,
|
|
653
|
+
mode,
|
|
654
|
+
packageName === moduleReference ? '' : packageName,
|
|
655
|
+
);
|
|
656
|
+
return result;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* Where types for `moduleReference`, imported by `file`, might come from; also what an incremental
|
|
661
|
+
* build recomputes for a stored diagnostic (`RepopulateKind.ModuleNotFound`).
|
|
662
|
+
*/
|
|
663
|
+
export function moduleNotFoundDetails(
|
|
664
|
+
host: CheckerHost,
|
|
665
|
+
file: SourceFile,
|
|
666
|
+
moduleReference: string,
|
|
667
|
+
mode: ModuleKind,
|
|
668
|
+
packageNameIn: string,
|
|
669
|
+
): DiagnosticDetails {
|
|
670
|
+
let packageName = packageNameIn;
|
|
671
|
+
const resolved = host.getResolvedModule(file, moduleReference, mode);
|
|
672
|
+
if (resolved !== undefined && resolved.alternateResult !== '') {
|
|
673
|
+
if (resolved.alternateResult.includes('/node_modules/@types/'))
|
|
674
|
+
packageName = '@types/' + mangleScopedPackageName(packageName);
|
|
675
|
+
return new DiagnosticDetails(
|
|
676
|
+
Diagnostics.There_are_types_at_0_but_this_result_could_not_be_resolved_when_respecting_package_json_exports_The_1_library_may_need_to_update_its_package_json_or_typings,
|
|
677
|
+
[resolved.alternateResult, packageName],
|
|
678
|
+
);
|
|
679
|
+
}
|
|
680
|
+
const packages = host.getPackagesMap();
|
|
681
|
+
if (packages.has(getTypesPackageName(packageName)))
|
|
682
|
+
return new DiagnosticDetails(
|
|
683
|
+
Diagnostics.If_the_0_package_actually_exposes_this_module_consider_sending_a_pull_request_to_amend_https_Colon_Slash_Slashgithub_com_SlashDefinitelyTyped_SlashDefinitelyTyped_Slashtree_Slashmaster_Slashtypes_Slash_1,
|
|
684
|
+
[packageName, mangleScopedPackageName(packageName)],
|
|
685
|
+
);
|
|
686
|
+
if (packages.get(packageName) === true)
|
|
687
|
+
return new DiagnosticDetails(
|
|
688
|
+
Diagnostics.If_the_0_package_actually_exposes_this_module_try_adding_a_new_declaration_d_ts_file_containing_declare_module_1,
|
|
689
|
+
[packageName, moduleReference],
|
|
690
|
+
);
|
|
691
|
+
return new DiagnosticDetails(
|
|
692
|
+
Diagnostics.Try_npm_i_save_dev_types_Slash_1_if_it_exists_or_add_a_new_declaration_d_ts_file_containing_declare_module_0,
|
|
693
|
+
[moduleReference, mangleScopedPackageName(packageName)],
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
function isSideEffectImport(node: Node): boolean {
|
|
698
|
+
let current: Node | undefined = node;
|
|
699
|
+
while (current !== undefined && current.kind !== SyntaxKind.ImportDeclaration) current = current.parent;
|
|
700
|
+
return current !== undefined && (current as ImportDeclaration).importClause === undefined;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
export function tryFindAmbientModule(c: Checker, moduleName: string, withAugmentations: boolean): Symbol | undefined {
|
|
704
|
+
if (isExternalModuleNameRelative(moduleName)) return undefined;
|
|
705
|
+
const symbol = getSymbolOfMeaning(c, c.globals, '"' + moduleName + '"', SymbolFlags.ValueModule);
|
|
706
|
+
return symbol !== undefined && withAugmentations ? getMergedSymbol(c, symbol) : symbol;
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
/** The `export =` target of a module, or the module itself. */
|
|
710
|
+
export function resolveExternalModuleSymbol(
|
|
711
|
+
c: Checker,
|
|
712
|
+
moduleSymbol: Symbol | undefined,
|
|
713
|
+
dontResolveAlias: boolean,
|
|
714
|
+
): Symbol | undefined {
|
|
715
|
+
if (moduleSymbol === undefined) return undefined;
|
|
716
|
+
const exportEquals = moduleSymbol.exports?.get(InternalSymbolName.ExportEquals);
|
|
717
|
+
if (exportEquals === undefined) return moduleSymbol;
|
|
718
|
+
return getMergedSymbol(c, resolveSymbolEx(c, exportEquals, dontResolveAlias));
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
/** The module as a namespace: removes call and construct signatures, or wraps it with a synthetic default. */
|
|
722
|
+
export function resolveESModuleSymbol(
|
|
723
|
+
c: Checker,
|
|
724
|
+
moduleSymbol: Symbol | undefined,
|
|
725
|
+
node: Node,
|
|
726
|
+
moduleSpecifier: Node,
|
|
727
|
+
): Symbol | undefined {
|
|
728
|
+
let symbol = resolveExternalModuleSymbol(c, moduleSymbol, true);
|
|
729
|
+
if (symbol !== undefined && isNonLocalAlias(symbol, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace)) {
|
|
730
|
+
const source = getSymbolOfDeclaration(c, node);
|
|
731
|
+
symbol = getMergedSymbol(
|
|
732
|
+
c,
|
|
733
|
+
source !== undefined ? resolveIndirectionAlias(c, source, symbol) : resolveAlias(c, symbol),
|
|
734
|
+
);
|
|
735
|
+
}
|
|
736
|
+
if (symbol === undefined || moduleSymbol === undefined) return symbol;
|
|
737
|
+
const referenceParent = moduleSpecifier.parent!;
|
|
738
|
+
const namespaceImport =
|
|
739
|
+
referenceParent.kind === SyntaxKind.ImportDeclaration ? getNamespaceImport(referenceParent) : undefined;
|
|
740
|
+
if (namespaceImport === undefined && !isImportCall(referenceParent)) return symbol;
|
|
741
|
+
const reference = isImportCall(referenceParent)
|
|
742
|
+
? (referenceParent as CallExpression).arguments.nodes[0]!
|
|
743
|
+
: getModuleSpecifier(referenceParent)!;
|
|
744
|
+
const type = getTypeOfSymbol(c, symbol);
|
|
745
|
+
const defaultOnlyType = getTypeWithSyntheticDefaultOnly(c, type, symbol, moduleSymbol, reference);
|
|
746
|
+
if (defaultOnlyType !== undefined) return cloneTypeAsModuleType(c, symbol, defaultOnlyType);
|
|
747
|
+
if (hasSignatures(c, type) || getPropertyOfTypeEx(c, type, InternalSymbolName.Default, true, false) !== undefined) {
|
|
748
|
+
const moduleType =
|
|
749
|
+
(type.flags & TypeFlags.StructuredType) !== 0
|
|
750
|
+
? getTypeWithSyntheticDefaultImportType(c, type, symbol, moduleSymbol, reference)
|
|
751
|
+
: createDefaultPropertyWrapperForModule(c, symbol, symbol.parent, undefined);
|
|
752
|
+
return cloneTypeAsModuleType(c, symbol, moduleType);
|
|
753
|
+
}
|
|
754
|
+
return symbol;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
function getNamespaceImport(declaration: Node): Node | undefined {
|
|
758
|
+
const clause = (declaration as ImportDeclaration).importClause;
|
|
759
|
+
const bindings = clause?.namedBindings;
|
|
760
|
+
return bindings !== undefined && bindings.kind === SyntaxKind.NamespaceImport ? bindings : undefined;
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
function hasSignatures(c: Checker, type: Type): boolean {
|
|
764
|
+
return (
|
|
765
|
+
getSignaturesOfStructuredType(c, type, SignatureKind.Call).length > 0 ||
|
|
766
|
+
getSignaturesOfStructuredType(c, type, SignatureKind.Construct).length > 0
|
|
767
|
+
);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
/**
|
|
771
|
+
* Whether the module `usage` names is a JSON module imported into an ES module under `--module node16` to
|
|
772
|
+
* `nodenext`, which Node.js gives only a default export.
|
|
773
|
+
*/
|
|
774
|
+
function isOnlyImportableAsDefault(c: Checker, usage: Node, resolvedModule: Symbol | undefined): boolean {
|
|
775
|
+
const moduleKind = getEmitModuleKind(c.compilerOptions);
|
|
776
|
+
if (moduleKind < ModuleKind.Node16 || moduleKind > ModuleKind.NodeNext) return false;
|
|
777
|
+
if (getEmitSyntaxForModuleSpecifierExpression(c, usage) !== ModuleKind.ESNext) return false;
|
|
778
|
+
const moduleSymbol = resolvedModule ?? resolveExternalModuleName(c, usage, usage, true);
|
|
779
|
+
const targetFile = moduleSymbol !== undefined ? getSourceFileOfModuleSymbol(moduleSymbol) : undefined;
|
|
780
|
+
return (
|
|
781
|
+
targetFile !== undefined &&
|
|
782
|
+
(isJsonSourceFile(targetFile) || getDeclarationFileExtension(targetFile.fileName) === '.d.json.ts')
|
|
783
|
+
);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/** The source file that declares a module: its value declaration, or its first declaration that is not an augmentation. */
|
|
787
|
+
function getSourceFileOfModuleSymbol(symbol: Symbol): SourceFile | undefined {
|
|
788
|
+
let declaration = symbol.valueDeclaration;
|
|
789
|
+
if (declaration === undefined)
|
|
790
|
+
for (const d of symbol.declarations)
|
|
791
|
+
if (!(isAmbientModule(d) && isModuleAugmentationExternal(d)) && !isGlobalScopeAugmentation(d)) {
|
|
792
|
+
declaration = d;
|
|
793
|
+
break;
|
|
794
|
+
}
|
|
795
|
+
return declaration !== undefined ? getSourceFileOfNode(declaration) : undefined;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/** For a module only importable as default, the type of its namespace: an object with only `default`. */
|
|
799
|
+
function getTypeWithSyntheticDefaultOnly(
|
|
800
|
+
c: Checker,
|
|
801
|
+
type: Type,
|
|
802
|
+
symbol: Symbol,
|
|
803
|
+
originalSymbol: Symbol,
|
|
804
|
+
moduleSpecifier: Node,
|
|
805
|
+
): Type | undefined {
|
|
806
|
+
if (!isOnlyImportableAsDefault(c, moduleSpecifier, undefined) || isErrorType(c, type)) return undefined;
|
|
807
|
+
const key = 'defaultOnly:' + type.id;
|
|
808
|
+
const cached = c.moduleTypeCache.get(key);
|
|
809
|
+
if (cached !== undefined) return cached;
|
|
810
|
+
const result = createDefaultPropertyWrapperForModule(c, symbol, originalSymbol, undefined);
|
|
811
|
+
c.moduleTypeCache.set(key, result);
|
|
812
|
+
return result;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
/** `type` with a `default` member for the module, when the module may not have a real one. */
|
|
816
|
+
function getTypeWithSyntheticDefaultImportType(
|
|
817
|
+
c: Checker,
|
|
818
|
+
type: Type,
|
|
819
|
+
symbol: Symbol,
|
|
820
|
+
originalSymbol: Symbol,
|
|
821
|
+
moduleSpecifier: Node,
|
|
822
|
+
): Type {
|
|
823
|
+
if (isErrorType(c, type)) return type;
|
|
824
|
+
const key = 'synthetic:' + type.id;
|
|
825
|
+
const cached = c.moduleTypeCache.get(key);
|
|
826
|
+
if (cached !== undefined) return cached;
|
|
827
|
+
const file = getSourceFileDeclaration(originalSymbol);
|
|
828
|
+
let result = type;
|
|
829
|
+
if (canHaveSyntheticDefault(c, file, originalSymbol, false, moduleSpecifier)) {
|
|
830
|
+
const anonymousSymbol = c.newSymbol(SymbolFlags.TypeLiteral, InternalSymbolName.Type);
|
|
831
|
+
const defaultContainingObject = createDefaultPropertyWrapperForModule(c, symbol, originalSymbol, anonymousSymbol);
|
|
832
|
+
c.symbolTypes.set(getSymbolId(anonymousSymbol), defaultContainingObject);
|
|
833
|
+
result = isValidSpreadType(c, type)
|
|
834
|
+
? getSpreadType(c, type, defaultContainingObject, anonymousSymbol, ObjectFlags.None, false)
|
|
835
|
+
: defaultContainingObject;
|
|
836
|
+
}
|
|
837
|
+
c.moduleTypeCache.set(key, result);
|
|
838
|
+
return result;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
function createDefaultPropertyWrapperForModule(
|
|
842
|
+
c: Checker,
|
|
843
|
+
symbol: Symbol,
|
|
844
|
+
originalSymbol: Symbol | undefined,
|
|
845
|
+
anonymousSymbol: Symbol | undefined,
|
|
846
|
+
): Type {
|
|
847
|
+
const defaultSymbol = c.newSymbol(SymbolFlags.Alias, InternalSymbolName.Default);
|
|
848
|
+
defaultSymbol.parent = originalSymbol;
|
|
849
|
+
const links = c.symbolLinks(defaultSymbol);
|
|
850
|
+
const id = defaultSymbol.id;
|
|
851
|
+
c.symbolNameTypes.set(id, getStringLiteralType(c, 'default'));
|
|
852
|
+
links.aliasTarget = resolveSymbol(c, symbol);
|
|
853
|
+
const members: SymbolTable = new Map<string, Symbol>();
|
|
854
|
+
members.set(InternalSymbolName.Default, defaultSymbol);
|
|
855
|
+
return createAnonymousType(c, anonymousSymbol, members, [], [], []);
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
function cloneTypeAsModuleType(c: Checker, symbol: Symbol, moduleType: Type): Symbol {
|
|
859
|
+
const result = c.newSymbol(symbol.flags, symbol.name);
|
|
860
|
+
result.declarations = symbol.declarations.slice();
|
|
861
|
+
result.valueDeclaration = symbol.valueDeclaration;
|
|
862
|
+
if (symbol.members !== undefined) result.members = new Map(symbol.members);
|
|
863
|
+
if (symbol.exports !== undefined) result.exports = new Map(symbol.exports);
|
|
864
|
+
result.parent = symbol.parent;
|
|
865
|
+
const resolved = resolveStructuredTypeMembers(c, moduleType);
|
|
866
|
+
c.symbolTypes.set(getSymbolId(result), createAnonymousType(c, result, resolved.members, [], [], resolved.indexInfos));
|
|
867
|
+
return result;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
function getSourceFileDeclaration(symbol: Symbol | undefined): SourceFile | undefined {
|
|
871
|
+
if (symbol === undefined) return undefined;
|
|
872
|
+
for (const d of symbol.declarations) if (d.kind === SyntaxKind.SourceFile) return d as SourceFile;
|
|
873
|
+
return undefined;
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
/** The module syntax of the import or export containing `usage`, or `None` for non-literals. */
|
|
877
|
+
function getEmitSyntaxForModuleSpecifierExpression(c: Checker, usage: Node): ModuleKind {
|
|
878
|
+
return isStringLiteralLike(usage)
|
|
879
|
+
? c.host.getEmitSyntaxForUsageLocation(getSourceFileOfNode(usage), usage)
|
|
880
|
+
: ModuleKind.None;
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
/** Whether a default import of the module gets the module itself, as interop helpers make it. */
|
|
884
|
+
function canHaveSyntheticDefault(
|
|
885
|
+
c: Checker,
|
|
886
|
+
file: SourceFile | undefined,
|
|
887
|
+
moduleSymbol: Symbol,
|
|
888
|
+
dontResolveAlias: boolean,
|
|
889
|
+
usage: Node,
|
|
890
|
+
): boolean {
|
|
891
|
+
const usageMode = file !== undefined ? getEmitSyntaxForModuleSpecifierExpression(c, usage) : ModuleKind.None;
|
|
892
|
+
if (file !== undefined && usageMode !== ModuleKind.None) {
|
|
893
|
+
const targetMode = c.host.getImpliedNodeFormatForEmit(file);
|
|
894
|
+
const moduleKind = getEmitModuleKind(c.compilerOptions);
|
|
895
|
+
// In Node.js, CommonJS modules always have a synthetic default when imported into ESM.
|
|
896
|
+
if (
|
|
897
|
+
usageMode === ModuleKind.ESNext &&
|
|
898
|
+
targetMode === ModuleKind.CommonJS &&
|
|
899
|
+
moduleKind >= ModuleKind.Node16 &&
|
|
900
|
+
moduleKind <= ModuleKind.NodeNext
|
|
901
|
+
)
|
|
902
|
+
return true;
|
|
903
|
+
// Two ES modules never have a synthetic default.
|
|
904
|
+
if (usageMode === ModuleKind.ESNext && targetMode === ModuleKind.ESNext) return false;
|
|
905
|
+
}
|
|
906
|
+
if (file === undefined || file.isDeclarationFile) {
|
|
907
|
+
// A syntactic default export or an `__esModule` marker rules it out.
|
|
908
|
+
const defaultExport = resolveExportByName(c, moduleSymbol, InternalSymbolName.Default, undefined, true);
|
|
909
|
+
if (defaultExport !== undefined && hasSyntacticDefault(defaultExport)) return false;
|
|
910
|
+
return resolveExportByName(c, moduleSymbol, '__esModule', undefined, dontResolveAlias) === undefined;
|
|
911
|
+
}
|
|
912
|
+
// TypeScript files are emitted with `__esModule` unless they use `export =`.
|
|
913
|
+
if (!isInJSFile(file)) return hasExportAssignmentSymbol(moduleSymbol);
|
|
914
|
+
// JavaScript files without ES module syntax or an `__esModule` marker have one.
|
|
915
|
+
return (
|
|
916
|
+
(file.externalModuleIndicator === undefined || file.externalModuleIndicator === file) &&
|
|
917
|
+
resolveExportByName(c, moduleSymbol, '__esModule', undefined, dontResolveAlias) === undefined
|
|
918
|
+
);
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
function hasSyntacticDefault(symbol: Symbol): boolean {
|
|
922
|
+
for (const d of symbol.declarations) if (isSyntacticDefault(d)) return true;
|
|
923
|
+
return false;
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
function isSyntacticDefault(node: Node): boolean {
|
|
927
|
+
return (
|
|
928
|
+
(node.kind === SyntaxKind.ExportAssignment && !(node as ExportAssignment).isExportEquals) ||
|
|
929
|
+
hasSyntacticModifier(node, ModifierFlags.Default) ||
|
|
930
|
+
node.kind === SyntaxKind.ExportSpecifier ||
|
|
931
|
+
node.kind === SyntaxKind.NamespaceExport
|
|
932
|
+
);
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
function hasExportAssignmentSymbol(moduleSymbol: Symbol): boolean {
|
|
936
|
+
return moduleSymbol.exports?.get(InternalSymbolName.ExportEquals) !== undefined;
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
function isShorthandAmbientModuleSymbol(symbol: Symbol | undefined): boolean {
|
|
940
|
+
const d = symbol?.valueDeclaration;
|
|
941
|
+
return d !== undefined && d.kind === SyntaxKind.ModuleDeclaration && (d as ModuleDeclaration).body === undefined;
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
// ---- Alias targets
|
|
945
|
+
|
|
946
|
+
/** The symbol an alias declaration refers to, without resolving further aliases. */
|
|
947
|
+
export function getTargetOfAliasDeclaration(c: Checker, node: Node | undefined): Symbol | undefined {
|
|
948
|
+
if (node === undefined) return undefined;
|
|
949
|
+
switch (node.kind) {
|
|
950
|
+
case SyntaxKind.ImportEqualsDeclaration:
|
|
951
|
+
case SyntaxKind.VariableDeclaration:
|
|
952
|
+
return getTargetOfImportEqualsDeclaration(c, node);
|
|
953
|
+
case SyntaxKind.ImportClause:
|
|
954
|
+
return getTargetOfImportClause(c, node);
|
|
955
|
+
case SyntaxKind.NamespaceImport:
|
|
956
|
+
return getTargetOfNamespaceImport(c, node);
|
|
957
|
+
case SyntaxKind.NamespaceExport:
|
|
958
|
+
return getTargetOfNamespaceExport(c, node);
|
|
959
|
+
case SyntaxKind.ImportSpecifier:
|
|
960
|
+
case SyntaxKind.BindingElement:
|
|
961
|
+
return getTargetOfImportSpecifier(c, node);
|
|
962
|
+
case SyntaxKind.ExportSpecifier:
|
|
963
|
+
return getTargetOfExportSpecifier(c, node, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace, true);
|
|
964
|
+
case SyntaxKind.ExportAssignment:
|
|
965
|
+
return withTypeOnlyMark(c, node, getTargetOfAliasLikeExpression(c, getExpression(node)!));
|
|
966
|
+
case SyntaxKind.BinaryExpression:
|
|
967
|
+
return withTypeOnlyMark(c, node, getTargetOfAliasLikeExpression(c, (node as BinaryExpression).right));
|
|
968
|
+
case SyntaxKind.NamespaceExportDeclaration: {
|
|
969
|
+
const resolved = resolveExternalModuleSymbol(c, getSymbol(node.parent!), true);
|
|
970
|
+
markSymbolOfAliasDeclarationIfTypeOnly(c, node, undefined);
|
|
971
|
+
return resolved;
|
|
972
|
+
}
|
|
973
|
+
case SyntaxKind.ShorthandPropertyAssignment:
|
|
974
|
+
return resolveEntityName(
|
|
975
|
+
c,
|
|
976
|
+
getName(node)!,
|
|
977
|
+
SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace,
|
|
978
|
+
true,
|
|
979
|
+
true,
|
|
980
|
+
undefined,
|
|
981
|
+
);
|
|
982
|
+
case SyntaxKind.PropertyAssignment:
|
|
983
|
+
return getTargetOfAliasLikeExpression(c, getInitializer(node)!);
|
|
984
|
+
case SyntaxKind.ElementAccessExpression:
|
|
985
|
+
case SyntaxKind.PropertyAccessExpression: {
|
|
986
|
+
const parent = node.parent;
|
|
987
|
+
if (
|
|
988
|
+
parent !== undefined &&
|
|
989
|
+
parent.kind === SyntaxKind.BinaryExpression &&
|
|
990
|
+
(parent as BinaryExpression).left === node &&
|
|
991
|
+
(parent as BinaryExpression).operatorToken.kind === SyntaxKind.EqualsToken
|
|
992
|
+
)
|
|
993
|
+
return getTargetOfAliasLikeExpression(c, (parent as BinaryExpression).right);
|
|
994
|
+
return undefined;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
return undefined;
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
function withTypeOnlyMark(c: Checker, node: Node, resolved: Symbol | undefined): Symbol | undefined {
|
|
1001
|
+
markSymbolOfAliasDeclarationIfTypeOnly(c, node, undefined);
|
|
1002
|
+
return resolved;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
function getTargetOfImportEqualsDeclaration(c: Checker, node: Node): Symbol | undefined {
|
|
1006
|
+
if (node.kind === SyntaxKind.VariableDeclaration || isExternalModuleImportEquals(node)) {
|
|
1007
|
+
const moduleReference = isVariableDeclarationInitializedToRequire(node)
|
|
1008
|
+
? (getInitializer(node) as CallExpression).arguments.nodes[0]!
|
|
1009
|
+
: getExpression((node as ImportEqualsDeclaration).moduleReference);
|
|
1010
|
+
const immediate = resolveExternalModuleName(c, node, moduleReference, false);
|
|
1011
|
+
const resolved = resolveExternalModuleSymbol(c, immediate, true);
|
|
1012
|
+
const moduleKind = getEmitModuleKind(c.compilerOptions);
|
|
1013
|
+
if (resolved !== undefined && moduleKind >= ModuleKind.Node20 && moduleKind <= ModuleKind.NodeNext) {
|
|
1014
|
+
const moduleExports = getExportOfModule(c, resolved, InternalSymbolName.ModuleExports, node, true);
|
|
1015
|
+
if (moduleExports !== undefined) return moduleExports;
|
|
1016
|
+
}
|
|
1017
|
+
markSymbolOfAliasDeclarationIfTypeOnly(c, node, undefined);
|
|
1018
|
+
return resolved;
|
|
1019
|
+
}
|
|
1020
|
+
const resolved = getSymbolOfPartOfRightHandSideOfImportEquals(c, (node as ImportEqualsDeclaration).moduleReference);
|
|
1021
|
+
checkAndReportErrorForResolvingImportAliasToTypeOnlySymbol(c, node as ImportEqualsDeclaration);
|
|
1022
|
+
return resolved;
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
function isExternalModuleImportEquals(node: Node): boolean {
|
|
1026
|
+
return (
|
|
1027
|
+
node.kind === SyntaxKind.ImportEqualsDeclaration &&
|
|
1028
|
+
(node as ImportEqualsDeclaration).moduleReference.kind === SyntaxKind.ExternalModuleReference
|
|
1029
|
+
);
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
/**
|
|
1033
|
+
* Resolves the entity name of `import a = b.c`: `b` alone or the left part of a longer name is a
|
|
1034
|
+
* namespace, the whole name any meaning.
|
|
1035
|
+
*/
|
|
1036
|
+
export function getSymbolOfPartOfRightHandSideOfImportEquals(c: Checker, entityName: Node): Symbol | undefined {
|
|
1037
|
+
if (
|
|
1038
|
+
entityName.kind === SyntaxKind.Identifier &&
|
|
1039
|
+
entityName.parent !== undefined &&
|
|
1040
|
+
entityName.parent.kind === SyntaxKind.QualifiedName &&
|
|
1041
|
+
(entityName.parent as QualifiedName).right === entityName
|
|
1042
|
+
)
|
|
1043
|
+
entityName = entityName.parent;
|
|
1044
|
+
if (entityName.kind === SyntaxKind.Identifier || entityName.parent!.kind === SyntaxKind.QualifiedName)
|
|
1045
|
+
return resolveEntityName(c, entityName, SymbolFlags.Namespace, false, true, undefined);
|
|
1046
|
+
return resolveEntityName(
|
|
1047
|
+
c,
|
|
1048
|
+
entityName,
|
|
1049
|
+
SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace,
|
|
1050
|
+
false,
|
|
1051
|
+
true,
|
|
1052
|
+
undefined,
|
|
1053
|
+
);
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
function checkAndReportErrorForResolvingImportAliasToTypeOnlySymbol(c: Checker, node: ImportEqualsDeclaration): void {
|
|
1057
|
+
let name: Node = node.moduleReference;
|
|
1058
|
+
for (;;) {
|
|
1059
|
+
const typeOnlyDeclaration = getTypeOnlyDeclarationOfEntityName(c, name);
|
|
1060
|
+
if (typeOnlyDeclaration !== undefined) {
|
|
1061
|
+
const isExport =
|
|
1062
|
+
typeOnlyDeclaration.kind === SyntaxKind.ExportSpecifier ||
|
|
1063
|
+
typeOnlyDeclaration.kind === SyntaxKind.ExportDeclaration;
|
|
1064
|
+
const diagnostic = c.error(
|
|
1065
|
+
node.moduleReference,
|
|
1066
|
+
isExport
|
|
1067
|
+
? Diagnostics.An_import_alias_cannot_reference_a_declaration_that_was_exported_using_export_type
|
|
1068
|
+
: Diagnostics.An_import_alias_cannot_reference_a_declaration_that_was_imported_using_import_type,
|
|
1069
|
+
);
|
|
1070
|
+
const typeOnlyName =
|
|
1071
|
+
typeOnlyDeclaration.kind === SyntaxKind.ExportDeclaration ? '*' : getText(getName(typeOnlyDeclaration)!);
|
|
1072
|
+
diagnostic.addRelatedInfo(
|
|
1073
|
+
createDiagnosticForNode(
|
|
1074
|
+
typeOnlyDeclaration,
|
|
1075
|
+
isExport ? Diagnostics.X_0_was_exported_here : Diagnostics.X_0_was_imported_here,
|
|
1076
|
+
[typeOnlyName],
|
|
1077
|
+
),
|
|
1078
|
+
);
|
|
1079
|
+
return;
|
|
1080
|
+
}
|
|
1081
|
+
if (name.kind !== SyntaxKind.QualifiedName) return;
|
|
1082
|
+
name = (name as QualifiedName).left;
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
function getTypeOnlyDeclarationOfEntityName(c: Checker, name: Node): Node | undefined {
|
|
1087
|
+
if (name.kind !== SyntaxKind.Identifier && name.kind !== SyntaxKind.QualifiedName) return undefined;
|
|
1088
|
+
const symbol = resolveEntityName(
|
|
1089
|
+
c,
|
|
1090
|
+
name,
|
|
1091
|
+
SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace,
|
|
1092
|
+
true,
|
|
1093
|
+
true,
|
|
1094
|
+
undefined,
|
|
1095
|
+
);
|
|
1096
|
+
return symbol !== undefined ? getTypeOnlyAliasDeclaration(c, symbol) : undefined;
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
function getTargetOfImportClause(c: Checker, node: Node): Symbol | undefined {
|
|
1100
|
+
const moduleSymbol = resolveExternalModuleName(c, node, getModuleSpecifier(node.parent!), false);
|
|
1101
|
+
return moduleSymbol !== undefined ? getTargetOfModuleDefault(c, moduleSymbol, node, true) : undefined;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
function getTargetOfModuleDefault(
|
|
1105
|
+
c: Checker,
|
|
1106
|
+
moduleSymbol: Symbol,
|
|
1107
|
+
node: Node,
|
|
1108
|
+
dontResolveAlias: boolean,
|
|
1109
|
+
): Symbol | undefined {
|
|
1110
|
+
const file = getSourceFileDeclaration(moduleSymbol);
|
|
1111
|
+
const specifier = getModuleSpecifierForImportOrExport(node);
|
|
1112
|
+
const exportDefaultSymbol = isShorthandAmbientModuleSymbol(moduleSymbol)
|
|
1113
|
+
? undefined
|
|
1114
|
+
: resolveExportByName(c, moduleSymbol, InternalSymbolName.Default, node, dontResolveAlias);
|
|
1115
|
+
if (specifier === undefined) return exportDefaultSymbol;
|
|
1116
|
+
const hasDefaultOnly = isOnlyImportableAsDefault(c, specifier, moduleSymbol);
|
|
1117
|
+
const hasSyntheticDefault = canHaveSyntheticDefault(c, file, moduleSymbol, dontResolveAlias, specifier);
|
|
1118
|
+
if (exportDefaultSymbol === undefined && !hasSyntheticDefault && !hasDefaultOnly) {
|
|
1119
|
+
if (node.kind === SyntaxKind.ImportClause) reportNonDefaultExport(c, moduleSymbol, node);
|
|
1120
|
+
else {
|
|
1121
|
+
const name =
|
|
1122
|
+
node.kind === SyntaxKind.ImportSpecifier || node.kind === SyntaxKind.ExportSpecifier
|
|
1123
|
+
? getPropertyNameOrName(node)
|
|
1124
|
+
: getName(node)!;
|
|
1125
|
+
errorNoModuleMemberSymbol(c, moduleSymbol, moduleSymbol, node, name);
|
|
1126
|
+
}
|
|
1127
|
+
} else if (hasSyntheticDefault || hasDefaultOnly) {
|
|
1128
|
+
// A synthetic default overrides a real `default` member without an `__esModule` marker.
|
|
1129
|
+
const resolved =
|
|
1130
|
+
resolveExternalModuleSymbol(c, moduleSymbol, dontResolveAlias) ??
|
|
1131
|
+
resolveSymbolEx(c, moduleSymbol, dontResolveAlias);
|
|
1132
|
+
markSymbolOfAliasDeclarationIfTypeOnly(c, node, undefined);
|
|
1133
|
+
return resolved;
|
|
1134
|
+
}
|
|
1135
|
+
markSymbolOfAliasDeclarationIfTypeOnly(c, node, undefined);
|
|
1136
|
+
return exportDefaultSymbol;
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
function reportNonDefaultExport(c: Checker, moduleSymbol: Symbol, node: Node): void {
|
|
1140
|
+
const local = getSymbol(node)!;
|
|
1141
|
+
if (moduleSymbol.exports?.get(local.name) !== undefined) {
|
|
1142
|
+
c.error(
|
|
1143
|
+
node,
|
|
1144
|
+
Diagnostics.Module_0_has_no_default_export_Did_you_mean_to_use_import_1_from_0_instead,
|
|
1145
|
+
symbolToString(c, moduleSymbol),
|
|
1146
|
+
symbolToString(c, local),
|
|
1147
|
+
);
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
const diagnostic = c.error(
|
|
1151
|
+
getName(node),
|
|
1152
|
+
Diagnostics.Module_0_has_no_default_export,
|
|
1153
|
+
symbolToString(c, moduleSymbol),
|
|
1154
|
+
);
|
|
1155
|
+
const exportStar = moduleSymbol.exports?.get(InternalSymbolName.ExportStar);
|
|
1156
|
+
if (exportStar === undefined) return;
|
|
1157
|
+
for (const d of exportStar.declarations) {
|
|
1158
|
+
const specifier = getModuleSpecifier(d);
|
|
1159
|
+
if (d.kind !== SyntaxKind.ExportDeclaration || specifier === undefined) continue;
|
|
1160
|
+
const resolved = resolveExternalModuleName(c, d, specifier, false);
|
|
1161
|
+
if (resolved !== undefined && resolved.exports?.get(InternalSymbolName.Default) !== undefined) {
|
|
1162
|
+
diagnostic.addRelatedInfo(
|
|
1163
|
+
createDiagnosticForNode(d, Diagnostics.X_export_Asterisk_does_not_re_export_a_default, []),
|
|
1164
|
+
);
|
|
1165
|
+
return;
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
function resolveExportByName(
|
|
1171
|
+
c: Checker,
|
|
1172
|
+
moduleSymbol: Symbol,
|
|
1173
|
+
name: string,
|
|
1174
|
+
sourceNode: Node | undefined,
|
|
1175
|
+
dontResolveAlias: boolean,
|
|
1176
|
+
): Symbol | undefined {
|
|
1177
|
+
const exportValue = moduleSymbol.exports?.get(InternalSymbolName.ExportEquals);
|
|
1178
|
+
const exportSymbol =
|
|
1179
|
+
exportValue !== undefined
|
|
1180
|
+
? getPropertyOfTypeEx(c, getTypeOfSymbol(c, exportValue), name, true, false)
|
|
1181
|
+
: moduleSymbol.exports?.get(name);
|
|
1182
|
+
const resolved = exportSymbol !== undefined ? resolveSymbolEx(c, exportSymbol, dontResolveAlias) : undefined;
|
|
1183
|
+
markSymbolOfAliasDeclarationIfTypeOnly(c, sourceNode, undefined);
|
|
1184
|
+
return resolved;
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
function getTargetOfNamespaceImport(c: Checker, node: Node): Symbol | undefined {
|
|
1188
|
+
const moduleSpecifier = getModuleSpecifierForImportOrExport(node)!;
|
|
1189
|
+
const immediate = resolveExternalModuleName(c, node, moduleSpecifier, false);
|
|
1190
|
+
const resolved = resolveESModuleSymbol(c, immediate, node, moduleSpecifier);
|
|
1191
|
+
markSymbolOfAliasDeclarationIfTypeOnly(c, node, undefined);
|
|
1192
|
+
return resolved;
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
function getTargetOfNamespaceExport(c: Checker, node: Node): Symbol | undefined {
|
|
1196
|
+
const moduleSpecifier = getModuleSpecifierForImportOrExport(node);
|
|
1197
|
+
if (moduleSpecifier === undefined) return undefined;
|
|
1198
|
+
const immediate = resolveExternalModuleName(c, node, moduleSpecifier, false);
|
|
1199
|
+
const resolved = resolveESModuleSymbol(c, immediate, node, moduleSpecifier);
|
|
1200
|
+
markSymbolOfAliasDeclarationIfTypeOnly(c, node, undefined);
|
|
1201
|
+
return resolved;
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
function getTargetOfImportSpecifier(c: Checker, node: Node): Symbol | undefined {
|
|
1205
|
+
const name = getPropertyNameOrName(node);
|
|
1206
|
+
if (node.kind === SyntaxKind.ImportSpecifier && moduleExportNameIsDefault(name)) {
|
|
1207
|
+
const specifier = getModuleSpecifierForImportOrExport(node);
|
|
1208
|
+
if (specifier !== undefined) {
|
|
1209
|
+
const moduleSymbol = resolveExternalModuleName(c, node, specifier, false);
|
|
1210
|
+
if (moduleSymbol !== undefined) return getTargetOfModuleDefault(c, moduleSymbol, node, true);
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
const root = node.kind === SyntaxKind.BindingElement ? getRootDeclaration(node) : node.parent!.parent!.parent!;
|
|
1214
|
+
const resolved = getExternalModuleMember(c, root, node, true);
|
|
1215
|
+
markSymbolOfAliasDeclarationIfTypeOnly(c, node, undefined);
|
|
1216
|
+
return resolved;
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
/** The export `specifier` names from the module of the import or export declaration `node`. */
|
|
1220
|
+
export function getExternalModuleMember(
|
|
1221
|
+
c: Checker,
|
|
1222
|
+
node: Node,
|
|
1223
|
+
specifier: Node,
|
|
1224
|
+
dontResolveAlias: boolean,
|
|
1225
|
+
): Symbol | undefined {
|
|
1226
|
+
const moduleSpecifier = isVariableDeclarationInitializedToRequire(node)
|
|
1227
|
+
? (getInitializer(node) as CallExpression).arguments.nodes[0]!
|
|
1228
|
+
: getExternalModuleName(node);
|
|
1229
|
+
const moduleSymbol = resolveExternalModuleName(c, node, moduleSpecifier, false);
|
|
1230
|
+
const name =
|
|
1231
|
+
specifier.kind === SyntaxKind.PropertyAccessExpression ? getName(specifier)! : getPropertyNameOrName(specifier);
|
|
1232
|
+
if (name.kind !== SyntaxKind.Identifier && name.kind !== SyntaxKind.StringLiteral) return undefined;
|
|
1233
|
+
const nameText = getText(name);
|
|
1234
|
+
const targetSymbol =
|
|
1235
|
+
moduleSpecifier !== undefined ? resolveESModuleSymbol(c, moduleSymbol, specifier, moduleSpecifier) : undefined;
|
|
1236
|
+
// The empty string is a valid module export name.
|
|
1237
|
+
if (
|
|
1238
|
+
targetSymbol === undefined ||
|
|
1239
|
+
moduleSymbol === undefined ||
|
|
1240
|
+
(nameText === '' && name.kind !== SyntaxKind.StringLiteral)
|
|
1241
|
+
)
|
|
1242
|
+
return undefined;
|
|
1243
|
+
if (isShorthandAmbientModuleSymbol(moduleSymbol)) return moduleSymbol;
|
|
1244
|
+
// An `export =` module exports the members of its target's type.
|
|
1245
|
+
const exportEquals = hasExportAssignmentSymbol(moduleSymbol);
|
|
1246
|
+
let symbolFromVariable = exportEquals
|
|
1247
|
+
? getPropertyOfTypeEx(c, getTypeOfSymbol(c, targetSymbol), nameText, true, false)
|
|
1248
|
+
: getPropertyOfVariable(c, targetSymbol, nameText);
|
|
1249
|
+
if (symbolFromVariable !== undefined) symbolFromVariable = resolveSymbolEx(c, symbolFromVariable, dontResolveAlias);
|
|
1250
|
+
// Types and namespaces of an `export =` module live on the module symbol.
|
|
1251
|
+
let symbolFromModule = getExportOfModule(
|
|
1252
|
+
c,
|
|
1253
|
+
exportEquals ? moduleSymbol : targetSymbol,
|
|
1254
|
+
nameText,
|
|
1255
|
+
specifier,
|
|
1256
|
+
dontResolveAlias,
|
|
1257
|
+
);
|
|
1258
|
+
if (
|
|
1259
|
+
symbolFromModule === undefined &&
|
|
1260
|
+
nameText === InternalSymbolName.Default &&
|
|
1261
|
+
(isOnlyImportableAsDefault(c, moduleSpecifier!, moduleSymbol) ||
|
|
1262
|
+
canHaveSyntheticDefault(
|
|
1263
|
+
c,
|
|
1264
|
+
getSourceFileDeclaration(moduleSymbol),
|
|
1265
|
+
moduleSymbol,
|
|
1266
|
+
dontResolveAlias,
|
|
1267
|
+
moduleSpecifier!,
|
|
1268
|
+
))
|
|
1269
|
+
)
|
|
1270
|
+
symbolFromModule =
|
|
1271
|
+
resolveExternalModuleSymbol(c, moduleSymbol, dontResolveAlias) ??
|
|
1272
|
+
resolveSymbolEx(c, moduleSymbol, dontResolveAlias);
|
|
1273
|
+
const symbol =
|
|
1274
|
+
symbolFromModule === undefined
|
|
1275
|
+
? symbolFromVariable
|
|
1276
|
+
: symbolFromVariable === undefined
|
|
1277
|
+
? symbolFromModule
|
|
1278
|
+
: combineValueAndTypeSymbols(c, symbolFromVariable, symbolFromModule);
|
|
1279
|
+
if (
|
|
1280
|
+
(specifier.kind === SyntaxKind.ImportSpecifier || specifier.kind === SyntaxKind.ExportSpecifier) &&
|
|
1281
|
+
nameText !== InternalSymbolName.Default &&
|
|
1282
|
+
isOnlyImportableAsDefault(c, moduleSpecifier!, moduleSymbol)
|
|
1283
|
+
)
|
|
1284
|
+
c.error(
|
|
1285
|
+
name,
|
|
1286
|
+
Diagnostics.Named_imports_from_a_JSON_file_into_an_ECMAScript_module_are_not_allowed_when_module_is_set_to_0,
|
|
1287
|
+
moduleKindName(getEmitModuleKind(c.compilerOptions)),
|
|
1288
|
+
);
|
|
1289
|
+
else if (symbol === undefined) errorNoModuleMemberSymbol(c, moduleSymbol, targetSymbol, node, name);
|
|
1290
|
+
return symbol;
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
function getPropertyOfVariable(c: Checker, symbol: Symbol, name: string): Symbol | undefined {
|
|
1294
|
+
if ((symbol.flags & SymbolFlags.Variable) === 0) return undefined;
|
|
1295
|
+
const declaration = symbol.valueDeclaration;
|
|
1296
|
+
const typeAnnotation = declaration !== undefined ? getType(declaration) : undefined;
|
|
1297
|
+
if (typeAnnotation === undefined) return undefined;
|
|
1298
|
+
return resolveSymbol(c, getPropertyOfType(c, getTypeFromTypeNode(c, typeAnnotation), name));
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
/** A symbol with the value side of `valueSymbol` and the type and namespace sides of `typeSymbol`. */
|
|
1302
|
+
function combineValueAndTypeSymbols(c: Checker, valueSymbol: Symbol, typeSymbol: Symbol): Symbol {
|
|
1303
|
+
if (valueSymbol === c.unknownSymbol && typeSymbol === c.unknownSymbol) return c.unknownSymbol;
|
|
1304
|
+
if ((typeSymbol.flags & SymbolFlags.Value) !== 0) return typeSymbol;
|
|
1305
|
+
if ((valueSymbol.flags & (SymbolFlags.Type | SymbolFlags.Namespace)) !== 0) return valueSymbol;
|
|
1306
|
+
const result = c.newSymbol(valueSymbol.flags | typeSymbol.flags, valueSymbol.name);
|
|
1307
|
+
const declarations = valueSymbol.declarations.slice();
|
|
1308
|
+
for (const d of typeSymbol.declarations) if (!declarations.includes(d)) declarations.push(d);
|
|
1309
|
+
result.declarations = declarations;
|
|
1310
|
+
result.parent = valueSymbol.parent ?? typeSymbol.parent;
|
|
1311
|
+
result.valueDeclaration = valueSymbol.valueDeclaration;
|
|
1312
|
+
if (typeSymbol.members !== undefined) result.members = new Map(typeSymbol.members);
|
|
1313
|
+
if (valueSymbol.exports !== undefined) result.exports = new Map(valueSymbol.exports);
|
|
1314
|
+
return result;
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
function getExportOfModule(
|
|
1318
|
+
c: Checker,
|
|
1319
|
+
symbol: Symbol,
|
|
1320
|
+
nameText: string,
|
|
1321
|
+
specifier: Node,
|
|
1322
|
+
dontResolveAlias: boolean,
|
|
1323
|
+
): Symbol | undefined {
|
|
1324
|
+
if ((symbol.flags & SymbolFlags.Module) === 0) return undefined;
|
|
1325
|
+
const exportSymbol = getExportsOfSymbol(c, symbol).get(nameText);
|
|
1326
|
+
const resolved = exportSymbol !== undefined ? resolveSymbolEx(c, exportSymbol, dontResolveAlias) : undefined;
|
|
1327
|
+
const exportStarDeclaration = c.symbolLinks(symbol).typeOnlyExportStarMap?.get(nameText);
|
|
1328
|
+
markSymbolOfAliasDeclarationIfTypeOnly(c, specifier, exportStarDeclaration);
|
|
1329
|
+
return resolved;
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
function errorNoModuleMemberSymbol(
|
|
1333
|
+
c: Checker,
|
|
1334
|
+
moduleSymbol: Symbol,
|
|
1335
|
+
targetSymbol: Symbol,
|
|
1336
|
+
node: Node,
|
|
1337
|
+
name: Node,
|
|
1338
|
+
): void {
|
|
1339
|
+
const moduleName = getFullyQualifiedName(c, moduleSymbol, node);
|
|
1340
|
+
const declarationName = declarationNameToString(getSourceFileOfNode(name), name);
|
|
1341
|
+
const suggestion =
|
|
1342
|
+
name.kind === SyntaxKind.Identifier
|
|
1343
|
+
? getSpellingSuggestionForName(
|
|
1344
|
+
c,
|
|
1345
|
+
getText(name),
|
|
1346
|
+
Array.from(getExportsOfModule(c, targetSymbol).values()),
|
|
1347
|
+
SymbolFlags.ModuleMember,
|
|
1348
|
+
)
|
|
1349
|
+
: undefined;
|
|
1350
|
+
if (suggestion !== undefined) {
|
|
1351
|
+
const suggestionName = symbolToString(c, suggestion);
|
|
1352
|
+
const diagnostic = c.error(
|
|
1353
|
+
name,
|
|
1354
|
+
Diagnostics.X_0_has_no_exported_member_named_1_Did_you_mean_2,
|
|
1355
|
+
moduleName,
|
|
1356
|
+
declarationName,
|
|
1357
|
+
suggestionName,
|
|
1358
|
+
);
|
|
1359
|
+
if (suggestion.valueDeclaration !== undefined)
|
|
1360
|
+
diagnostic.addRelatedInfo(
|
|
1361
|
+
createDiagnosticForNode(suggestion.valueDeclaration, Diagnostics.X_0_is_declared_here, [suggestionName]),
|
|
1362
|
+
);
|
|
1363
|
+
return;
|
|
1364
|
+
}
|
|
1365
|
+
if (moduleSymbol.exports?.get(InternalSymbolName.Default) !== undefined) {
|
|
1366
|
+
c.error(
|
|
1367
|
+
name,
|
|
1368
|
+
Diagnostics.Module_0_has_no_exported_member_1_Did_you_mean_to_use_import_1_from_0_instead,
|
|
1369
|
+
moduleName,
|
|
1370
|
+
declarationName,
|
|
1371
|
+
);
|
|
1372
|
+
return;
|
|
1373
|
+
}
|
|
1374
|
+
reportNonExportedMember(c, name, declarationName, moduleSymbol, moduleName);
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
function reportNonExportedMember(
|
|
1378
|
+
c: Checker,
|
|
1379
|
+
name: Node,
|
|
1380
|
+
declarationName: string,
|
|
1381
|
+
moduleSymbol: Symbol,
|
|
1382
|
+
moduleName: string,
|
|
1383
|
+
): void {
|
|
1384
|
+
const locals = moduleSymbol.valueDeclaration !== undefined ? getLocals(moduleSymbol.valueDeclaration) : undefined;
|
|
1385
|
+
const localSymbol = locals?.get(getText(name));
|
|
1386
|
+
if (localSymbol === undefined) {
|
|
1387
|
+
c.error(name, Diagnostics.Module_0_has_no_exported_member_1, moduleName, declarationName);
|
|
1388
|
+
return;
|
|
1389
|
+
}
|
|
1390
|
+
const exports = moduleSymbol.exports;
|
|
1391
|
+
const exportEquals = exports?.get(InternalSymbolName.ExportEquals);
|
|
1392
|
+
if (exportEquals !== undefined) {
|
|
1393
|
+
if (isSameReference(c, exportEquals, localSymbol))
|
|
1394
|
+
reportInvalidImportEqualsExportMember(c, name, declarationName, moduleName);
|
|
1395
|
+
else c.error(name, Diagnostics.Module_0_has_no_exported_member_1, moduleName, declarationName);
|
|
1396
|
+
return;
|
|
1397
|
+
}
|
|
1398
|
+
let exportedSymbol: Symbol | undefined;
|
|
1399
|
+
if (exports !== undefined)
|
|
1400
|
+
for (const s of exports.values())
|
|
1401
|
+
if (isSameReference(c, s, localSymbol)) {
|
|
1402
|
+
exportedSymbol = s;
|
|
1403
|
+
break;
|
|
1404
|
+
}
|
|
1405
|
+
const diagnostic =
|
|
1406
|
+
exportedSymbol !== undefined
|
|
1407
|
+
? c.error(
|
|
1408
|
+
name,
|
|
1409
|
+
Diagnostics.Module_0_declares_1_locally_but_it_is_exported_as_2,
|
|
1410
|
+
moduleName,
|
|
1411
|
+
declarationName,
|
|
1412
|
+
symbolToString(c, exportedSymbol),
|
|
1413
|
+
)
|
|
1414
|
+
: c.error(name, Diagnostics.Module_0_declares_1_locally_but_it_is_not_exported, moduleName, declarationName);
|
|
1415
|
+
for (let i = 0; i < localSymbol.declarations.length; i++)
|
|
1416
|
+
diagnostic.addRelatedInfo(
|
|
1417
|
+
createDiagnosticForNode(
|
|
1418
|
+
localSymbol.declarations[i]!,
|
|
1419
|
+
i === 0 ? Diagnostics.X_0_is_declared_here : Diagnostics.X_and_here,
|
|
1420
|
+
[declarationName],
|
|
1421
|
+
),
|
|
1422
|
+
);
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
function isSameReference(c: Checker, s1: Symbol, s2: Symbol): boolean {
|
|
1426
|
+
return (
|
|
1427
|
+
getMergedSymbol(c, resolveSymbol(c, getMergedSymbol(c, s1))!) ===
|
|
1428
|
+
getMergedSymbol(c, resolveSymbol(c, getMergedSymbol(c, s2))!)
|
|
1429
|
+
);
|
|
1430
|
+
}
|
|
1431
|
+
|
|
1432
|
+
function reportInvalidImportEqualsExportMember(
|
|
1433
|
+
c: Checker,
|
|
1434
|
+
name: Node,
|
|
1435
|
+
declarationName: string,
|
|
1436
|
+
moduleName: string,
|
|
1437
|
+
): void {
|
|
1438
|
+
if (getEmitModuleKind(c.compilerOptions) >= ModuleKind.ES2015)
|
|
1439
|
+
c.error(name, Diagnostics.X_0_can_only_be_imported_by_using_a_default_import, declarationName);
|
|
1440
|
+
else if (isInJSFile(name))
|
|
1441
|
+
c.error(
|
|
1442
|
+
name,
|
|
1443
|
+
Diagnostics.X_0_can_only_be_imported_by_using_a_require_call_or_by_using_a_default_import,
|
|
1444
|
+
declarationName,
|
|
1445
|
+
);
|
|
1446
|
+
else
|
|
1447
|
+
c.error(
|
|
1448
|
+
name,
|
|
1449
|
+
Diagnostics.X_0_can_only_be_imported_by_using_import_1_require_2_or_a_default_import,
|
|
1450
|
+
declarationName,
|
|
1451
|
+
declarationName,
|
|
1452
|
+
moduleName,
|
|
1453
|
+
);
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
export function getTargetOfExportSpecifier(
|
|
1457
|
+
c: Checker,
|
|
1458
|
+
node: Node,
|
|
1459
|
+
meaning: SymbolFlags,
|
|
1460
|
+
dontResolveAlias: boolean,
|
|
1461
|
+
): Symbol | undefined {
|
|
1462
|
+
const name = getPropertyNameOrName(node);
|
|
1463
|
+
if (moduleExportNameIsDefault(name)) {
|
|
1464
|
+
const specifier = getModuleSpecifierForImportOrExport(node);
|
|
1465
|
+
if (specifier !== undefined) {
|
|
1466
|
+
const moduleSymbol = resolveExternalModuleName(c, node, specifier, false);
|
|
1467
|
+
if (moduleSymbol !== undefined) return getTargetOfModuleDefault(c, moduleSymbol, node, dontResolveAlias);
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
const exportDeclaration = node.parent!.parent!;
|
|
1471
|
+
let resolved: Symbol | undefined;
|
|
1472
|
+
if ((exportDeclaration as ExportDeclaration).moduleSpecifier !== undefined)
|
|
1473
|
+
resolved = getExternalModuleMember(c, exportDeclaration, node, dontResolveAlias);
|
|
1474
|
+
else if (name.kind !== SyntaxKind.StringLiteral)
|
|
1475
|
+
resolved = resolveEntityName(c, name, meaning, false, dontResolveAlias, undefined);
|
|
1476
|
+
markSymbolOfAliasDeclarationIfTypeOnly(c, node, undefined);
|
|
1477
|
+
return resolved;
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
function getTargetOfAliasLikeExpression(c: Checker, expression: Node): Symbol | undefined {
|
|
1481
|
+
if (expression.kind === SyntaxKind.ClassExpression) return checkExpressionCached(c, expression).symbol;
|
|
1482
|
+
if (!isEntityNameOrEntityNameExpression(expression)) return undefined;
|
|
1483
|
+
const aliasLike = resolveEntityName(
|
|
1484
|
+
c,
|
|
1485
|
+
expression,
|
|
1486
|
+
SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace,
|
|
1487
|
+
true,
|
|
1488
|
+
true,
|
|
1489
|
+
undefined,
|
|
1490
|
+
);
|
|
1491
|
+
if (aliasLike !== undefined) return aliasLike;
|
|
1492
|
+
checkExpressionCached(c, expression);
|
|
1493
|
+
return c.resolvedSymbols.get(expression.id);
|
|
1494
|
+
}
|
|
1495
|
+
|
|
1496
|
+
function isEntityNameOrEntityNameExpression(node: Node): boolean {
|
|
1497
|
+
if (node.kind === SyntaxKind.Identifier) return true;
|
|
1498
|
+
if (node.kind === SyntaxKind.QualifiedName) return true;
|
|
1499
|
+
if (node.kind === SyntaxKind.PropertyAccessExpression) {
|
|
1500
|
+
const name = getName(node);
|
|
1501
|
+
return (
|
|
1502
|
+
name !== undefined &&
|
|
1503
|
+
name.kind === SyntaxKind.Identifier &&
|
|
1504
|
+
isEntityNameOrEntityNameExpression(getExpression(node)!)
|
|
1505
|
+
);
|
|
1506
|
+
}
|
|
1507
|
+
return false;
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
function getPropertyNameOrName(node: Node): Node {
|
|
1511
|
+
return getPropertyName(node) ?? getName(node)!;
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
function getModuleSpecifierForImportOrExport(node: Node): Node | undefined {
|
|
1515
|
+
switch (node.kind) {
|
|
1516
|
+
case SyntaxKind.ImportClause:
|
|
1517
|
+
return getModuleSpecifierOfDeclaration(node.parent!);
|
|
1518
|
+
case SyntaxKind.ImportEqualsDeclaration: {
|
|
1519
|
+
const reference = (node as ImportEqualsDeclaration).moduleReference;
|
|
1520
|
+
return reference.kind === SyntaxKind.ExternalModuleReference ? getExpression(reference) : undefined;
|
|
1521
|
+
}
|
|
1522
|
+
case SyntaxKind.NamespaceImport:
|
|
1523
|
+
return getModuleSpecifierOfDeclaration(node.parent!.parent!);
|
|
1524
|
+
case SyntaxKind.ImportSpecifier:
|
|
1525
|
+
return getModuleSpecifierOfDeclaration(node.parent!.parent!.parent!);
|
|
1526
|
+
case SyntaxKind.NamespaceExport:
|
|
1527
|
+
return getModuleSpecifierOfDeclaration(node.parent!);
|
|
1528
|
+
case SyntaxKind.ExportSpecifier:
|
|
1529
|
+
return getModuleSpecifierOfDeclaration(node.parent!.parent!);
|
|
1530
|
+
}
|
|
1531
|
+
return undefined;
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
function getModuleSpecifierOfDeclaration(node: Node): Node | undefined {
|
|
1535
|
+
if (node.kind === SyntaxKind.ExportDeclaration) return (node as ExportDeclaration).moduleSpecifier;
|
|
1536
|
+
return getModuleSpecifier(node);
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
// ---- Type-only aliases
|
|
1540
|
+
|
|
1541
|
+
/** Whether the declaration itself is written `import type` or `export type`. */
|
|
1542
|
+
export function isTypeOnlyImportOrExportDeclaration(node: Node): boolean {
|
|
1543
|
+
switch (node.kind) {
|
|
1544
|
+
case SyntaxKind.ImportSpecifier:
|
|
1545
|
+
return getIsTypeOnly(node) || isTypeOnlyImportClause(node.parent!.parent!);
|
|
1546
|
+
case SyntaxKind.NamespaceImport:
|
|
1547
|
+
return isTypeOnlyImportClause(node.parent!);
|
|
1548
|
+
case SyntaxKind.ImportClause:
|
|
1549
|
+
return isTypeOnlyImportClause(node);
|
|
1550
|
+
case SyntaxKind.ImportEqualsDeclaration:
|
|
1551
|
+
return getIsTypeOnly(node);
|
|
1552
|
+
case SyntaxKind.ExportSpecifier:
|
|
1553
|
+
return getIsTypeOnly(node) || getIsTypeOnly(node.parent!.parent!);
|
|
1554
|
+
case SyntaxKind.ExportDeclaration: {
|
|
1555
|
+
const declaration = node as ExportDeclaration;
|
|
1556
|
+
return (
|
|
1557
|
+
declaration.isTypeOnly && declaration.moduleSpecifier !== undefined && declaration.exportClause === undefined
|
|
1558
|
+
);
|
|
1559
|
+
}
|
|
1560
|
+
case SyntaxKind.NamespaceExport:
|
|
1561
|
+
return getIsTypeOnly(node.parent!);
|
|
1562
|
+
}
|
|
1563
|
+
return false;
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
function isTypeOnlyImportClause(node: Node): boolean {
|
|
1567
|
+
return node.kind === SyntaxKind.ImportClause && (node as ImportClause).phaseModifier === SyntaxKind.TypeKeyword;
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
/**
|
|
1571
|
+
* Marks an alias declared `import type`/`export type`, or reached through a type-only `export *`,
|
|
1572
|
+
* as type-only; returns whether it is.
|
|
1573
|
+
*/
|
|
1574
|
+
function markSymbolOfAliasDeclarationIfTypeOnly(
|
|
1575
|
+
c: Checker,
|
|
1576
|
+
aliasDeclaration: Node | undefined,
|
|
1577
|
+
exportStarDeclaration: Node | undefined,
|
|
1578
|
+
): boolean {
|
|
1579
|
+
if (aliasDeclaration === undefined) return false;
|
|
1580
|
+
const sourceSymbol = getSymbol(aliasDeclaration);
|
|
1581
|
+
if (sourceSymbol === undefined) return false;
|
|
1582
|
+
const links = c.symbolLinks(getMergedSymbol(c, sourceSymbol));
|
|
1583
|
+
if (links.typeOnlyDeclaration === undefined && isTypeOnlyImportOrExportDeclaration(aliasDeclaration)) {
|
|
1584
|
+
links.typeOnlyDeclaration = aliasDeclaration;
|
|
1585
|
+
return true;
|
|
1586
|
+
}
|
|
1587
|
+
if (links.typeOnlyDeclaration === undefined && exportStarDeclaration !== undefined) {
|
|
1588
|
+
links.typeOnlyDeclaration = exportStarDeclaration;
|
|
1589
|
+
return true;
|
|
1590
|
+
}
|
|
1591
|
+
return links.typeOnlyDeclaration !== undefined;
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
/** Marks the alias declaration an entity name is part of, if it is type-only. */
|
|
1595
|
+
export function markAliasDeclarationOfNameIfTypeOnly(c: Checker, name: Node): void {
|
|
1596
|
+
let current = name;
|
|
1597
|
+
while (current.parent !== undefined && current.parent.kind === SyntaxKind.QualifiedName) current = current.parent;
|
|
1598
|
+
const parent = current.parent;
|
|
1599
|
+
if (parent === undefined) return;
|
|
1600
|
+
switch (parent.kind) {
|
|
1601
|
+
case SyntaxKind.ImportClause:
|
|
1602
|
+
case SyntaxKind.ImportSpecifier:
|
|
1603
|
+
case SyntaxKind.NamespaceImport:
|
|
1604
|
+
case SyntaxKind.ExportSpecifier:
|
|
1605
|
+
case SyntaxKind.ExportAssignment:
|
|
1606
|
+
case SyntaxKind.ImportEqualsDeclaration:
|
|
1607
|
+
case SyntaxKind.NamespaceExport:
|
|
1608
|
+
markSymbolOfAliasDeclarationIfTypeOnly(c, parent, undefined);
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
/** Resolves an alias whose target is an alias, carrying over the target's type-only declaration. */
|
|
1613
|
+
export function resolveIndirectionAlias(c: Checker, source: Symbol, target: Symbol): Symbol {
|
|
1614
|
+
const result = getMergedSymbol(c, resolveAlias(c, target));
|
|
1615
|
+
const targetDeclaration = c.symbolLinks(target).typeOnlyDeclaration;
|
|
1616
|
+
if (targetDeclaration !== undefined) {
|
|
1617
|
+
const sourceLinks = c.symbolLinks(source);
|
|
1618
|
+
if (sourceLinks.typeOnlyDeclaration === undefined) sourceLinks.typeOnlyDeclaration = targetDeclaration;
|
|
1619
|
+
}
|
|
1620
|
+
return result;
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1623
|
+
export function getTypeOnlyAliasDeclaration(c: Checker, symbol: Symbol): Node | undefined {
|
|
1624
|
+
if ((symbol.flags & SymbolFlags.Alias) === 0) return undefined;
|
|
1625
|
+
resolveAlias(c, symbol);
|
|
1626
|
+
return c.symbolLinks(symbol).typeOnlyDeclaration;
|
|
1627
|
+
}
|
|
1628
|
+
|
|
1629
|
+
/** The first type-only declaration along the alias chain of `symbol` before one with `meaning`. */
|
|
1630
|
+
export function getTypeOnlyAliasDeclarationOfMeaning(
|
|
1631
|
+
c: Checker,
|
|
1632
|
+
symbol: Symbol,
|
|
1633
|
+
meaning: SymbolFlags,
|
|
1634
|
+
): Node | undefined {
|
|
1635
|
+
while ((symbol.flags & SymbolFlags.Alias) !== 0 && (symbol.flags & meaning) === 0) {
|
|
1636
|
+
const resolved = resolveAlias(c, symbol);
|
|
1637
|
+
const declaration = c.symbolLinks(symbol).typeOnlyDeclaration;
|
|
1638
|
+
if (declaration !== undefined) return declaration;
|
|
1639
|
+
symbol = resolved;
|
|
1640
|
+
}
|
|
1641
|
+
return undefined;
|
|
1642
|
+
}
|
|
1643
|
+
|
|
1644
|
+
function addTypeOnlyDeclarationRelatedInfo(
|
|
1645
|
+
diagnostic: Diagnostic,
|
|
1646
|
+
typeOnlyDeclaration: Node | undefined,
|
|
1647
|
+
name: string,
|
|
1648
|
+
): void {
|
|
1649
|
+
if (typeOnlyDeclaration === undefined) return;
|
|
1650
|
+
const isExport =
|
|
1651
|
+
typeOnlyDeclaration.kind === SyntaxKind.ExportSpecifier ||
|
|
1652
|
+
typeOnlyDeclaration.kind === SyntaxKind.ExportDeclaration ||
|
|
1653
|
+
typeOnlyDeclaration.kind === SyntaxKind.NamespaceExport;
|
|
1654
|
+
diagnostic.addRelatedInfo(
|
|
1655
|
+
createDiagnosticForNode(
|
|
1656
|
+
typeOnlyDeclaration,
|
|
1657
|
+
isExport ? Diagnostics.X_0_was_exported_here : Diagnostics.X_0_was_imported_here,
|
|
1658
|
+
[name],
|
|
1659
|
+
),
|
|
1660
|
+
);
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
/** Reports a value use of a name that only resolves to a value through a type-only import or export. */
|
|
1664
|
+
export function checkTypeOnlyAliasUse(c: Checker, location: Node, result: Symbol, name: string): void {
|
|
1665
|
+
if (
|
|
1666
|
+
(result.flags & SymbolFlags.Alias) === 0 ||
|
|
1667
|
+
(result.flags & SymbolFlags.Value) !== 0 ||
|
|
1668
|
+
isValidTypeOnlyAliasUseSite(location)
|
|
1669
|
+
)
|
|
1670
|
+
return;
|
|
1671
|
+
const typeOnlyDeclaration = getTypeOnlyAliasDeclarationOfMeaning(c, result, SymbolFlags.Value);
|
|
1672
|
+
if (typeOnlyDeclaration === undefined) return;
|
|
1673
|
+
const isExport =
|
|
1674
|
+
typeOnlyDeclaration.kind === SyntaxKind.ExportSpecifier ||
|
|
1675
|
+
typeOnlyDeclaration.kind === SyntaxKind.ExportDeclaration ||
|
|
1676
|
+
typeOnlyDeclaration.kind === SyntaxKind.NamespaceExport;
|
|
1677
|
+
const diagnostic = c.error(
|
|
1678
|
+
location,
|
|
1679
|
+
isExport
|
|
1680
|
+
? Diagnostics.X_0_cannot_be_used_as_a_value_because_it_was_exported_using_export_type
|
|
1681
|
+
: Diagnostics.X_0_cannot_be_used_as_a_value_because_it_was_imported_using_import_type,
|
|
1682
|
+
name,
|
|
1683
|
+
);
|
|
1684
|
+
addTypeOnlyDeclarationRelatedInfo(diagnostic, typeOnlyDeclaration, name);
|
|
1685
|
+
}
|
|
1686
|
+
|
|
1687
|
+
export function isValidTypeOnlyAliasUseSite(useSite: Node): boolean {
|
|
1688
|
+
return (
|
|
1689
|
+
(useSite.flags & (NodeFlags.Ambient | NodeFlags.JSDoc)) !== 0 ||
|
|
1690
|
+
isPartOfTypeQuery(useSite) ||
|
|
1691
|
+
isIdentifierInNonEmittingHeritageClause(useSite) ||
|
|
1692
|
+
isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(useSite) ||
|
|
1693
|
+
!(isExpressionNode(useSite) || isShorthandPropertyNameUseSite(useSite))
|
|
1694
|
+
);
|
|
1695
|
+
}
|
|
1696
|
+
|
|
1697
|
+
function isIdentifierInNonEmittingHeritageClause(node: Node): boolean {
|
|
1698
|
+
if (node.kind !== SyntaxKind.Identifier) return false;
|
|
1699
|
+
let parent = node.parent!;
|
|
1700
|
+
while (parent.kind === SyntaxKind.PropertyAccessExpression || parent.kind === SyntaxKind.ExpressionWithTypeArguments)
|
|
1701
|
+
parent = parent.parent!;
|
|
1702
|
+
return (
|
|
1703
|
+
parent.kind === SyntaxKind.HeritageClause &&
|
|
1704
|
+
((parent as HeritageClause).token === SyntaxKind.ImplementsKeyword ||
|
|
1705
|
+
parent.parent!.kind === SyntaxKind.InterfaceDeclaration)
|
|
1706
|
+
);
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
function isPartOfPossiblyValidTypeOrAbstractComputedPropertyName(node: Node): boolean {
|
|
1710
|
+
let current = node;
|
|
1711
|
+
while (current.kind === SyntaxKind.Identifier || current.kind === SyntaxKind.PropertyAccessExpression)
|
|
1712
|
+
current = current.parent!;
|
|
1713
|
+
if (current.kind !== SyntaxKind.ComputedPropertyName) return false;
|
|
1714
|
+
if (hasSyntacticModifier(current.parent!, ModifierFlags.Abstract)) return true;
|
|
1715
|
+
const container = current.parent!.parent!.kind;
|
|
1716
|
+
return container === SyntaxKind.InterfaceDeclaration || container === SyntaxKind.TypeLiteral;
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1719
|
+
function isShorthandPropertyNameUseSite(useSite: Node): boolean {
|
|
1720
|
+
return (
|
|
1721
|
+
useSite.kind === SyntaxKind.Identifier &&
|
|
1722
|
+
useSite.parent!.kind === SyntaxKind.ShorthandPropertyAssignment &&
|
|
1723
|
+
getName(useSite.parent!) === useSite
|
|
1724
|
+
);
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
// ---- Referenced aliases
|
|
1728
|
+
|
|
1729
|
+
/**
|
|
1730
|
+
* Whether aliases are marked as referenced, which decides the imports emit keeps. Under
|
|
1731
|
+
* verbatimModuleSyntax, emit keeps every import that isn't type-only, so they aren't.
|
|
1732
|
+
*/
|
|
1733
|
+
function marksReferencedAliases(c: Checker): boolean {
|
|
1734
|
+
return c.compilerOptions.verbatimModuleSyntax !== true;
|
|
1735
|
+
}
|
|
1736
|
+
|
|
1737
|
+
/** Marks the aliases an identifier in an expression uses as referenced. */
|
|
1738
|
+
export function markIdentifierAliasReferencedInExpression(c: Checker, node: Node): void {
|
|
1739
|
+
if (!marksReferencedAliases(c)) return;
|
|
1740
|
+
if ((node.flags & NodeFlags.Ambient) !== 0 || !shouldMarkIdentifierAliasReferenced(node)) return;
|
|
1741
|
+
markIdentifierAliasReferenced(c, node);
|
|
1742
|
+
}
|
|
1743
|
+
|
|
1744
|
+
/** Resolves the alias side of a referenced symbol, which may also be a local variable. */
|
|
1745
|
+
export function resolveAliasOfReference(c: Checker, symbol: Symbol): void {
|
|
1746
|
+
if ((symbol.flags & SymbolFlags.Alias) !== 0 && getDeclarationOfAliasSymbol(symbol) !== undefined)
|
|
1747
|
+
resolveAlias(c, symbol);
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
function shouldMarkIdentifierAliasReferenced(node: Node): boolean {
|
|
1751
|
+
const parent = node.parent;
|
|
1752
|
+
if (parent === undefined) return true;
|
|
1753
|
+
// The left of a property access is marked with the property.
|
|
1754
|
+
if (parent.kind === SyntaxKind.PropertyAccessExpression && getExpression(parent) === node) return false;
|
|
1755
|
+
// Names of type-only exports aren't emitted.
|
|
1756
|
+
if (parent.kind === SyntaxKind.ExportSpecifier && getIsTypeOnly(parent)) return false;
|
|
1757
|
+
const greatGrandparent = parent.parent?.parent;
|
|
1758
|
+
return !(
|
|
1759
|
+
greatGrandparent !== undefined &&
|
|
1760
|
+
greatGrandparent.kind === SyntaxKind.ExportDeclaration &&
|
|
1761
|
+
getIsTypeOnly(greatGrandparent)
|
|
1762
|
+
);
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
/**
|
|
1766
|
+
* Marks the alias on the left of a property access as referenced, unless the access reads a const
|
|
1767
|
+
* enum (or a module of only const enums) that is inlined.
|
|
1768
|
+
*/
|
|
1769
|
+
export function markPropertyAliasReferenced(
|
|
1770
|
+
c: Checker,
|
|
1771
|
+
location: Node,
|
|
1772
|
+
prop: Symbol | undefined,
|
|
1773
|
+
leftType: Type,
|
|
1774
|
+
): void {
|
|
1775
|
+
if (!marksReferencedAliases(c)) return;
|
|
1776
|
+
if ((location.flags & NodeFlags.Ambient) !== 0 || isPartOfImportEqualsModuleReference(location)) return;
|
|
1777
|
+
const left =
|
|
1778
|
+
location.kind === SyntaxKind.PropertyAccessExpression ? getExpression(location)! : (location as QualifiedName).left;
|
|
1779
|
+
if (left.kind !== SyntaxKind.Identifier || getText(left) === 'this') return;
|
|
1780
|
+
const parentSymbol = getResolvedSymbol(c, left);
|
|
1781
|
+
if (parentSymbol === c.unknownSymbol) return;
|
|
1782
|
+
if (
|
|
1783
|
+
c.isolatedModules ||
|
|
1784
|
+
(c.preserveConstEnums && isExportOrExportExpression(location)) ||
|
|
1785
|
+
(leftType.flags & TypeFlags.Any) !== 0 ||
|
|
1786
|
+
leftType === c.silentNeverType
|
|
1787
|
+
) {
|
|
1788
|
+
markAliasReferenced(c, parentSymbol, location);
|
|
1789
|
+
return;
|
|
1790
|
+
}
|
|
1791
|
+
if (
|
|
1792
|
+
prop !== undefined &&
|
|
1793
|
+
(isConstEnumOrConstEnumOnlyModule(prop) ||
|
|
1794
|
+
((prop.flags & SymbolFlags.EnumMember) !== 0 && location.parent!.kind === SyntaxKind.EnumMember))
|
|
1795
|
+
)
|
|
1796
|
+
return;
|
|
1797
|
+
markAliasReferenced(c, parentSymbol, location);
|
|
1798
|
+
}
|
|
1799
|
+
|
|
1800
|
+
function isPartOfImportEqualsModuleReference(location: Node): boolean {
|
|
1801
|
+
let importEquals: Node | undefined = location;
|
|
1802
|
+
while (importEquals !== undefined && importEquals.kind !== SyntaxKind.ImportEqualsDeclaration)
|
|
1803
|
+
importEquals = importEquals.parent;
|
|
1804
|
+
if (importEquals === undefined) return false;
|
|
1805
|
+
const reference = (importEquals as ImportEqualsDeclaration).moduleReference;
|
|
1806
|
+
for (let node: Node | undefined = location; node !== undefined && node !== importEquals; node = node.parent)
|
|
1807
|
+
if (node === reference) return true;
|
|
1808
|
+
return false;
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1811
|
+
function markIdentifierAliasReferenced(c: Checker, location: Node): void {
|
|
1812
|
+
const symbol = getResolvedSymbol(c, location);
|
|
1813
|
+
if (symbol !== c.argumentsSymbol && symbol !== c.unknownSymbol) markAliasReferenced(c, symbol, location);
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
function markAliasReferenced(c: Checker, symbol: Symbol, location: Node): void {
|
|
1817
|
+
if (!marksReferencedAliases(c)) return;
|
|
1818
|
+
if (!isNonLocalAlias(symbol, SymbolFlags.Value) || isPartOfTypeQuery(location)) return;
|
|
1819
|
+
const target = resolveAlias(c, symbol);
|
|
1820
|
+
if ((getSymbolFlagsExcludingTypeOnly(c, symbol) & (SymbolFlags.Value | SymbolFlags.ExportValue)) === 0) return;
|
|
1821
|
+
// An alias of a const enum can be elided unless the enum is kept.
|
|
1822
|
+
if (
|
|
1823
|
+
c.isolatedModules ||
|
|
1824
|
+
(c.preserveConstEnums && isExportOrExportExpression(location)) ||
|
|
1825
|
+
!isConstEnumOrConstEnumOnlyModule(getExportSymbolOfValueSymbolIfExported(c, target))
|
|
1826
|
+
)
|
|
1827
|
+
markAliasSymbolAsReferenced(c, symbol);
|
|
1828
|
+
}
|
|
1829
|
+
|
|
1830
|
+
/**
|
|
1831
|
+
* Marks an alias as referenced; an `import a = b.c` whose target is a value then references `b`,
|
|
1832
|
+
* resolving it as a value.
|
|
1833
|
+
*/
|
|
1834
|
+
function markAliasSymbolAsReferenced(c: Checker, symbol: Symbol): void {
|
|
1835
|
+
const links = c.symbolLinks(symbol);
|
|
1836
|
+
if (links.aliasReferenced) return;
|
|
1837
|
+
links.aliasReferenced = true;
|
|
1838
|
+
const node = getDeclarationOfAliasSymbol(symbol);
|
|
1839
|
+
if (
|
|
1840
|
+
node !== undefined &&
|
|
1841
|
+
node.kind === SyntaxKind.ImportEqualsDeclaration &&
|
|
1842
|
+
(node as ImportEqualsDeclaration).moduleReference.kind !== SyntaxKind.ExternalModuleReference &&
|
|
1843
|
+
(getSymbolFlags(c, resolveSymbol(c, symbol)!) & SymbolFlags.Value) !== 0
|
|
1844
|
+
)
|
|
1845
|
+
markIdentifierAliasReferenced(c, getFirstIdentifierOfEntityName((node as ImportEqualsDeclaration).moduleReference));
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1848
|
+
function markExportAsReferenced(c: Checker, node: Node): void {
|
|
1849
|
+
if (!marksReferencedAliases(c)) return;
|
|
1850
|
+
const symbol = getSymbolOfDeclaration(c, node);
|
|
1851
|
+
if (symbol === undefined) return;
|
|
1852
|
+
const target = resolveAlias(c, symbol);
|
|
1853
|
+
if (
|
|
1854
|
+
target === c.unknownSymbol ||
|
|
1855
|
+
((getSymbolFlagsExcludingTypeOnly(c, symbol) & SymbolFlags.Value) !== 0 &&
|
|
1856
|
+
!isConstEnumOrConstEnumOnlyModule(target))
|
|
1857
|
+
)
|
|
1858
|
+
markAliasSymbolAsReferenced(c, symbol);
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
function markExportSpecifierAliasReferenced(
|
|
1862
|
+
c: Checker,
|
|
1863
|
+
node: Node,
|
|
1864
|
+
symbol: Symbol | undefined,
|
|
1865
|
+
exportedName: Node,
|
|
1866
|
+
): void {
|
|
1867
|
+
if (!marksReferencedAliases(c)) return;
|
|
1868
|
+
if ((node.flags & NodeFlags.Ambient) !== 0 || getIsTypeOnly(node) || getIsTypeOnly(node.parent!.parent!)) return;
|
|
1869
|
+
let target = symbol;
|
|
1870
|
+
if (target !== undefined && (target.flags & SymbolFlags.Alias) !== 0) target = resolveAlias(c, target);
|
|
1871
|
+
if (target === undefined || (getSymbolFlags(c, target) & SymbolFlags.Value) !== 0) {
|
|
1872
|
+
markExportAsReferenced(c, node);
|
|
1873
|
+
markIdentifierAliasReferenced(c, exportedName);
|
|
1874
|
+
}
|
|
1875
|
+
}
|
|
1876
|
+
|
|
1877
|
+
function isConstEnumOrConstEnumOnlyModule(symbol: Symbol): boolean {
|
|
1878
|
+
return (symbol.flags & (SymbolFlags.ConstEnum | SymbolFlags.ConstEnumOnlyModule)) !== 0;
|
|
1879
|
+
}
|
|
1880
|
+
|
|
1881
|
+
function isExportOrExportExpression(location: Node): boolean {
|
|
1882
|
+
for (let n: Node | undefined = location; n !== undefined; n = n.parent) {
|
|
1883
|
+
const parent = n.parent;
|
|
1884
|
+
if (parent === undefined) return false;
|
|
1885
|
+
if (parent.kind === SyntaxKind.ExportAssignment)
|
|
1886
|
+
return (
|
|
1887
|
+
getExpression(parent) === n && isEntityNameOrEntityNameExpression(n) && n.kind !== SyntaxKind.QualifiedName
|
|
1888
|
+
);
|
|
1889
|
+
if (parent.kind === SyntaxKind.ExportSpecifier) {
|
|
1890
|
+
if (getName(parent) === n || getPropertyName(parent) === n) return true;
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
return false;
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
/** `getSymbolFlags`, stopping at the first type-only alias along the chain. */
|
|
1897
|
+
function getSymbolFlagsExcludingTypeOnly(c: Checker, symbol: Symbol): SymbolFlags {
|
|
1898
|
+
return getSymbolFlagsEx(c, symbol, true, false);
|
|
1899
|
+
}
|
|
1900
|
+
|
|
1901
|
+
/** `getSymbolFlags`, optionally stopping at type-only aliases or leaving out the symbol's own flags. */
|
|
1902
|
+
function getSymbolFlagsEx(
|
|
1903
|
+
c: Checker,
|
|
1904
|
+
symbol: Symbol,
|
|
1905
|
+
excludeTypeOnlyMeanings: boolean,
|
|
1906
|
+
excludeLocalMeanings: boolean,
|
|
1907
|
+
): SymbolFlags {
|
|
1908
|
+
const seen = new Set<Symbol>();
|
|
1909
|
+
let flags = excludeLocalMeanings ? SymbolFlags.None : symbol.flags;
|
|
1910
|
+
while ((symbol.flags & SymbolFlags.Alias) !== 0) {
|
|
1911
|
+
if (excludeTypeOnlyMeanings && getTypeOnlyAliasDeclaration(c, symbol) !== undefined) break;
|
|
1912
|
+
const target = getExportSymbolOfValueSymbolIfExported(c, resolveAlias(c, symbol));
|
|
1913
|
+
if (target === c.unknownSymbol) return SymbolFlags.All;
|
|
1914
|
+
if ((target.flags & SymbolFlags.Alias) !== 0) {
|
|
1915
|
+
if (target === symbol || seen.has(target)) break;
|
|
1916
|
+
seen.add(symbol);
|
|
1917
|
+
seen.add(target);
|
|
1918
|
+
}
|
|
1919
|
+
flags |= target.flags;
|
|
1920
|
+
symbol = target;
|
|
1921
|
+
}
|
|
1922
|
+
return flags;
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1925
|
+
// ---- Module exports
|
|
1926
|
+
|
|
1927
|
+
/** Own exports plus those re-exported with `export *`; own exports shadow re-exported ones. */
|
|
1928
|
+
export function getExportsOfModuleWorker(c: Checker, moduleSymbol: Symbol): SymbolTable {
|
|
1929
|
+
const collector = new ExportCollector(c);
|
|
1930
|
+
const exportEquals = moduleSymbol.exports?.get(InternalSymbolName.ExportEquals);
|
|
1931
|
+
const originalModule =
|
|
1932
|
+
exportEquals !== undefined && resolveSymbolEx(c, exportEquals, false) !== undefined ? moduleSymbol : undefined;
|
|
1933
|
+
const resolvedModule = resolveExternalModuleSymbol(c, moduleSymbol, false);
|
|
1934
|
+
const exports = collector.visit(resolvedModule, undefined, false) ?? new Map<string, Symbol>();
|
|
1935
|
+
// A CommonJS module with `export =` may also export types, stored on the original module.
|
|
1936
|
+
if (originalModule !== undefined && originalModule.exports !== undefined && originalModule.exports.size > 1) {
|
|
1937
|
+
for (const symbol of originalModule.exports.values()) {
|
|
1938
|
+
if (symbol.name === InternalSymbolName.ExportEquals || symbol.name === InternalSymbolName.ExportStar) continue;
|
|
1939
|
+
const flags = getSymbolFlags(c, symbol);
|
|
1940
|
+
if (
|
|
1941
|
+
(flags & (SymbolFlags.Type | SymbolFlags.Namespace)) !== 0 &&
|
|
1942
|
+
(flags & SymbolFlags.Value) === 0 &&
|
|
1943
|
+
!exports.has(symbol.name)
|
|
1944
|
+
)
|
|
1945
|
+
exports.set(symbol.name, symbol);
|
|
1946
|
+
}
|
|
1947
|
+
}
|
|
1948
|
+
const typeOnlyExportStarMap = collector.typeOnlyExportStarMap;
|
|
1949
|
+
if (typeOnlyExportStarMap !== undefined) {
|
|
1950
|
+
for (const name of collector.nonTypeOnlyNames) typeOnlyExportStarMap.delete(name);
|
|
1951
|
+
c.symbolLinks(moduleSymbol).typeOnlyExportStarMap = typeOnlyExportStarMap;
|
|
1952
|
+
}
|
|
1953
|
+
return exports;
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
/** A name re-exported by several `export *` declarations. */
|
|
1957
|
+
class ExportCollision {
|
|
1958
|
+
readonly specifierText: string;
|
|
1959
|
+
readonly exportsWithDuplicate: Node[] = [];
|
|
1960
|
+
|
|
1961
|
+
constructor(specifierText: string) {
|
|
1962
|
+
this.specifierText = specifierText;
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
|
|
1966
|
+
class ExportCollector {
|
|
1967
|
+
readonly c: Checker;
|
|
1968
|
+
readonly visited: Symbol[] = [];
|
|
1969
|
+
readonly nonTypeOnlyNames = new Set<string>();
|
|
1970
|
+
typeOnlyExportStarMap: Map<string, Node> | undefined = undefined;
|
|
1971
|
+
|
|
1972
|
+
constructor(c: Checker) {
|
|
1973
|
+
this.c = c;
|
|
1974
|
+
}
|
|
1975
|
+
|
|
1976
|
+
// Modules may re-export each other circularly.
|
|
1977
|
+
visit(symbol: Symbol | undefined, exportStar: Node | undefined, isTypeOnly: boolean): SymbolTable | undefined {
|
|
1978
|
+
if (!isTypeOnly && symbol !== undefined && symbol.exports !== undefined)
|
|
1979
|
+
// Added before the visited check: `export *` overrides an earlier `export type *` of the same module.
|
|
1980
|
+
for (const name of symbol.exports.keys()) this.nonTypeOnlyNames.add(name);
|
|
1981
|
+
if (symbol === undefined || symbol.exports === undefined || this.visited.includes(symbol)) return undefined;
|
|
1982
|
+
this.visited.push(symbol);
|
|
1983
|
+
const symbols: SymbolTable = new Map(symbol.exports);
|
|
1984
|
+
const exportStars = symbol.exports.get(InternalSymbolName.ExportStar);
|
|
1985
|
+
if (exportStars !== undefined) {
|
|
1986
|
+
const nested: SymbolTable = new Map<string, Symbol>();
|
|
1987
|
+
const lookupTable = new Map<string, ExportCollision>();
|
|
1988
|
+
for (const node of exportStars.declarations) {
|
|
1989
|
+
const specifier = (node as ExportDeclaration).moduleSpecifier;
|
|
1990
|
+
const resolved = resolveExternalModuleName(this.c, node, specifier, false);
|
|
1991
|
+
const exported = this.visit(resolved, node, isTypeOnly || getIsTypeOnly(node));
|
|
1992
|
+
this.extendExportSymbols(nested, exported, lookupTable, node);
|
|
1993
|
+
}
|
|
1994
|
+
for (const [id, collision] of lookupTable) {
|
|
1995
|
+
// A module that exports the name itself resolves the ambiguity.
|
|
1996
|
+
if (id === InternalSymbolName.ExportEquals || collision.exportsWithDuplicate.length === 0 || symbols.has(id))
|
|
1997
|
+
continue;
|
|
1998
|
+
for (const node of collision.exportsWithDuplicate)
|
|
1999
|
+
this.c.error(
|
|
2000
|
+
node,
|
|
2001
|
+
Diagnostics.Module_0_has_already_exported_a_member_named_1_Consider_explicitly_re_exporting_to_resolve_the_ambiguity,
|
|
2002
|
+
collision.specifierText,
|
|
2003
|
+
id,
|
|
2004
|
+
);
|
|
2005
|
+
}
|
|
2006
|
+
this.extendExportSymbols(symbols, nested, undefined, undefined);
|
|
2007
|
+
}
|
|
2008
|
+
if (exportStar !== undefined && getIsTypeOnly(exportStar)) {
|
|
2009
|
+
if (this.typeOnlyExportStarMap === undefined) this.typeOnlyExportStarMap = new Map<string, Node>();
|
|
2010
|
+
for (const name of symbols.keys()) this.typeOnlyExportStarMap.set(name, exportStar);
|
|
2011
|
+
}
|
|
2012
|
+
return symbols;
|
|
2013
|
+
}
|
|
2014
|
+
|
|
2015
|
+
extendExportSymbols(
|
|
2016
|
+
target: SymbolTable,
|
|
2017
|
+
source: SymbolTable | undefined,
|
|
2018
|
+
lookupTable: Map<string, ExportCollision> | undefined,
|
|
2019
|
+
exportNode: Node | undefined,
|
|
2020
|
+
): void {
|
|
2021
|
+
if (source === undefined) return;
|
|
2022
|
+
for (const [id, sourceSymbol] of source) {
|
|
2023
|
+
if (id === InternalSymbolName.Default) continue;
|
|
2024
|
+
const targetSymbol = target.get(id);
|
|
2025
|
+
if (targetSymbol === undefined) {
|
|
2026
|
+
target.set(id, sourceSymbol);
|
|
2027
|
+
if (lookupTable !== undefined && exportNode !== undefined) {
|
|
2028
|
+
const specifier = (exportNode as ExportDeclaration).moduleSpecifier!;
|
|
2029
|
+
lookupTable.set(id, new ExportCollision(getSourceTextOfNode(getSourceFileOfNode(specifier), specifier)));
|
|
2030
|
+
}
|
|
2031
|
+
} else if (
|
|
2032
|
+
lookupTable !== undefined &&
|
|
2033
|
+
exportNode !== undefined &&
|
|
2034
|
+
resolveSymbol(this.c, targetSymbol) !== resolveSymbol(this.c, sourceSymbol)
|
|
2035
|
+
)
|
|
2036
|
+
lookupTable.get(id)?.exportsWithDuplicate.push(exportNode);
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
}
|
|
2040
|
+
|
|
2041
|
+
// ---- Import types and dynamic imports
|
|
2042
|
+
|
|
2043
|
+
/** The type of `import("m")`, `import("m").T` or `typeof import("m").v`. */
|
|
2044
|
+
export function getTypeFromImportTypeNode(c: Checker, node: Node): Type {
|
|
2045
|
+
const type = c.resolvedTypes.get(node.id);
|
|
2046
|
+
if (type !== undefined) return type;
|
|
2047
|
+
const importType = node as ImportTypeNode;
|
|
2048
|
+
if (!isLiteralImportTypeNode(node)) {
|
|
2049
|
+
c.error(importType.argument, Diagnostics.String_literal_expected);
|
|
2050
|
+
c.resolvedSymbols.set(node.id, c.unknownSymbol);
|
|
2051
|
+
c.resolvedTypes.set(node.id, c.errorType);
|
|
2052
|
+
return c.resolvedTypes.get(node.id)!;
|
|
2053
|
+
}
|
|
2054
|
+
const targetMeaning = importType.isTypeOf ? SymbolFlags.Value : SymbolFlags.Type;
|
|
2055
|
+
const literal = (importType.argument as LiteralTypeNode).literal;
|
|
2056
|
+
const innerModuleSymbol = resolveExternalModuleName(c, node, literal, false);
|
|
2057
|
+
if (innerModuleSymbol === undefined) {
|
|
2058
|
+
c.resolvedSymbols.set(node.id, c.unknownSymbol);
|
|
2059
|
+
c.resolvedTypes.set(node.id, c.errorType);
|
|
2060
|
+
return c.resolvedTypes.get(node.id)!;
|
|
2061
|
+
}
|
|
2062
|
+
const moduleSymbol = resolveExternalModuleSymbol(c, innerModuleSymbol, false)!;
|
|
2063
|
+
const qualifier = importType.qualifier;
|
|
2064
|
+
if (qualifier !== undefined && !nodeIsMissing(qualifier)) {
|
|
2065
|
+
const nameChain = getIdentifierChain(qualifier);
|
|
2066
|
+
let currentNamespace = moduleSymbol;
|
|
2067
|
+
for (let i = 0; i < nameChain.length; i++) {
|
|
2068
|
+
const current = nameChain[i]!;
|
|
2069
|
+
const meaning = i === nameChain.length - 1 ? targetMeaning : SymbolFlags.Namespace;
|
|
2070
|
+
const merged = getMergedSymbol(c, resolveSymbol(c, currentNamespace)!);
|
|
2071
|
+
const text = getText(current);
|
|
2072
|
+
const next = importType.isTypeOf
|
|
2073
|
+
? getPropertyOfTypeEx(c, getTypeOfSymbol(c, merged), text, false, true)
|
|
2074
|
+
: getSymbolOfMeaning(c, getExportsOfSymbol(c, merged), text, meaning);
|
|
2075
|
+
if (next === undefined) {
|
|
2076
|
+
c.error(
|
|
2077
|
+
current,
|
|
2078
|
+
Diagnostics.Namespace_0_has_no_exported_member_1,
|
|
2079
|
+
getFullyQualifiedName(c, currentNamespace),
|
|
2080
|
+
declarationNameToString(getSourceFileOfNode(current), current),
|
|
2081
|
+
);
|
|
2082
|
+
c.resolvedTypes.set(node.id, c.errorType);
|
|
2083
|
+
return c.resolvedTypes.get(node.id)!;
|
|
2084
|
+
}
|
|
2085
|
+
c.resolvedSymbols.set(current.id, next);
|
|
2086
|
+
c.resolvedSymbols.set(current.parent!.id, next);
|
|
2087
|
+
currentNamespace = next;
|
|
2088
|
+
}
|
|
2089
|
+
c.resolvedTypes.set(node.id, resolveImportSymbolType(c, node, currentNamespace, targetMeaning));
|
|
2090
|
+
} else if ((getSymbolFlags(c, moduleSymbol) & targetMeaning) !== 0) {
|
|
2091
|
+
c.resolvedTypes.set(node.id, resolveImportSymbolType(c, node, moduleSymbol, targetMeaning));
|
|
2092
|
+
} else {
|
|
2093
|
+
c.error(
|
|
2094
|
+
node,
|
|
2095
|
+
targetMeaning === SymbolFlags.Value
|
|
2096
|
+
? Diagnostics.Module_0_does_not_refer_to_a_value_but_is_used_as_a_value_here
|
|
2097
|
+
: Diagnostics.Module_0_does_not_refer_to_a_type_but_is_used_as_a_type_here_Did_you_mean_typeof_import_0,
|
|
2098
|
+
getText(literal),
|
|
2099
|
+
);
|
|
2100
|
+
c.resolvedSymbols.set(node.id, c.unknownSymbol);
|
|
2101
|
+
c.resolvedTypes.set(node.id, c.errorType);
|
|
2102
|
+
}
|
|
2103
|
+
return c.resolvedTypes.get(node.id)!;
|
|
2104
|
+
}
|
|
2105
|
+
|
|
2106
|
+
function getIdentifierChain(node: Node): Identifier[] {
|
|
2107
|
+
if (node.kind === SyntaxKind.Identifier) return [node as Identifier];
|
|
2108
|
+
const chain = getIdentifierChain((node as QualifiedName).left);
|
|
2109
|
+
chain.push((node as QualifiedName).right);
|
|
2110
|
+
return chain;
|
|
2111
|
+
}
|
|
2112
|
+
|
|
2113
|
+
function resolveImportSymbolType(c: Checker, node: Node, symbol: Symbol, meaning: SymbolFlags): Type {
|
|
2114
|
+
const resolvedSymbol = resolveSymbol(c, symbol)!;
|
|
2115
|
+
c.resolvedSymbols.set(node.id, resolvedSymbol);
|
|
2116
|
+
// The alias itself caches the type of a value.
|
|
2117
|
+
if (meaning === SymbolFlags.Value) return getTypeOfSymbol(c, symbol);
|
|
2118
|
+
return getTypeReferenceType(c, node, resolvedSymbol);
|
|
2119
|
+
}
|
|
2120
|
+
|
|
2121
|
+
/** `import(specifier, options)`: a promise of the module namespace. */
|
|
2122
|
+
export function checkImportCallExpression(c: Checker, node: Node): Type {
|
|
2123
|
+
const args = (node as CallExpression).arguments.nodes;
|
|
2124
|
+
if (args.length === 0) return createPromiseReturnType(c, node, c.anyType);
|
|
2125
|
+
const specifier = args[0]!;
|
|
2126
|
+
const specifierType = checkExpressionCached(c, specifier);
|
|
2127
|
+
const optionsType = args.length > 1 ? checkExpressionCached(c, args[1]!) : undefined;
|
|
2128
|
+
// Extra arguments are a grammar error; check them for completions.
|
|
2129
|
+
for (let i = 2; i < args.length; i++) checkExpressionCached(c, args[i]!);
|
|
2130
|
+
if ((specifierType.flags & TypeFlags.Nullable) !== 0 || !isTypeAssignableTo(c, specifierType, c.stringType))
|
|
2131
|
+
c.error(
|
|
2132
|
+
specifier,
|
|
2133
|
+
Diagnostics.Dynamic_import_s_specifier_must_be_of_type_string_but_here_has_type_0,
|
|
2134
|
+
typeToString(c, specifierType),
|
|
2135
|
+
);
|
|
2136
|
+
if (optionsType !== undefined) {
|
|
2137
|
+
const importCallOptionsType = getCachedGlobalType(c, 'ImportCallOptions', 0, true);
|
|
2138
|
+
if (importCallOptionsType !== c.emptyObjectType)
|
|
2139
|
+
checkTypeAssignableTo(
|
|
2140
|
+
c,
|
|
2141
|
+
optionsType,
|
|
2142
|
+
getNullableType(c, importCallOptionsType, TypeFlags.Undefined),
|
|
2143
|
+
args[1]!,
|
|
2144
|
+
undefined,
|
|
2145
|
+
);
|
|
2146
|
+
if (args[1]!.kind === SyntaxKind.ObjectLiteralExpression)
|
|
2147
|
+
for (const property of (args[1]! as ObjectLiteralExpression).properties.nodes) {
|
|
2148
|
+
const name = getName(property);
|
|
2149
|
+
if (
|
|
2150
|
+
property.kind === SyntaxKind.PropertyAssignment &&
|
|
2151
|
+
name !== undefined &&
|
|
2152
|
+
name.kind === SyntaxKind.Identifier &&
|
|
2153
|
+
getText(name) === 'assert'
|
|
2154
|
+
) {
|
|
2155
|
+
c.error(
|
|
2156
|
+
name,
|
|
2157
|
+
Diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_assert,
|
|
2158
|
+
);
|
|
2159
|
+
break;
|
|
2160
|
+
}
|
|
2161
|
+
}
|
|
2162
|
+
}
|
|
2163
|
+
const moduleSymbol = resolveExternalModuleName(c, node, specifier, false);
|
|
2164
|
+
if (moduleSymbol !== undefined) {
|
|
2165
|
+
const esModuleSymbol = resolveExternalModuleSymbol(c, moduleSymbol, true);
|
|
2166
|
+
if (esModuleSymbol !== undefined)
|
|
2167
|
+
return createPromiseReturnType(
|
|
2168
|
+
c,
|
|
2169
|
+
node,
|
|
2170
|
+
getTypeWithSyntheticDefaultOnly(
|
|
2171
|
+
c,
|
|
2172
|
+
getTypeOfSymbol(c, esModuleSymbol),
|
|
2173
|
+
esModuleSymbol,
|
|
2174
|
+
moduleSymbol,
|
|
2175
|
+
specifier,
|
|
2176
|
+
) ??
|
|
2177
|
+
getTypeWithSyntheticDefaultImportType(
|
|
2178
|
+
c,
|
|
2179
|
+
getTypeOfSymbol(c, esModuleSymbol),
|
|
2180
|
+
esModuleSymbol,
|
|
2181
|
+
moduleSymbol,
|
|
2182
|
+
specifier,
|
|
2183
|
+
),
|
|
2184
|
+
);
|
|
2185
|
+
}
|
|
2186
|
+
return createPromiseReturnType(c, node, c.anyType);
|
|
2187
|
+
}
|
|
2188
|
+
|
|
2189
|
+
function createPromiseReturnType(c: Checker, node: Node, promisedType: Type): Type {
|
|
2190
|
+
const promiseType = createPromiseType(c, promisedType);
|
|
2191
|
+
if (promiseType !== c.unknownType) return promiseType;
|
|
2192
|
+
c.error(
|
|
2193
|
+
node,
|
|
2194
|
+
Diagnostics.A_dynamic_import_call_returns_a_Promise_Make_sure_you_have_a_declaration_for_Promise_or_include_ES2015_in_your_lib_option,
|
|
2195
|
+
);
|
|
2196
|
+
return c.errorType;
|
|
2197
|
+
}
|
|
2198
|
+
|
|
2199
|
+
// ---- JSDoc links
|
|
2200
|
+
|
|
2201
|
+
/** The symbol a `{@link}` name refers to, as an entity or a member of one. */
|
|
2202
|
+
export function resolveJSDocMemberName(c: Checker, name: Node | undefined): Symbol | undefined {
|
|
2203
|
+
if (name === undefined || (name.kind !== SyntaxKind.Identifier && name.kind !== SyntaxKind.QualifiedName))
|
|
2204
|
+
return undefined;
|
|
2205
|
+
const meaning = SymbolFlags.Type | SymbolFlags.Namespace | SymbolFlags.Value;
|
|
2206
|
+
const symbol = resolveEntityName(c, name, meaning, true, true, getHostSignatureFromJSDoc(name));
|
|
2207
|
+
if (symbol !== undefined) return symbol;
|
|
2208
|
+
if (name.kind !== SyntaxKind.QualifiedName) return undefined;
|
|
2209
|
+
const left = resolveJSDocMemberName(c, (name as QualifiedName).left);
|
|
2210
|
+
if (left === undefined) return undefined;
|
|
2211
|
+
let type: Type | undefined;
|
|
2212
|
+
if ((left.flags & SymbolFlags.Value) !== 0) {
|
|
2213
|
+
const prototype = getPropertyOfType(c, getTypeOfSymbol(c, left), 'prototype');
|
|
2214
|
+
if (prototype !== undefined) type = getTypeOfSymbol(c, prototype);
|
|
2215
|
+
}
|
|
2216
|
+
return getPropertyOfType(c, type ?? getDeclaredTypeOfSymbol(c, left), (name as QualifiedName).right.text);
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
export function getHostSignatureFromJSDoc(node: Node): Node | undefined {
|
|
2220
|
+
let current: Node | undefined = node;
|
|
2221
|
+
while (current !== undefined && current.kind !== SyntaxKind.JSDoc) current = current.parent;
|
|
2222
|
+
const host = current?.parent;
|
|
2223
|
+
return host !== undefined && isFunctionLike(host) ? host : undefined;
|
|
2224
|
+
}
|
|
2225
|
+
|
|
2226
|
+
// ---- Meta properties
|
|
2227
|
+
|
|
2228
|
+
/** `import.meta` and `new.target`. */
|
|
2229
|
+
export function checkMetaProperty(c: Checker, node: Node): Type {
|
|
2230
|
+
const meta = node as MetaProperty;
|
|
2231
|
+
if (meta.keywordToken === SyntaxKind.NewKeyword) return checkNewTargetMetaProperty(c, node);
|
|
2232
|
+
// `import.defer` is only valid as a callee.
|
|
2233
|
+
if (meta.name.text === 'defer') return c.errorType;
|
|
2234
|
+
const moduleKind = getEmitModuleKind(c.compilerOptions);
|
|
2235
|
+
if (moduleKind >= ModuleKind.Node16 && moduleKind <= ModuleKind.NodeNext) {
|
|
2236
|
+
if (c.host.getImpliedNodeFormatForEmit(getSourceFileOfNode(node)) === ModuleKind.CommonJS)
|
|
2237
|
+
c.error(
|
|
2238
|
+
node,
|
|
2239
|
+
Diagnostics.The_import_meta_meta_property_is_not_allowed_in_files_which_will_build_into_CommonJS_output,
|
|
2240
|
+
);
|
|
2241
|
+
} else if (moduleKind < ModuleKind.ES2020 && moduleKind !== ModuleKind.System)
|
|
2242
|
+
c.error(
|
|
2243
|
+
node,
|
|
2244
|
+
Diagnostics.The_import_meta_meta_property_is_only_allowed_when_the_module_option_is_es2020_es2022_esnext_system_node16_node18_node20_or_nodenext,
|
|
2245
|
+
);
|
|
2246
|
+
return meta.name.text === 'meta' ? getCachedGlobalType(c, 'ImportMeta', 0, true) : c.errorType;
|
|
2247
|
+
}
|
|
2248
|
+
|
|
2249
|
+
export function checkNewTargetMetaProperty(c: Checker, node: Node): Type {
|
|
2250
|
+
const container = getThisContainer(node, false, false);
|
|
2251
|
+
if (
|
|
2252
|
+
container.kind !== SyntaxKind.Constructor &&
|
|
2253
|
+
container.kind !== SyntaxKind.FunctionDeclaration &&
|
|
2254
|
+
container.kind !== SyntaxKind.FunctionExpression
|
|
2255
|
+
) {
|
|
2256
|
+
c.error(
|
|
2257
|
+
node,
|
|
2258
|
+
Diagnostics.Meta_property_0_is_only_allowed_in_the_body_of_a_function_declaration_function_expression_or_constructor,
|
|
2259
|
+
'new.target',
|
|
2260
|
+
);
|
|
2261
|
+
return c.errorType;
|
|
2262
|
+
}
|
|
2263
|
+
const symbol = getSymbolOfDeclaration(c, container.kind === SyntaxKind.Constructor ? container.parent! : container);
|
|
2264
|
+
return symbol !== undefined ? getTypeOfSymbol(c, symbol) : c.errorType;
|
|
2265
|
+
}
|
|
2266
|
+
|
|
2267
|
+
// ---- Import and export declarations
|
|
2268
|
+
|
|
2269
|
+
/**
|
|
2270
|
+
* Whether an import or export with a module specifier is in a valid place with a valid specifier;
|
|
2271
|
+
* the grammar checker reports the errors.
|
|
2272
|
+
*/
|
|
2273
|
+
function isValidExternalImportOrExportDeclaration(node: Node): boolean {
|
|
2274
|
+
const moduleName = getExternalModuleName(node);
|
|
2275
|
+
if (moduleName === undefined || nodeIsMissing(moduleName) || moduleName.kind !== SyntaxKind.StringLiteral)
|
|
2276
|
+
return false;
|
|
2277
|
+
const parent = node.parent!;
|
|
2278
|
+
const inAmbientExternalModule = parent.kind === SyntaxKind.ModuleBlock && isAmbientModule(parent.parent!);
|
|
2279
|
+
if (parent.kind !== SyntaxKind.SourceFile && !inAmbientExternalModule) return false;
|
|
2280
|
+
if (
|
|
2281
|
+
inAmbientExternalModule &&
|
|
2282
|
+
isExternalModuleNameRelative(getText(moduleName)) &&
|
|
2283
|
+
!isModuleAugmentationExternal(parent.parent!)
|
|
2284
|
+
)
|
|
2285
|
+
return false;
|
|
2286
|
+
if (node.kind === SyntaxKind.ImportEqualsDeclaration) return true;
|
|
2287
|
+
const attributes = getImportAttributes(node);
|
|
2288
|
+
if (attributes === undefined) return true;
|
|
2289
|
+
for (const attribute of attributes.attributes.nodes)
|
|
2290
|
+
if ((attribute as ImportAttribute).value.kind !== SyntaxKind.StringLiteral) return false;
|
|
2291
|
+
return true;
|
|
2292
|
+
}
|
|
2293
|
+
|
|
2294
|
+
function isInModuleElementContext(node: Node): boolean {
|
|
2295
|
+
const parent = node.parent!.kind;
|
|
2296
|
+
return (
|
|
2297
|
+
parent === SyntaxKind.SourceFile || parent === SyntaxKind.ModuleBlock || parent === SyntaxKind.ModuleDeclaration
|
|
2298
|
+
);
|
|
2299
|
+
}
|
|
2300
|
+
|
|
2301
|
+
function getImportAttributes(node: Node): ImportAttributes | undefined {
|
|
2302
|
+
if (node.kind === SyntaxKind.ImportDeclaration || node.kind === SyntaxKind.JSImportDeclaration)
|
|
2303
|
+
return (node as ImportDeclaration).attributes;
|
|
2304
|
+
if (node.kind === SyntaxKind.ExportDeclaration) return (node as ExportDeclaration).attributes;
|
|
2305
|
+
return undefined;
|
|
2306
|
+
}
|
|
2307
|
+
|
|
2308
|
+
/** Whether the grammar checker reports the import clause, which skips its bindings. */
|
|
2309
|
+
function hasImportClauseGrammarError(c: Checker, node: ImportClause): boolean {
|
|
2310
|
+
const namedBindings = node.namedBindings;
|
|
2311
|
+
if (node.phaseModifier === SyntaxKind.TypeKeyword) {
|
|
2312
|
+
if ((node.flags & NodeFlags.JSDoc) === 0 && node.name !== undefined && namedBindings !== undefined)
|
|
2313
|
+
return !hasParseErrors(node);
|
|
2314
|
+
if (namedBindings !== undefined && namedBindings.kind === SyntaxKind.NamedImports)
|
|
2315
|
+
for (const specifier of (namedBindings as NamedImports).elements.nodes)
|
|
2316
|
+
if (getIsTypeOnly(specifier)) return !hasParseErrors(node);
|
|
2317
|
+
} else if (node.phaseModifier === SyntaxKind.DeferKeyword) {
|
|
2318
|
+
if (node.name !== undefined) return !hasParseErrors(node);
|
|
2319
|
+
if (namedBindings !== undefined && namedBindings.kind === SyntaxKind.NamedImports) return !hasParseErrors(node);
|
|
2320
|
+
const moduleKind = getEmitModuleKind(c.compilerOptions);
|
|
2321
|
+
if (moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.Preserve) return !hasParseErrors(node);
|
|
2322
|
+
}
|
|
2323
|
+
return false;
|
|
2324
|
+
}
|
|
2325
|
+
|
|
2326
|
+
function hasParseErrors(node: Node): boolean {
|
|
2327
|
+
return getSourceFileOfNode(node).diagnostics.length > 0;
|
|
2328
|
+
}
|
|
2329
|
+
|
|
2330
|
+
export function checkImportDeclaration(c: Checker, node: Node): void {
|
|
2331
|
+
if (!isInModuleElementContext(node) || !isValidExternalImportOrExportDeclaration(node)) {
|
|
2332
|
+
checkImportAttributes(c, node);
|
|
2333
|
+
return;
|
|
2334
|
+
}
|
|
2335
|
+
const declaration = node as ImportDeclaration;
|
|
2336
|
+
const clause = declaration.importClause;
|
|
2337
|
+
if (clause !== undefined && !hasImportClauseGrammarError(c, clause)) {
|
|
2338
|
+
if (clause.name !== undefined) checkImportBinding(c, clause);
|
|
2339
|
+
const namedBindings = clause.namedBindings;
|
|
2340
|
+
let resolvedModule: Symbol | undefined;
|
|
2341
|
+
const emittedAsCommonJS = c.host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS;
|
|
2342
|
+
let needsImportStar = false;
|
|
2343
|
+
if (namedBindings !== undefined) {
|
|
2344
|
+
if (namedBindings.kind === SyntaxKind.NamespaceImport) {
|
|
2345
|
+
checkImportBinding(c, namedBindings);
|
|
2346
|
+
if (emittedAsCommonJS) {
|
|
2347
|
+
needsImportStar = true;
|
|
2348
|
+
checkExternalEmitHelpers(c, node, ExternalEmitHelpers.ImportStar);
|
|
2349
|
+
}
|
|
2350
|
+
} else {
|
|
2351
|
+
resolvedModule = resolveExternalModuleName(c, node, declaration.moduleSpecifier, false);
|
|
2352
|
+
if (resolvedModule !== undefined)
|
|
2353
|
+
for (const binding of (namedBindings as NamedImports).elements.nodes) checkImportBinding(c, binding);
|
|
2354
|
+
}
|
|
2355
|
+
}
|
|
2356
|
+
if (clause.name !== undefined && !needsImportStar && emittedAsCommonJS)
|
|
2357
|
+
checkExternalEmitHelpers(c, node, ExternalEmitHelpers.ImportDefault);
|
|
2358
|
+
const moduleKind = getEmitModuleKind(c.compilerOptions);
|
|
2359
|
+
if (
|
|
2360
|
+
!isTypeOnly(clause) &&
|
|
2361
|
+
moduleKind >= ModuleKind.Node18 &&
|
|
2362
|
+
moduleKind <= ModuleKind.NodeNext &&
|
|
2363
|
+
isOnlyImportableAsDefault(c, declaration.moduleSpecifier, resolvedModule) &&
|
|
2364
|
+
!hasTypeJsonImportAttribute(declaration)
|
|
2365
|
+
)
|
|
2366
|
+
c.error(
|
|
2367
|
+
declaration.moduleSpecifier,
|
|
2368
|
+
Diagnostics.Importing_a_JSON_file_into_an_ECMAScript_module_requires_a_type_Colon_json_import_attribute_when_module_is_set_to_0,
|
|
2369
|
+
moduleKindName(moduleKind),
|
|
2370
|
+
);
|
|
2371
|
+
} else if (clause === undefined && c.compilerOptions.noUncheckedSideEffectImports !== false)
|
|
2372
|
+
resolveExternalModuleNameWorker(
|
|
2373
|
+
c,
|
|
2374
|
+
node,
|
|
2375
|
+
declaration.moduleSpecifier,
|
|
2376
|
+
Diagnostics.Cannot_find_module_or_type_declarations_for_side_effect_import_of_0,
|
|
2377
|
+
false,
|
|
2378
|
+
false,
|
|
2379
|
+
);
|
|
2380
|
+
checkImportAttributes(c, node);
|
|
2381
|
+
}
|
|
2382
|
+
|
|
2383
|
+
function hasTypeJsonImportAttribute(node: ImportDeclaration): boolean {
|
|
2384
|
+
const attributes = getImportAttributes(node) as ImportAttributes | undefined;
|
|
2385
|
+
if (attributes === undefined) return false;
|
|
2386
|
+
for (const attribute of attributes.attributes.nodes) {
|
|
2387
|
+
const value = (attribute as ImportAttribute).value;
|
|
2388
|
+
if (getText(getName(attribute)!) === 'type' && isStringLiteralLike(value) && getText(value) === 'json') return true;
|
|
2389
|
+
}
|
|
2390
|
+
return false;
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
function checkImportBinding(c: Checker, node: Node): void {
|
|
2394
|
+
checkCollisionsForDeclarationName(c, node, getName(node));
|
|
2395
|
+
checkAliasSymbol(c, node);
|
|
2396
|
+
if (
|
|
2397
|
+
node.kind === SyntaxKind.ImportSpecifier &&
|
|
2398
|
+
moduleExportNameIsDefault(getPropertyNameOrName(node)) &&
|
|
2399
|
+
c.host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS
|
|
2400
|
+
)
|
|
2401
|
+
checkExternalEmitHelpers(c, node, ExternalEmitHelpers.ImportDefault);
|
|
2402
|
+
}
|
|
2403
|
+
|
|
2404
|
+
/** Import attributes must be assignable to the global `ImportAttributes`. */
|
|
2405
|
+
function checkImportAttributes(c: Checker, declaration: Node): void {
|
|
2406
|
+
const node = getImportAttributes(declaration);
|
|
2407
|
+
if (node === undefined) return;
|
|
2408
|
+
const importAttributesType = getCachedGlobalType(c, 'ImportAttributes', 0, true);
|
|
2409
|
+
if (importAttributesType !== c.emptyObjectType)
|
|
2410
|
+
checkTypeAssignableTo(
|
|
2411
|
+
c,
|
|
2412
|
+
getTypeFromImportAttributes(c, node),
|
|
2413
|
+
getNullableType(c, importAttributesType, TypeFlags.Undefined),
|
|
2414
|
+
node,
|
|
2415
|
+
undefined,
|
|
2416
|
+
);
|
|
2417
|
+
}
|
|
2418
|
+
|
|
2419
|
+
function getTypeFromImportAttributes(c: Checker, node: ImportAttributes): Type {
|
|
2420
|
+
if (c.resolvedTypes.get(node.id) === undefined) {
|
|
2421
|
+
const symbol = c.newSymbol(SymbolFlags.ObjectLiteral, InternalSymbolName.ImportAttributes);
|
|
2422
|
+
const members: SymbolTable = new Map<string, Symbol>();
|
|
2423
|
+
for (const attribute of node.attributes.nodes) {
|
|
2424
|
+
const name = getText((attribute as ImportAttribute).name!);
|
|
2425
|
+
const member = c.newSymbol(SymbolFlags.Property, name);
|
|
2426
|
+
c.symbolTypes.set(
|
|
2427
|
+
getSymbolId(member),
|
|
2428
|
+
getRegularTypeOfLiteralType(c, checkExpression(c, (attribute as ImportAttribute).value)),
|
|
2429
|
+
);
|
|
2430
|
+
members.set(name, member);
|
|
2431
|
+
}
|
|
2432
|
+
const type = createAnonymousType(c, symbol, members, [], [], []);
|
|
2433
|
+
type.objectFlags |= ObjectFlags.ObjectLiteral | ObjectFlags.NonInferrableType;
|
|
2434
|
+
c.resolvedTypes.set(node.id, type);
|
|
2435
|
+
}
|
|
2436
|
+
return c.resolvedTypes.get(node.id)!;
|
|
2437
|
+
}
|
|
2438
|
+
|
|
2439
|
+
export function checkImportEqualsDeclaration(c: Checker, node: Node): void {
|
|
2440
|
+
if (!isInModuleElementContext(node)) return;
|
|
2441
|
+
const declaration = node as ImportEqualsDeclaration;
|
|
2442
|
+
const reference = declaration.moduleReference;
|
|
2443
|
+
const isExternal = reference.kind === SyntaxKind.ExternalModuleReference;
|
|
2444
|
+
if (shouldCheckErasableSyntax(c, node) && (node.flags & NodeFlags.Ambient) === 0)
|
|
2445
|
+
c.error(node, Diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled);
|
|
2446
|
+
if (isExternal && !isValidExternalImportOrExportDeclaration(node)) return;
|
|
2447
|
+
checkImportBinding(c, node);
|
|
2448
|
+
if (hasSyntacticModifier(node, ModifierFlags.Export) && (node.flags & NodeFlags.Ambient) === 0)
|
|
2449
|
+
markExportAsReferenced(c, node);
|
|
2450
|
+
if (isExternal) return;
|
|
2451
|
+
const target = resolveAlias(c, getSymbolOfDeclaration(c, node)!);
|
|
2452
|
+
if (target === c.unknownSymbol) return;
|
|
2453
|
+
const targetFlags = getSymbolFlags(c, target);
|
|
2454
|
+
if ((targetFlags & SymbolFlags.Value) !== 0) {
|
|
2455
|
+
// A value target must not be hidden by a local declaration of the same name.
|
|
2456
|
+
const moduleName = getFirstIdentifierOfEntityName(reference);
|
|
2457
|
+
const resolved = resolveEntityName(
|
|
2458
|
+
c,
|
|
2459
|
+
moduleName,
|
|
2460
|
+
SymbolFlags.Value | SymbolFlags.Namespace,
|
|
2461
|
+
false,
|
|
2462
|
+
false,
|
|
2463
|
+
undefined,
|
|
2464
|
+
);
|
|
2465
|
+
if (resolved !== undefined && (resolved.flags & SymbolFlags.Namespace) === 0)
|
|
2466
|
+
c.error(
|
|
2467
|
+
moduleName,
|
|
2468
|
+
Diagnostics.Module_0_is_hidden_by_a_local_declaration_with_the_same_name,
|
|
2469
|
+
declarationNameToString(getSourceFileOfNode(moduleName), moduleName),
|
|
2470
|
+
);
|
|
2471
|
+
}
|
|
2472
|
+
if ((targetFlags & SymbolFlags.Type) !== 0)
|
|
2473
|
+
checkTypeNameIsReserved(c, declaration.name, Diagnostics.Import_name_cannot_be_0);
|
|
2474
|
+
}
|
|
2475
|
+
|
|
2476
|
+
function getFirstIdentifierOfEntityName(node: Node): Node {
|
|
2477
|
+
let current = node;
|
|
2478
|
+
while (current.kind === SyntaxKind.QualifiedName) current = (current as QualifiedName).left;
|
|
2479
|
+
return current;
|
|
2480
|
+
}
|
|
2481
|
+
|
|
2482
|
+
/** The string literal name of `export * as "x" from`, which module es2015 and es2020 don't support. */
|
|
2483
|
+
function checkNamespaceExportName(c: Checker, name: Node | undefined): void {
|
|
2484
|
+
if (name === undefined || name.kind !== SyntaxKind.StringLiteral) return;
|
|
2485
|
+
const moduleKind = getEmitModuleKind(c.compilerOptions);
|
|
2486
|
+
if (
|
|
2487
|
+
(moduleKind === ModuleKind.ES2015 || moduleKind === ModuleKind.ES2020) &&
|
|
2488
|
+
!getSourceFileOfNode(name).isDeclarationFile
|
|
2489
|
+
)
|
|
2490
|
+
grammarErrorOnNode(
|
|
2491
|
+
c,
|
|
2492
|
+
name,
|
|
2493
|
+
Diagnostics.String_literal_import_and_export_names_are_not_supported_when_the_module_flag_is_set_to_es2015_or_es2020,
|
|
2494
|
+
);
|
|
2495
|
+
}
|
|
2496
|
+
|
|
2497
|
+
export function checkExportDeclaration(c: Checker, node: Node): void {
|
|
2498
|
+
if (!isInModuleElementContext(node)) return;
|
|
2499
|
+
const declaration = node as ExportDeclaration;
|
|
2500
|
+
if (declaration.moduleSpecifier === undefined || isValidExternalImportOrExportDeclaration(node)) {
|
|
2501
|
+
const clause = declaration.exportClause;
|
|
2502
|
+
if (clause !== undefined && clause.kind === SyntaxKind.NamedExports) {
|
|
2503
|
+
for (const specifier of (clause as NamedExports).elements.nodes) checkExportSpecifier(c, specifier);
|
|
2504
|
+
} else {
|
|
2505
|
+
const moduleSymbol = resolveExternalModuleName(c, node, declaration.moduleSpecifier, false);
|
|
2506
|
+
if (moduleSymbol !== undefined && hasExportAssignmentSymbol(moduleSymbol))
|
|
2507
|
+
c.error(
|
|
2508
|
+
declaration.moduleSpecifier,
|
|
2509
|
+
Diagnostics.Module_0_uses_export_and_cannot_be_used_with_export_Asterisk,
|
|
2510
|
+
symbolToString(c, moduleSymbol),
|
|
2511
|
+
);
|
|
2512
|
+
else if (clause !== undefined) {
|
|
2513
|
+
checkAliasSymbol(c, clause);
|
|
2514
|
+
checkNamespaceExportName(c, getName(clause));
|
|
2515
|
+
}
|
|
2516
|
+
if (c.host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS)
|
|
2517
|
+
checkExternalEmitHelpers(
|
|
2518
|
+
c,
|
|
2519
|
+
node,
|
|
2520
|
+
clause !== undefined ? ExternalEmitHelpers.ImportStar : ExternalEmitHelpers.ExportStar,
|
|
2521
|
+
);
|
|
2522
|
+
}
|
|
2523
|
+
}
|
|
2524
|
+
checkImportAttributes(c, node);
|
|
2525
|
+
}
|
|
2526
|
+
|
|
2527
|
+
function checkExportSpecifier(c: Checker, node: Node): void {
|
|
2528
|
+
checkAliasSymbol(c, node);
|
|
2529
|
+
if ((node.parent!.parent as ExportDeclaration).moduleSpecifier !== undefined) {
|
|
2530
|
+
if (
|
|
2531
|
+
c.host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS &&
|
|
2532
|
+
moduleExportNameIsDefault(getPropertyNameOrName(node))
|
|
2533
|
+
)
|
|
2534
|
+
checkExternalEmitHelpers(c, node, ExternalEmitHelpers.ImportDefault);
|
|
2535
|
+
return;
|
|
2536
|
+
}
|
|
2537
|
+
const exportedName = getPropertyNameOrName(node);
|
|
2538
|
+
// `export { "x" }` is a syntax error.
|
|
2539
|
+
if (exportedName.kind === SyntaxKind.StringLiteral) return;
|
|
2540
|
+
// The immediate symbol of the name, without following aliases.
|
|
2541
|
+
const symbol = resolveName(
|
|
2542
|
+
c,
|
|
2543
|
+
exportedName,
|
|
2544
|
+
getText(exportedName),
|
|
2545
|
+
SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace | SymbolFlags.Alias,
|
|
2546
|
+
undefined,
|
|
2547
|
+
true,
|
|
2548
|
+
false,
|
|
2549
|
+
);
|
|
2550
|
+
if (
|
|
2551
|
+
symbol !== undefined &&
|
|
2552
|
+
(symbol === c.undefinedSymbol ||
|
|
2553
|
+
symbol === c.globalThisSymbol ||
|
|
2554
|
+
(symbol.declarations.length > 0 && isGlobalSourceFile(getDeclarationContainer(symbol.declarations[0]!))))
|
|
2555
|
+
)
|
|
2556
|
+
c.error(
|
|
2557
|
+
exportedName,
|
|
2558
|
+
Diagnostics.Cannot_export_0_Only_local_declarations_can_be_exported_from_a_module,
|
|
2559
|
+
getText(exportedName),
|
|
2560
|
+
);
|
|
2561
|
+
else markExportSpecifierAliasReferenced(c, node, symbol, exportedName);
|
|
2562
|
+
}
|
|
2563
|
+
|
|
2564
|
+
export function getDeclarationContainer(node: Node): Node {
|
|
2565
|
+
let current = getRootDeclaration(node);
|
|
2566
|
+
for (;;) {
|
|
2567
|
+
const parent = current.parent;
|
|
2568
|
+
if (parent === undefined) return current;
|
|
2569
|
+
switch (current.kind) {
|
|
2570
|
+
case SyntaxKind.VariableDeclaration:
|
|
2571
|
+
case SyntaxKind.VariableDeclarationList:
|
|
2572
|
+
case SyntaxKind.ImportSpecifier:
|
|
2573
|
+
case SyntaxKind.NamedImports:
|
|
2574
|
+
case SyntaxKind.NamespaceImport:
|
|
2575
|
+
case SyntaxKind.ImportClause:
|
|
2576
|
+
current = parent;
|
|
2577
|
+
continue;
|
|
2578
|
+
}
|
|
2579
|
+
return parent;
|
|
2580
|
+
}
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2583
|
+
/** `export = e` and `export default e`; the grammar checker reports misplaced ones. */
|
|
2584
|
+
export function checkExportAssignment(c: Checker, node: Node): void {
|
|
2585
|
+
if (!isInModuleElementContext(node)) return;
|
|
2586
|
+
const declaration = node as ExportAssignment;
|
|
2587
|
+
const isExportEquals = declaration.isExportEquals;
|
|
2588
|
+
const ambient = (node.flags & NodeFlags.Ambient) !== 0;
|
|
2589
|
+
if (shouldCheckErasableSyntax(c, node) && isExportEquals && !ambient)
|
|
2590
|
+
c.error(node, Diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled);
|
|
2591
|
+
const container = node.parent!.kind === SyntaxKind.SourceFile ? node.parent! : node.parent!.parent!;
|
|
2592
|
+
if (container.kind === SyntaxKind.ModuleDeclaration && !isAmbientModule(container)) return;
|
|
2593
|
+
const verbatimModuleSyntax = c.compilerOptions.verbatimModuleSyntax === true;
|
|
2594
|
+
const isIllegalExportDefaultInCJS =
|
|
2595
|
+
!isExportEquals &&
|
|
2596
|
+
!ambient &&
|
|
2597
|
+
verbatimModuleSyntax &&
|
|
2598
|
+
c.host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS;
|
|
2599
|
+
const expression = declaration.expression;
|
|
2600
|
+
if (expression.kind === SyntaxKind.Identifier) {
|
|
2601
|
+
const id = expression;
|
|
2602
|
+
const resolved = resolveEntityName(c, id, SymbolFlags.All, true, true, node);
|
|
2603
|
+
if (resolved !== undefined) {
|
|
2604
|
+
const sym = getExportSymbolOfValueSymbolIfExported(c, resolved);
|
|
2605
|
+
if (!ambient) markAliasReferenced(c, sym, id);
|
|
2606
|
+
const typeOnlyDeclaration = getTypeOnlyAliasDeclarationOfMeaning(c, sym, SymbolFlags.Value);
|
|
2607
|
+
const name = getText(id);
|
|
2608
|
+
// A non-value is exported as a type, like `export { Id as default }`.
|
|
2609
|
+
if ((getSymbolFlags(c, sym) & SymbolFlags.Value) !== 0) {
|
|
2610
|
+
checkExpressionCached(c, id);
|
|
2611
|
+
if (!isIllegalExportDefaultInCJS && !ambient && verbatimModuleSyntax && typeOnlyDeclaration !== undefined)
|
|
2612
|
+
c.error(
|
|
2613
|
+
id,
|
|
2614
|
+
isExportEquals
|
|
2615
|
+
? Diagnostics.An_export_declaration_must_reference_a_real_value_when_verbatimModuleSyntax_is_enabled_but_0_resolves_to_a_type_only_declaration
|
|
2616
|
+
: Diagnostics.An_export_default_must_reference_a_real_value_when_verbatimModuleSyntax_is_enabled_but_0_resolves_to_a_type_only_declaration,
|
|
2617
|
+
name,
|
|
2618
|
+
);
|
|
2619
|
+
} else if (!isIllegalExportDefaultInCJS && !ambient && verbatimModuleSyntax)
|
|
2620
|
+
c.error(
|
|
2621
|
+
id,
|
|
2622
|
+
isExportEquals
|
|
2623
|
+
? Diagnostics.An_export_declaration_must_reference_a_value_when_verbatimModuleSyntax_is_enabled_but_0_only_refers_to_a_type
|
|
2624
|
+
: Diagnostics.An_export_default_must_reference_a_value_when_verbatimModuleSyntax_is_enabled_but_0_only_refers_to_a_type,
|
|
2625
|
+
name,
|
|
2626
|
+
);
|
|
2627
|
+
if (!isIllegalExportDefaultInCJS && !ambient && c.isolatedModules && (sym.flags & SymbolFlags.Value) === 0)
|
|
2628
|
+
checkIsolatedModulesExportAssignment(c, node, sym, typeOnlyDeclaration, isExportEquals);
|
|
2629
|
+
} else checkExpressionCached(c, id);
|
|
2630
|
+
} else checkExpressionCached(c, expression);
|
|
2631
|
+
if (isIllegalExportDefaultInCJS) c.error(node, verbatimModuleSyntaxErrorMessage(node));
|
|
2632
|
+
checkExternalModuleExports(c, container);
|
|
2633
|
+
// A JSDoc `@type` on `export default`.
|
|
2634
|
+
const typeNode = getType(node);
|
|
2635
|
+
if (typeNode !== undefined) {
|
|
2636
|
+
const expr = getExpression(node)!;
|
|
2637
|
+
checkTypeAssignableToAndOptionallyElaborate(
|
|
2638
|
+
c,
|
|
2639
|
+
checkExpressionCached(c, expr),
|
|
2640
|
+
getTypeFromTypeNode(c, typeNode),
|
|
2641
|
+
expr,
|
|
2642
|
+
expr,
|
|
2643
|
+
);
|
|
2644
|
+
}
|
|
2645
|
+
}
|
|
2646
|
+
|
|
2647
|
+
function checkIsolatedModulesExportAssignment(
|
|
2648
|
+
c: Checker,
|
|
2649
|
+
node: Node,
|
|
2650
|
+
sym: Symbol,
|
|
2651
|
+
typeOnlyDeclaration: Node | undefined,
|
|
2652
|
+
isExportEquals: boolean,
|
|
2653
|
+
): void {
|
|
2654
|
+
const id = getExpression(node)!;
|
|
2655
|
+
const name = getText(id);
|
|
2656
|
+
const nonLocalMeanings = getSymbolFlagsEx(c, sym, false, true);
|
|
2657
|
+
const sameFile =
|
|
2658
|
+
typeOnlyDeclaration !== undefined && getSourceFileOfNode(typeOnlyDeclaration) === getSourceFileOfNode(node);
|
|
2659
|
+
if (
|
|
2660
|
+
(sym.flags & SymbolFlags.Alias) !== 0 &&
|
|
2661
|
+
(nonLocalMeanings & SymbolFlags.Type) !== 0 &&
|
|
2662
|
+
(nonLocalMeanings & SymbolFlags.Value) === 0 &&
|
|
2663
|
+
!sameFile
|
|
2664
|
+
)
|
|
2665
|
+
c.error(
|
|
2666
|
+
id,
|
|
2667
|
+
isExportEquals
|
|
2668
|
+
? Diagnostics.X_0_resolves_to_a_type_and_must_be_marked_type_only_in_this_file_before_re_exporting_when_1_is_enabled_Consider_using_import_type_where_0_is_imported
|
|
2669
|
+
: Diagnostics.X_0_resolves_to_a_type_and_must_be_marked_type_only_in_this_file_before_re_exporting_when_1_is_enabled_Consider_using_export_type_0_as_default,
|
|
2670
|
+
name,
|
|
2671
|
+
getIsolatedModulesLikeFlagName(c),
|
|
2672
|
+
);
|
|
2673
|
+
else if (typeOnlyDeclaration !== undefined && !sameFile)
|
|
2674
|
+
addTypeOnlyDeclarationRelatedInfo(
|
|
2675
|
+
c.error(
|
|
2676
|
+
id,
|
|
2677
|
+
isExportEquals
|
|
2678
|
+
? Diagnostics.X_0_resolves_to_a_type_only_declaration_and_must_be_marked_type_only_in_this_file_before_re_exporting_when_1_is_enabled_Consider_using_import_type_where_0_is_imported
|
|
2679
|
+
: Diagnostics.X_0_resolves_to_a_type_only_declaration_and_must_be_marked_type_only_in_this_file_before_re_exporting_when_1_is_enabled_Consider_using_export_type_0_as_default,
|
|
2680
|
+
name,
|
|
2681
|
+
getIsolatedModulesLikeFlagName(c),
|
|
2682
|
+
),
|
|
2683
|
+
typeOnlyDeclaration,
|
|
2684
|
+
name,
|
|
2685
|
+
);
|
|
2686
|
+
}
|
|
2687
|
+
|
|
2688
|
+
/** Reports aliases that conflict with local declarations, and type-only rules of isolated modules. */
|
|
2689
|
+
export function checkAliasSymbol(c: Checker, node: Node): void {
|
|
2690
|
+
const aliasSymbol = getSymbolOfDeclaration(c, node);
|
|
2691
|
+
if (aliasSymbol === undefined) return;
|
|
2692
|
+
const target = resolveAlias(c, aliasSymbol);
|
|
2693
|
+
if (target === c.unknownSymbol) return;
|
|
2694
|
+
// The local symbol merges other local (and exported) declarations, whose meanings the target can't have.
|
|
2695
|
+
const symbol = getMergedSymbol(c, aliasSymbol.exportSymbol ?? aliasSymbol);
|
|
2696
|
+
const targetFlags = getSymbolFlags(c, target);
|
|
2697
|
+
// Type-only imports and exports are grammar errors in JavaScript.
|
|
2698
|
+
if (isInJSFile(node) && (targetFlags & SymbolFlags.Value) === 0 && !isTypeOnlyImportOrExportDeclaration(node)) {
|
|
2699
|
+
reportJSTypeAlias(c, node, symbol);
|
|
2700
|
+
return;
|
|
2701
|
+
}
|
|
2702
|
+
const excludedMeanings =
|
|
2703
|
+
((symbol.flags & (SymbolFlags.Value | SymbolFlags.ExportValue)) !== 0 ? SymbolFlags.Value : 0) |
|
|
2704
|
+
((symbol.flags & SymbolFlags.Type) !== 0 ? SymbolFlags.Type : 0) |
|
|
2705
|
+
((symbol.flags & SymbolFlags.Namespace) !== 0 ? SymbolFlags.Namespace : 0);
|
|
2706
|
+
if ((targetFlags & excludedMeanings) !== 0)
|
|
2707
|
+
c.error(
|
|
2708
|
+
node,
|
|
2709
|
+
node.kind === SyntaxKind.ExportSpecifier
|
|
2710
|
+
? Diagnostics.Export_declaration_conflicts_with_exported_declaration_of_0
|
|
2711
|
+
: Diagnostics.Import_declaration_conflicts_with_local_declaration_of_0,
|
|
2712
|
+
symbolToString(c, symbol),
|
|
2713
|
+
);
|
|
2714
|
+
else if (
|
|
2715
|
+
node.kind !== SyntaxKind.ExportSpecifier &&
|
|
2716
|
+
c.compilerOptions.isolatedModules === true &&
|
|
2717
|
+
!isPartOfTypeOnlyImportOrExportDeclaration(node) &&
|
|
2718
|
+
(symbol.flags & (SymbolFlags.Value | SymbolFlags.ExportValue)) !== 0
|
|
2719
|
+
)
|
|
2720
|
+
// A transpiler sees a value import that collides with the local value.
|
|
2721
|
+
c.error(
|
|
2722
|
+
node,
|
|
2723
|
+
Diagnostics.Import_0_conflicts_with_local_value_so_must_be_declared_with_a_type_only_import_when_isolatedModules_is_enabled,
|
|
2724
|
+
symbolToString(c, symbol),
|
|
2725
|
+
getIsolatedModulesLikeFlagName(c),
|
|
2726
|
+
);
|
|
2727
|
+
if (c.isolatedModules && !isTypeOnlyImportOrExportDeclaration(node) && (node.flags & NodeFlags.Ambient) === 0)
|
|
2728
|
+
checkIsolatedModulesAlias(c, node, symbol, target, targetFlags);
|
|
2729
|
+
if (node.kind === SyntaxKind.ImportSpecifier) checkDeprecatedImportSpecifier(c, node, symbol);
|
|
2730
|
+
}
|
|
2731
|
+
|
|
2732
|
+
function reportJSTypeAlias(c: Checker, node: Node, symbol: Symbol): void {
|
|
2733
|
+
const errorNode = getPropertyName(node) ?? getName(node) ?? node;
|
|
2734
|
+
if (node.kind === SyntaxKind.ExportSpecifier) {
|
|
2735
|
+
c.error(errorNode, Diagnostics.Types_cannot_appear_in_export_declarations_in_JavaScript_files);
|
|
2736
|
+
return;
|
|
2737
|
+
}
|
|
2738
|
+
const identifierText = errorNode.kind === SyntaxKind.Identifier ? getText(errorNode) : symbol.name;
|
|
2739
|
+
let specifierText = '...';
|
|
2740
|
+
let declaration: Node | undefined = node;
|
|
2741
|
+
while (
|
|
2742
|
+
declaration !== undefined &&
|
|
2743
|
+
declaration.kind !== SyntaxKind.ImportDeclaration &&
|
|
2744
|
+
declaration.kind !== SyntaxKind.ImportEqualsDeclaration &&
|
|
2745
|
+
declaration.kind !== SyntaxKind.VariableDeclaration
|
|
2746
|
+
)
|
|
2747
|
+
declaration = declaration.parent;
|
|
2748
|
+
if (declaration !== undefined) {
|
|
2749
|
+
const specifier =
|
|
2750
|
+
declaration.kind === SyntaxKind.VariableDeclaration
|
|
2751
|
+
? isVariableDeclarationInitializedToRequire(declaration)
|
|
2752
|
+
? (getInitializer(declaration) as CallExpression).arguments.nodes[0]!
|
|
2753
|
+
: undefined
|
|
2754
|
+
: getExternalModuleName(declaration);
|
|
2755
|
+
if (specifier !== undefined && isStringLiteralLike(specifier)) specifierText = getText(specifier);
|
|
2756
|
+
}
|
|
2757
|
+
let importText = 'import("' + specifierText + '")';
|
|
2758
|
+
if (node.kind === SyntaxKind.ImportSpecifier) importText += '.' + identifierText;
|
|
2759
|
+
c.error(
|
|
2760
|
+
errorNode,
|
|
2761
|
+
Diagnostics.X_0_is_a_type_and_cannot_be_imported_in_JavaScript_files_Use_1_in_a_JSDoc_type_annotation,
|
|
2762
|
+
identifierText,
|
|
2763
|
+
importText,
|
|
2764
|
+
);
|
|
2765
|
+
}
|
|
2766
|
+
|
|
2767
|
+
function checkIsolatedModulesAlias(
|
|
2768
|
+
c: Checker,
|
|
2769
|
+
node: Node,
|
|
2770
|
+
symbol: Symbol,
|
|
2771
|
+
target: Symbol,
|
|
2772
|
+
targetFlags: SymbolFlags,
|
|
2773
|
+
): void {
|
|
2774
|
+
const verbatimModuleSyntax = c.compilerOptions.verbatimModuleSyntax === true;
|
|
2775
|
+
const typeOnlyAlias = getTypeOnlyAliasDeclaration(c, symbol);
|
|
2776
|
+
const isType = (targetFlags & SymbolFlags.Value) === 0;
|
|
2777
|
+
if (isType || typeOnlyAlias !== undefined) {
|
|
2778
|
+
switch (node.kind) {
|
|
2779
|
+
case SyntaxKind.ImportClause:
|
|
2780
|
+
case SyntaxKind.ImportSpecifier:
|
|
2781
|
+
case SyntaxKind.ImportEqualsDeclaration: {
|
|
2782
|
+
if (verbatimModuleSyntax) {
|
|
2783
|
+
const message =
|
|
2784
|
+
node.kind === SyntaxKind.ImportEqualsDeclaration &&
|
|
2785
|
+
(node as ImportEqualsDeclaration).moduleReference.kind !== SyntaxKind.ExternalModuleReference
|
|
2786
|
+
? Diagnostics.An_import_alias_cannot_resolve_to_a_type_or_type_only_declaration_when_verbatimModuleSyntax_is_enabled
|
|
2787
|
+
: isType
|
|
2788
|
+
? Diagnostics.X_0_is_a_type_and_must_be_imported_using_a_type_only_import_when_verbatimModuleSyntax_is_enabled
|
|
2789
|
+
: Diagnostics.X_0_resolves_to_a_type_only_declaration_and_must_be_imported_using_a_type_only_import_when_verbatimModuleSyntax_is_enabled;
|
|
2790
|
+
const name = getText(getPropertyNameOrName(node));
|
|
2791
|
+
addTypeOnlyDeclarationRelatedInfo(c.error(node, message, name), isType ? undefined : typeOnlyAlias, name);
|
|
2792
|
+
}
|
|
2793
|
+
if (
|
|
2794
|
+
isType &&
|
|
2795
|
+
node.kind === SyntaxKind.ImportEqualsDeclaration &&
|
|
2796
|
+
hasSyntacticModifier(node, ModifierFlags.Export)
|
|
2797
|
+
)
|
|
2798
|
+
c.error(
|
|
2799
|
+
node,
|
|
2800
|
+
Diagnostics.Cannot_use_export_import_on_a_type_or_type_only_namespace_when_0_is_enabled,
|
|
2801
|
+
getIsolatedModulesLikeFlagName(c),
|
|
2802
|
+
);
|
|
2803
|
+
break;
|
|
2804
|
+
}
|
|
2805
|
+
case SyntaxKind.ExportSpecifier:
|
|
2806
|
+
// `import type { A } from './a'; export { A }` is fine: one file shows the export is a type.
|
|
2807
|
+
if (
|
|
2808
|
+
verbatimModuleSyntax ||
|
|
2809
|
+
typeOnlyAlias === undefined ||
|
|
2810
|
+
getSourceFileOfNode(typeOnlyAlias) !== getSourceFileOfNode(node)
|
|
2811
|
+
) {
|
|
2812
|
+
const name = getText(getPropertyNameOrName(node));
|
|
2813
|
+
const diagnostic = isType
|
|
2814
|
+
? c.error(
|
|
2815
|
+
node,
|
|
2816
|
+
Diagnostics.Re_exporting_a_type_when_0_is_enabled_requires_using_export_type,
|
|
2817
|
+
getIsolatedModulesLikeFlagName(c),
|
|
2818
|
+
)
|
|
2819
|
+
: c.error(
|
|
2820
|
+
node,
|
|
2821
|
+
Diagnostics.X_0_resolves_to_a_type_only_declaration_and_must_be_re_exported_using_a_type_only_re_export_when_1_is_enabled,
|
|
2822
|
+
name,
|
|
2823
|
+
getIsolatedModulesLikeFlagName(c),
|
|
2824
|
+
);
|
|
2825
|
+
addTypeOnlyDeclarationRelatedInfo(diagnostic, isType ? undefined : typeOnlyAlias, name);
|
|
2826
|
+
}
|
|
2827
|
+
break;
|
|
2828
|
+
}
|
|
2829
|
+
}
|
|
2830
|
+
if (
|
|
2831
|
+
verbatimModuleSyntax &&
|
|
2832
|
+
node.kind !== SyntaxKind.ImportEqualsDeclaration &&
|
|
2833
|
+
!isInJSFile(node) &&
|
|
2834
|
+
c.host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS
|
|
2835
|
+
)
|
|
2836
|
+
c.error(node, verbatimModuleSyntaxErrorMessage(node));
|
|
2837
|
+
else if (
|
|
2838
|
+
getEmitModuleKind(c.compilerOptions) === ModuleKind.Preserve &&
|
|
2839
|
+
node.kind !== SyntaxKind.ImportEqualsDeclaration &&
|
|
2840
|
+
node.kind !== SyntaxKind.VariableDeclaration &&
|
|
2841
|
+
c.host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS
|
|
2842
|
+
)
|
|
2843
|
+
// Under `--module preserve` ESM syntax emits as ESM, so it is an error in a file that is CommonJS (.cts, .cjs).
|
|
2844
|
+
c.error(
|
|
2845
|
+
node,
|
|
2846
|
+
Diagnostics.ECMAScript_module_syntax_is_not_allowed_in_a_CommonJS_module_when_module_is_set_to_preserve,
|
|
2847
|
+
);
|
|
2848
|
+
if (
|
|
2849
|
+
verbatimModuleSyntax &&
|
|
2850
|
+
(targetFlags & SymbolFlags.ConstEnum) !== 0 &&
|
|
2851
|
+
target.valueDeclaration !== undefined &&
|
|
2852
|
+
(target.valueDeclaration.flags & NodeFlags.Ambient) !== 0
|
|
2853
|
+
)
|
|
2854
|
+
c.error(node, Diagnostics.Cannot_access_ambient_const_enums_when_0_is_enabled, getIsolatedModulesLikeFlagName(c));
|
|
2855
|
+
}
|
|
2856
|
+
|
|
2857
|
+
/** The error for ESM syntax in a CommonJS file under `verbatimModuleSyntax`: shorter in a .cts or .cjs file. */
|
|
2858
|
+
function verbatimModuleSyntaxErrorMessage(node: Node): DiagnosticMessage {
|
|
2859
|
+
return fileExtensionIsOneOf(getSourceFileOfNode(node).fileName, ['.cts', '.cjs'])
|
|
2860
|
+
? Diagnostics.ECMAScript_imports_and_exports_cannot_be_written_in_a_CommonJS_file_under_verbatimModuleSyntax
|
|
2861
|
+
: Diagnostics.ECMAScript_imports_and_exports_cannot_be_written_in_a_CommonJS_file_under_verbatimModuleSyntax_Adjust_the_type_field_in_the_nearest_package_json_to_make_this_file_an_ECMAScript_module_or_adjust_your_verbatimModuleSyntax_module_and_moduleResolution_settings_in_TypeScript;
|
|
2862
|
+
}
|
|
2863
|
+
|
|
2864
|
+
function isPartOfTypeOnlyImportOrExportDeclaration(node: Node): boolean {
|
|
2865
|
+
for (let current: Node | undefined = node; current !== undefined; current = current.parent)
|
|
2866
|
+
if (isTypeOnlyImportOrExportDeclaration(current)) return true;
|
|
2867
|
+
return false;
|
|
2868
|
+
}
|
|
2869
|
+
|
|
2870
|
+
export function getIsolatedModulesLikeFlagName(c: Checker): string {
|
|
2871
|
+
return c.compilerOptions.verbatimModuleSyntax === true ? 'verbatimModuleSyntax' : 'isolatedModules';
|
|
2872
|
+
}
|
|
2873
|
+
|
|
2874
|
+
/** The file a module declaration or import names. */
|
|
2875
|
+
export function getExternalModuleFileFromDeclaration(c: Checker, declaration: Node): SourceFile | undefined {
|
|
2876
|
+
let specifier: Node | undefined = undefined;
|
|
2877
|
+
if (declaration.kind === SyntaxKind.ModuleDeclaration) {
|
|
2878
|
+
const name = getName(declaration);
|
|
2879
|
+
if (name !== undefined && name.kind === SyntaxKind.StringLiteral) specifier = name;
|
|
2880
|
+
} else specifier = getExternalModuleName(declaration);
|
|
2881
|
+
if (specifier === undefined) return undefined;
|
|
2882
|
+
const moduleSymbol = resolveExternalModuleNameWorker(c, specifier, specifier, undefined, false, false);
|
|
2883
|
+
if (moduleSymbol === undefined) return undefined;
|
|
2884
|
+
const file = getDeclarationOfKind(moduleSymbol, SyntaxKind.SourceFile);
|
|
2885
|
+
return file === undefined ? undefined : (file as SourceFile);
|
|
2886
|
+
}
|