tsc-ts 0.0.4 → 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 +33 -1
- 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 +3 -2
- package/dist/api/library/ast.generated.d.ts +127 -139
- package/dist/api/library/checker.d.ts +51 -0
- package/dist/api/library/checker.js +318 -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/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 +5 -1
- package/dist/api/library/index.js +4 -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/types.d.ts +14 -1
- package/package.json +12 -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 +141 -167
- package/src/library/ast.ts +4 -1
- package/src/library/checker.ts +384 -0
- package/src/library/diagnostics.ts +72 -0
- package/src/library/disk.ts +73 -0
- package/src/library/errors.ts +26 -0
- package/src/library/host.ts +63 -0
- package/src/library/index.ts +21 -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/types.ts +14 -1
|
@@ -0,0 +1,2380 @@
|
|
|
1
|
+
// The declaration transform: turns a source file into its declaration file. Keeps exported and
|
|
2
|
+
// referenced declarations without their implementations, writes the types of declarations from
|
|
3
|
+
// their annotations or the resolver, and elides imports the output doesn't use.
|
|
4
|
+
import { getJSDoc } from '../parser.js';
|
|
5
|
+
import { BindingElement, BindingPattern, CallSignatureDeclaration, ClassDeclaration, ClassExpression, ComputedPropertyName, ConditionalTypeNode, ConstructSignatureDeclaration, ConstructorDeclaration, ConstructorTypeNode, EnumDeclaration, EnumMember, ExportAssignment, ExportDeclaration, ExpressionWithTypeArguments, ExternalModuleReference, FileReference, ArrayTypeNode, ArrowFunction, BinaryExpression, TypeLiteralNode, UnionTypeNode, ElementAccessExpression, ExportSpecifier, FunctionDeclaration, FunctionExpression, FunctionTypeNode, ImportSpecifier, NodeVisitor, PropertyAccessExpression, QualifiedName, getArguments, GetAccessorDeclaration, HeritageClause, Identifier, ImportClause, ImportDeclaration, ImportEqualsDeclaration, ImportTypeNode, IndexSignatureDeclaration, InterfaceDeclaration, KeywordTypeNode, LiteralTypeNode, MappedTypeNode, MethodDeclaration, MethodSignatureDeclaration, ModuleBlock, ModuleDeclaration, NamedExports, NamedImports, NamespaceImport, NodeList, NumericLiteral, ParameterDeclaration, PrefixUnaryExpression, PrivateIdentifier, PropertyDeclaration, PropertySignatureDeclaration, JSDoc, JSDocText, SetAccessorDeclaration, SourceFile, StringLiteral, SyntaxList, TypeAliasDeclaration, TypeParameterDeclaration, VariableDeclaration, VariableStatement, getExpression, getHeritageClauses, getMembers, getInitializer, getModifiers, getName, getParameters, getSymbol, getText, getType, getTypeParameters, isDeclarationNode, } from '../ast.js';
|
|
6
|
+
import { isAmbientModule, isAnyImportOrReExport, isFunctionLike, isGlobalScopeAugmentation, isInJSFile, isLiteralImportTypeNode, modifierToFlag, nodeIsMissing, nodeIsPresent, } from '../ast-utilities.js';
|
|
7
|
+
import { getAssignmentDeclarationKind, getCombinedModifierFlags, getCombinedNodeFlags, getElementOrPropertyAccessName, getThisContainer, isVariableDeclarationInitializedToRequire, getSourceFileOfNode, getSourceTextOfNode, hasDynamicName, hasSyntacticModifier, isEntityNameExpression, isExternalOrCommonJSModule, isStatic, } from '../binder-utilities.js';
|
|
8
|
+
import { getOutputPathsFor } from '../output-paths.js';
|
|
9
|
+
import { getDirectoryPath, normalizeSlashes } from '../path.js';
|
|
10
|
+
import { isModifier } from '../printer/utilities.js';
|
|
11
|
+
import { getLeadingCommentRanges, getTrailingCommentRanges, identifierToKeywordKind, isIdentifierText, skipTrivia, stringToToken, } from '../scanner.js';
|
|
12
|
+
import { getRelativePathToDirectoryOrUrl } from '../sourcemap.js';
|
|
13
|
+
import { DiagnosticContext, canProduceDiagnostics, createDiagnosticForNode, declarationContext, declarationNameContext, noDiagnosticContext, } from './declarations-diagnostics.js';
|
|
14
|
+
import { DeclarationTracker } from './declarations-tracker.js';
|
|
15
|
+
import { Transformer } from './transformer.js';
|
|
16
|
+
export class DeclarationTransformer extends Transformer {
|
|
17
|
+
host;
|
|
18
|
+
options;
|
|
19
|
+
resolver;
|
|
20
|
+
tracker;
|
|
21
|
+
declarationFilePath;
|
|
22
|
+
needsDeclare;
|
|
23
|
+
needsScopeFixMarker;
|
|
24
|
+
resultHasScopeMarker;
|
|
25
|
+
resultHasExternalModuleIndicator;
|
|
26
|
+
enclosingDeclaration;
|
|
27
|
+
/** Whether types are inside an object type, where new diagnostic contexts aren't entered. */
|
|
28
|
+
suppressNewDiagnosticContexts;
|
|
29
|
+
inClassExpressionDeclaration;
|
|
30
|
+
/** Transformed top-level declarations by original node; undefined for removed ones. */
|
|
31
|
+
lateStatementReplacements;
|
|
32
|
+
/** Functions with assigned properties (`f.x = 1`), transformed, by statement. */
|
|
33
|
+
expandoHosts;
|
|
34
|
+
/** The namespace members for the assigned properties of each function, by statement. */
|
|
35
|
+
expandoMembers;
|
|
36
|
+
/** The `export =` for a CommonJS `module.exports = …`. */
|
|
37
|
+
cjsExportAssignment;
|
|
38
|
+
/** The exports for CommonJS `exports.x = …` assignments. */
|
|
39
|
+
cjsExportMembers;
|
|
40
|
+
/** The name of the `module.exports` value, which `exports.x` members are nested in. */
|
|
41
|
+
cjsExportAssignmentName;
|
|
42
|
+
witnessedCjsExports;
|
|
43
|
+
/** `this.x` members found in a JavaScript class, and their keys. */
|
|
44
|
+
thisPropertyAssignments;
|
|
45
|
+
seenProperties;
|
|
46
|
+
constructor(context, host, options, resolver, declarationFilePath) {
|
|
47
|
+
super(context);
|
|
48
|
+
this.host = host;
|
|
49
|
+
this.options = options;
|
|
50
|
+
this.resolver = resolver;
|
|
51
|
+
this.tracker = new DeclarationTracker(resolver, options.isolatedDeclarations === true, options.stripInternal === true);
|
|
52
|
+
this.declarationFilePath = declarationFilePath;
|
|
53
|
+
this.needsDeclare = true;
|
|
54
|
+
this.needsScopeFixMarker = false;
|
|
55
|
+
this.resultHasScopeMarker = false;
|
|
56
|
+
this.resultHasExternalModuleIndicator = false;
|
|
57
|
+
this.enclosingDeclaration = context.factory.createToken(0 /* SyntaxKind.Unknown */);
|
|
58
|
+
this.suppressNewDiagnosticContexts = false;
|
|
59
|
+
this.inClassExpressionDeclaration = false;
|
|
60
|
+
this.lateStatementReplacements = new Map();
|
|
61
|
+
this.expandoHosts = new Map();
|
|
62
|
+
this.expandoMembers = new Map();
|
|
63
|
+
this.cjsExportAssignment = undefined;
|
|
64
|
+
this.cjsExportMembers = [];
|
|
65
|
+
this.cjsExportAssignmentName = undefined;
|
|
66
|
+
this.witnessedCjsExports = new Set();
|
|
67
|
+
this.thisPropertyAssignments = [];
|
|
68
|
+
this.seenProperties = new Set();
|
|
69
|
+
}
|
|
70
|
+
get diagnostics() {
|
|
71
|
+
return this.tracker.diagnostics;
|
|
72
|
+
}
|
|
73
|
+
visit(node) {
|
|
74
|
+
switch (node.kind) {
|
|
75
|
+
case 307 /* SyntaxKind.SourceFile */:
|
|
76
|
+
if (node instanceof SourceFile) {
|
|
77
|
+
const narrowed = node;
|
|
78
|
+
return this.visitSourceFile(narrowed);
|
|
79
|
+
}
|
|
80
|
+
break;
|
|
81
|
+
case 263 /* SyntaxKind.FunctionDeclaration */:
|
|
82
|
+
case 268 /* SyntaxKind.ModuleDeclaration */:
|
|
83
|
+
case 272 /* SyntaxKind.ImportEqualsDeclaration */:
|
|
84
|
+
case 265 /* SyntaxKind.InterfaceDeclaration */:
|
|
85
|
+
case 264 /* SyntaxKind.ClassDeclaration */:
|
|
86
|
+
case 345 /* SyntaxKind.JSTypeAliasDeclaration */:
|
|
87
|
+
case 266 /* SyntaxKind.TypeAliasDeclaration */:
|
|
88
|
+
case 267 /* SyntaxKind.EnumDeclaration */:
|
|
89
|
+
case 244 /* SyntaxKind.VariableStatement */:
|
|
90
|
+
case 273 /* SyntaxKind.ImportDeclaration */:
|
|
91
|
+
case 346 /* SyntaxKind.JSImportDeclaration */:
|
|
92
|
+
case 279 /* SyntaxKind.ExportDeclaration */:
|
|
93
|
+
case 278 /* SyntaxKind.ExportAssignment */:
|
|
94
|
+
return this.visitDeclarationStatement(node);
|
|
95
|
+
case 253 /* SyntaxKind.BreakStatement */:
|
|
96
|
+
case 252 /* SyntaxKind.ContinueStatement */:
|
|
97
|
+
case 260 /* SyntaxKind.DebuggerStatement */:
|
|
98
|
+
case 247 /* SyntaxKind.DoStatement */:
|
|
99
|
+
case 243 /* SyntaxKind.EmptyStatement */:
|
|
100
|
+
case 250 /* SyntaxKind.ForInStatement */:
|
|
101
|
+
case 251 /* SyntaxKind.ForOfStatement */:
|
|
102
|
+
case 249 /* SyntaxKind.ForStatement */:
|
|
103
|
+
case 246 /* SyntaxKind.IfStatement */:
|
|
104
|
+
case 257 /* SyntaxKind.LabeledStatement */:
|
|
105
|
+
case 254 /* SyntaxKind.ReturnStatement */:
|
|
106
|
+
case 256 /* SyntaxKind.SwitchStatement */:
|
|
107
|
+
case 258 /* SyntaxKind.ThrowStatement */:
|
|
108
|
+
case 259 /* SyntaxKind.TryStatement */:
|
|
109
|
+
case 248 /* SyntaxKind.WhileStatement */:
|
|
110
|
+
case 255 /* SyntaxKind.WithStatement */:
|
|
111
|
+
case 347 /* SyntaxKind.NotEmittedStatement */:
|
|
112
|
+
case 242 /* SyntaxKind.Block */:
|
|
113
|
+
case 283 /* SyntaxKind.MissingDeclaration */:
|
|
114
|
+
case 245 /* SyntaxKind.ExpressionStatement */:
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
return this.visitDeclarationSubtree(node);
|
|
118
|
+
}
|
|
119
|
+
// Source files
|
|
120
|
+
visitSourceFile(file) {
|
|
121
|
+
if (file.isDeclarationFile)
|
|
122
|
+
return file;
|
|
123
|
+
this.needsDeclare = true;
|
|
124
|
+
this.needsScopeFixMarker = false;
|
|
125
|
+
this.resultHasScopeMarker = false;
|
|
126
|
+
this.resultHasExternalModuleIndicator = false;
|
|
127
|
+
this.enclosingDeclaration = file;
|
|
128
|
+
this.suppressNewDiagnosticContexts = false;
|
|
129
|
+
this.lateStatementReplacements = new Map();
|
|
130
|
+
this.expandoHosts.clear();
|
|
131
|
+
this.expandoMembers.clear();
|
|
132
|
+
this.witnessedCjsExports.clear();
|
|
133
|
+
this.cjsExportAssignment = undefined;
|
|
134
|
+
this.cjsExportAssignmentName = undefined;
|
|
135
|
+
this.cjsExportMembers = [];
|
|
136
|
+
const tracker = this.tracker;
|
|
137
|
+
tracker.diagnosticContext = noDiagnosticContext;
|
|
138
|
+
tracker.lateMarkedStatements = [];
|
|
139
|
+
tracker.currentSourceFile = file;
|
|
140
|
+
this.resolver.precalculateDeclarationEmitVisibility(this.originalSourceFile(file));
|
|
141
|
+
// `module.exports =` first: `exports.x` members are nested under its name.
|
|
142
|
+
if (file.commonJSModuleIndicator !== undefined)
|
|
143
|
+
file.forEachChild(new ExpressionWalker(this, 0 /* ExpressionWalkerKind.ModuleExports */));
|
|
144
|
+
file.forEachChild(new ExpressionWalker(this, 1 /* ExpressionWalkerKind.Members */));
|
|
145
|
+
const statements = this.visitNodes(file.statements);
|
|
146
|
+
let combined = this.appendCjsExports(this.transformAndReplaceLatePaintedStatements(statements));
|
|
147
|
+
combined.pos = statements.pos;
|
|
148
|
+
combined.end = statements.end;
|
|
149
|
+
if (isExternalOrCommonJSModule(file) && file.isJS())
|
|
150
|
+
this.reportMultipleModuleExports(file);
|
|
151
|
+
if (isExternalOrCommonJSModule(file) &&
|
|
152
|
+
(!this.resultHasExternalModuleIndicator || (this.needsScopeFixMarker && !this.resultHasScopeMarker))) {
|
|
153
|
+
const nodes = combined.nodes.slice();
|
|
154
|
+
nodes.push(this.createEmptyExports());
|
|
155
|
+
const withMarker = new NodeList(combined.pos, combined.end, nodes);
|
|
156
|
+
combined = withMarker;
|
|
157
|
+
}
|
|
158
|
+
const result = this.factory.updateSourceFile(file, combined, file.endOfFileToken);
|
|
159
|
+
// Always a new node: references and the declaration flag differ from the input.
|
|
160
|
+
const updated = result === file
|
|
161
|
+
? this.factory.updateSourceFile(file, new NodeList(combined.pos, combined.end, combined.nodes), file.endOfFileToken)
|
|
162
|
+
: result;
|
|
163
|
+
updated.isDeclarationFile = true;
|
|
164
|
+
updated.libReferenceDirectives = preservedReferences(file.libReferenceDirectives);
|
|
165
|
+
updated.typeReferenceDirectives = preservedReferences(file.typeReferenceDirectives);
|
|
166
|
+
updated.referencedFiles = this.getReferencedFiles(file, getDirectoryPath(normalizeSlashes(this.declarationFilePath)));
|
|
167
|
+
tracker.currentSourceFile = undefined;
|
|
168
|
+
this.cjsExportAssignment = undefined;
|
|
169
|
+
this.cjsExportAssignmentName = undefined;
|
|
170
|
+
this.cjsExportMembers = [];
|
|
171
|
+
return updated;
|
|
172
|
+
}
|
|
173
|
+
originalSourceFile(file) {
|
|
174
|
+
const original = this.context.mostOriginal(file);
|
|
175
|
+
if (original === undefined || original.kind !== 307 /* SyntaxKind.SourceFile */)
|
|
176
|
+
return file;
|
|
177
|
+
return original;
|
|
178
|
+
}
|
|
179
|
+
appendCjsExports(statements) {
|
|
180
|
+
if (this.cjsExportAssignment === undefined && this.cjsExportMembers.length === 0)
|
|
181
|
+
return statements;
|
|
182
|
+
const result = [];
|
|
183
|
+
if (this.cjsExportAssignment !== undefined)
|
|
184
|
+
appendFlattened(result, this.cjsExportAssignment);
|
|
185
|
+
for (const member of this.cjsExportMembers)
|
|
186
|
+
appendFlattened(result, member);
|
|
187
|
+
for (const statement of statements.nodes)
|
|
188
|
+
result.push(statement);
|
|
189
|
+
return this.factory.createNodeList(result);
|
|
190
|
+
}
|
|
191
|
+
reportMultipleModuleExports(file) {
|
|
192
|
+
const symbol = file.symbol;
|
|
193
|
+
const exportEquals = symbol === undefined || symbol.exports === undefined ? undefined : symbol.exports.get('export=');
|
|
194
|
+
if (exportEquals === undefined || exportEquals.declarations.length <= 1)
|
|
195
|
+
return false;
|
|
196
|
+
for (const declaration of exportEquals.declarations)
|
|
197
|
+
this.tracker.addDiagnostic(createDiagnosticForNode(declaration, 1513 /* Diagnostics.Multiple_module_exports_assignments_cannot_be_serialized_for_declaration_emit */, []));
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
createEmptyExports() {
|
|
201
|
+
return this.factory.created(new ExportDeclaration(undefined, false, this.factory.created(new NamedExports(this.factory.createNodeList([]))), undefined, undefined));
|
|
202
|
+
}
|
|
203
|
+
/** `/// <reference path>` directives with `preserve="true"`, relative to the output. */
|
|
204
|
+
getReferencedFiles(file, outputDirectory) {
|
|
205
|
+
const result = [];
|
|
206
|
+
for (const reference of file.referencedFiles) {
|
|
207
|
+
if (!reference.preserve)
|
|
208
|
+
continue;
|
|
209
|
+
const target = this.resolver.getSourceFileFromReference(file, reference);
|
|
210
|
+
if (target === undefined)
|
|
211
|
+
continue;
|
|
212
|
+
let fileName = target.fileName;
|
|
213
|
+
if (!target.isDeclarationFile) {
|
|
214
|
+
// The referenced file's declaration output, else its JavaScript output.
|
|
215
|
+
const paths = getOutputPathsFor(target.fileName, target.scriptKind === 6 /* ScriptKind.JSON */, this.options, this.host, true);
|
|
216
|
+
fileName = paths.declarationFilePath !== '' ? paths.declarationFilePath : paths.jsFilePath;
|
|
217
|
+
if (fileName === '')
|
|
218
|
+
fileName = target.fileName;
|
|
219
|
+
}
|
|
220
|
+
if (fileName === '')
|
|
221
|
+
continue;
|
|
222
|
+
const relative = getRelativePathToDirectoryOrUrl(outputDirectory, fileName, this.host.useCaseSensitiveFileNames, this.host.currentDirectory);
|
|
223
|
+
result.push(new FileReference(-1, -1, relative, reference.resolutionMode, reference.preserve));
|
|
224
|
+
}
|
|
225
|
+
return result;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Replaces top-level declarations with their transformed forms. Transforming a declaration can
|
|
229
|
+
* mark more unexported declarations as referenced, so this runs until none are left.
|
|
230
|
+
*/
|
|
231
|
+
transformAndReplaceLatePaintedStatements(statements) {
|
|
232
|
+
const tracker = this.tracker;
|
|
233
|
+
while (tracker.lateMarkedStatements.length > 0) {
|
|
234
|
+
const next = tracker.lateMarkedStatements.shift();
|
|
235
|
+
const saveNeedsDeclare = this.needsDeclare;
|
|
236
|
+
this.needsDeclare = next.parent !== undefined && next.parent.kind === 307 /* SyntaxKind.SourceFile */;
|
|
237
|
+
const result = this.transformTopLevelDeclaration(next);
|
|
238
|
+
this.needsDeclare = saveNeedsDeclare;
|
|
239
|
+
this.lateStatementReplacements.set(this.context.mostOriginal(next), result);
|
|
240
|
+
}
|
|
241
|
+
const results = [];
|
|
242
|
+
for (const statement of statements.nodes) {
|
|
243
|
+
if (!isLateVisibilityPaintedStatement(statement)) {
|
|
244
|
+
results.push(statement);
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
const original = this.context.mostOriginal(statement);
|
|
248
|
+
if (!this.lateStatementReplacements.has(original)) {
|
|
249
|
+
results.push(statement);
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
const replacement = this.lateStatementReplacements.get(original);
|
|
253
|
+
if (replacement === undefined)
|
|
254
|
+
continue;
|
|
255
|
+
const atTopLevel = statement.parent !== undefined && statement.parent.kind === 307 /* SyntaxKind.SourceFile */;
|
|
256
|
+
if (replacement instanceof SyntaxList) {
|
|
257
|
+
if (!this.needsScopeFixMarker || !this.resultHasExternalModuleIndicator)
|
|
258
|
+
for (const child of replacement.children) {
|
|
259
|
+
if (needsScopeMarker(child))
|
|
260
|
+
this.needsScopeFixMarker = true;
|
|
261
|
+
if (atTopLevel && isExternalModuleIndicator(child))
|
|
262
|
+
this.resultHasExternalModuleIndicator = true;
|
|
263
|
+
}
|
|
264
|
+
for (const child of replacement.children)
|
|
265
|
+
results.push(child);
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
if (needsScopeMarker(replacement))
|
|
269
|
+
this.needsScopeFixMarker = true;
|
|
270
|
+
if (atTopLevel && isExternalModuleIndicator(replacement))
|
|
271
|
+
this.resultHasExternalModuleIndicator = true;
|
|
272
|
+
results.push(replacement);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return this.factory.createNodeList(results);
|
|
276
|
+
}
|
|
277
|
+
// Diagnostic contexts
|
|
278
|
+
enterDiagnosticContext(input) {
|
|
279
|
+
const tracker = this.tracker;
|
|
280
|
+
const scope = new DiagnosticScope(tracker.diagnosticContext, tracker.errorNameNode, this.suppressNewDiagnosticContexts);
|
|
281
|
+
if (canProduceDiagnostics(input) && !this.suppressNewDiagnosticContexts)
|
|
282
|
+
tracker.diagnosticContext = declarationContext(input);
|
|
283
|
+
if ((input.kind === 188 /* SyntaxKind.TypeLiteral */ || input.kind === 201 /* SyntaxKind.MappedType */) &&
|
|
284
|
+
input.parent !== undefined &&
|
|
285
|
+
input.parent.kind !== 266 /* SyntaxKind.TypeAliasDeclaration */ &&
|
|
286
|
+
input.parent.kind !== 345 /* SyntaxKind.JSTypeAliasDeclaration */)
|
|
287
|
+
this.suppressNewDiagnosticContexts = true;
|
|
288
|
+
return scope;
|
|
289
|
+
}
|
|
290
|
+
exitDiagnosticContext(scope) {
|
|
291
|
+
this.tracker.diagnosticContext = scope.context;
|
|
292
|
+
this.tracker.errorNameNode = scope.errorNameNode;
|
|
293
|
+
this.suppressNewDiagnosticContexts = scope.suppressNewDiagnosticContexts;
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
// Declarations below the top level
|
|
297
|
+
visitDeclarationSubtree(input) {
|
|
298
|
+
if (input === undefined)
|
|
299
|
+
return undefined;
|
|
300
|
+
if (this.shouldStripInternal(input))
|
|
301
|
+
return undefined;
|
|
302
|
+
if (isDeclaration(input)) {
|
|
303
|
+
if (this.isDeclarationAndNotVisible(input))
|
|
304
|
+
return undefined;
|
|
305
|
+
if (hasDynamicName(input)) {
|
|
306
|
+
const expression = getExpression(getName(input));
|
|
307
|
+
if (this.tracker.isolatedDeclarations) {
|
|
308
|
+
// Computed names that aren't literal types can't be checked without types.
|
|
309
|
+
if (!this.resolver.isDefinitelyReferenceToGlobalSymbolObject(expression)) {
|
|
310
|
+
const parentKind = input.parent.kind;
|
|
311
|
+
if (parentKind === 264 /* SyntaxKind.ClassDeclaration */ || parentKind === 211 /* SyntaxKind.ObjectLiteralExpression */) {
|
|
312
|
+
this.tracker.addDiagnostic(createDiagnosticForNode(input, 1802 /* Diagnostics.Computed_property_names_on_class_or_object_literals_cannot_be_inferred_with_isolatedDeclarations */, []));
|
|
313
|
+
return undefined;
|
|
314
|
+
}
|
|
315
|
+
if ((parentKind === 265 /* SyntaxKind.InterfaceDeclaration */ || parentKind === 188 /* SyntaxKind.TypeLiteral */) &&
|
|
316
|
+
!isEntityNameExpression(expression, false)) {
|
|
317
|
+
this.tracker.addDiagnostic(createDiagnosticForNode(input, 1779 /* Diagnostics.Computed_properties_must_be_number_or_string_literals_variables_or_dotted_expressions_with_isolatedDeclarations */, []));
|
|
318
|
+
return undefined;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
else if (!this.resolver.isLateBound(this.parseNode(input)) || !isEntityNameExpression(expression, false))
|
|
323
|
+
return undefined;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
// Implementations of overloads are elided.
|
|
327
|
+
if (isFunctionLike(input) && this.resolver.isImplementationOfOverload(input))
|
|
328
|
+
return undefined;
|
|
329
|
+
if (input.kind === 241 /* SyntaxKind.SemicolonClassElement */)
|
|
330
|
+
return undefined;
|
|
331
|
+
if (input instanceof HeritageClause) {
|
|
332
|
+
const types = input.types.nodes;
|
|
333
|
+
if (types.length === 0 || (types.length === 1 && nodeIsMissing(types[0])))
|
|
334
|
+
return undefined;
|
|
335
|
+
}
|
|
336
|
+
const previousEnclosingDeclaration = this.enclosingDeclaration;
|
|
337
|
+
if (isEnclosingDeclaration(input))
|
|
338
|
+
this.enclosingDeclaration = input;
|
|
339
|
+
const canProduceDiagnostic = canProduceDiagnostics(input);
|
|
340
|
+
const scope = this.enterDiagnosticContext(input);
|
|
341
|
+
const result = this.transformDeclarationSubtree(input);
|
|
342
|
+
if (result !== undefined && canProduceDiagnostic && hasDynamicName(input))
|
|
343
|
+
this.checkName(input);
|
|
344
|
+
this.exitDiagnosticContext(scope);
|
|
345
|
+
this.enclosingDeclaration = previousEnclosingDeclaration;
|
|
346
|
+
return result;
|
|
347
|
+
}
|
|
348
|
+
transformDeclarationSubtree(input) {
|
|
349
|
+
switch (input.kind) {
|
|
350
|
+
case 201 /* SyntaxKind.MappedType */:
|
|
351
|
+
if (input instanceof MappedTypeNode) {
|
|
352
|
+
const narrowed = input;
|
|
353
|
+
return this.transformMappedType(narrowed);
|
|
354
|
+
}
|
|
355
|
+
break;
|
|
356
|
+
case 299 /* SyntaxKind.HeritageClause */:
|
|
357
|
+
if (input instanceof HeritageClause) {
|
|
358
|
+
const narrowed = input;
|
|
359
|
+
return this.transformHeritageClause(narrowed);
|
|
360
|
+
}
|
|
361
|
+
break;
|
|
362
|
+
case 174 /* SyntaxKind.MethodSignature */:
|
|
363
|
+
if (input instanceof MethodSignatureDeclaration) {
|
|
364
|
+
const narrowed = input;
|
|
365
|
+
return this.transformMethodSignature(narrowed);
|
|
366
|
+
}
|
|
367
|
+
break;
|
|
368
|
+
case 175 /* SyntaxKind.MethodDeclaration */:
|
|
369
|
+
if (input instanceof MethodDeclaration) {
|
|
370
|
+
const narrowed = input;
|
|
371
|
+
return this.transformMethodDeclaration(narrowed);
|
|
372
|
+
}
|
|
373
|
+
break;
|
|
374
|
+
case 181 /* SyntaxKind.ConstructSignature */:
|
|
375
|
+
if (input instanceof ConstructSignatureDeclaration) {
|
|
376
|
+
const narrowed = input;
|
|
377
|
+
return this.transformConstructSignature(narrowed);
|
|
378
|
+
}
|
|
379
|
+
break;
|
|
380
|
+
case 177 /* SyntaxKind.Constructor */:
|
|
381
|
+
if (input instanceof ConstructorDeclaration) {
|
|
382
|
+
const narrowed = input;
|
|
383
|
+
return this.transformConstructor(narrowed);
|
|
384
|
+
}
|
|
385
|
+
break;
|
|
386
|
+
case 178 /* SyntaxKind.GetAccessor */:
|
|
387
|
+
if (input instanceof GetAccessorDeclaration) {
|
|
388
|
+
const narrowed = input;
|
|
389
|
+
return this.transformGetAccessor(narrowed);
|
|
390
|
+
}
|
|
391
|
+
break;
|
|
392
|
+
case 179 /* SyntaxKind.SetAccessor */:
|
|
393
|
+
if (input instanceof SetAccessorDeclaration) {
|
|
394
|
+
const narrowed = input;
|
|
395
|
+
return this.transformSetAccessor(narrowed);
|
|
396
|
+
}
|
|
397
|
+
break;
|
|
398
|
+
case 173 /* SyntaxKind.PropertyDeclaration */:
|
|
399
|
+
if (input instanceof PropertyDeclaration) {
|
|
400
|
+
const narrowed = input;
|
|
401
|
+
return this.transformPropertyDeclaration(narrowed);
|
|
402
|
+
}
|
|
403
|
+
break;
|
|
404
|
+
case 172 /* SyntaxKind.PropertySignature */:
|
|
405
|
+
if (input instanceof PropertySignatureDeclaration) {
|
|
406
|
+
const narrowed = input;
|
|
407
|
+
return this.transformPropertySignature(narrowed);
|
|
408
|
+
}
|
|
409
|
+
break;
|
|
410
|
+
case 180 /* SyntaxKind.CallSignature */:
|
|
411
|
+
if (input instanceof CallSignatureDeclaration) {
|
|
412
|
+
const narrowed = input;
|
|
413
|
+
return this.transformCallSignature(narrowed);
|
|
414
|
+
}
|
|
415
|
+
break;
|
|
416
|
+
case 182 /* SyntaxKind.IndexSignature */:
|
|
417
|
+
if (input instanceof IndexSignatureDeclaration) {
|
|
418
|
+
const narrowed = input;
|
|
419
|
+
return this.transformIndexSignature(narrowed);
|
|
420
|
+
}
|
|
421
|
+
break;
|
|
422
|
+
case 261 /* SyntaxKind.VariableDeclaration */:
|
|
423
|
+
if (input instanceof VariableDeclaration) {
|
|
424
|
+
const narrowed = input;
|
|
425
|
+
return this.transformVariableDeclaration(narrowed);
|
|
426
|
+
}
|
|
427
|
+
break;
|
|
428
|
+
case 169 /* SyntaxKind.TypeParameter */:
|
|
429
|
+
if (input instanceof TypeParameterDeclaration) {
|
|
430
|
+
const narrowed = input;
|
|
431
|
+
return this.transformTypeParameter(narrowed);
|
|
432
|
+
}
|
|
433
|
+
break;
|
|
434
|
+
case 234 /* SyntaxKind.ExpressionWithTypeArguments */: {
|
|
435
|
+
const expression = input.expression;
|
|
436
|
+
if (isEntityName(expression) || isEntityNameExpression(expression, false))
|
|
437
|
+
this.checkEntityNameVisibility(expression, this.enclosingDeclaration);
|
|
438
|
+
return this.visitEachChild(input);
|
|
439
|
+
}
|
|
440
|
+
case 184 /* SyntaxKind.TypeReference */:
|
|
441
|
+
this.checkEntityNameVisibility(input.typeName, this.enclosingDeclaration);
|
|
442
|
+
return this.visitEachChild(input);
|
|
443
|
+
case 195 /* SyntaxKind.ConditionalType */:
|
|
444
|
+
if (input instanceof ConditionalTypeNode) {
|
|
445
|
+
const narrowed = input;
|
|
446
|
+
return this.transformConditionalType(narrowed);
|
|
447
|
+
}
|
|
448
|
+
break;
|
|
449
|
+
case 185 /* SyntaxKind.FunctionType */:
|
|
450
|
+
if (input instanceof FunctionTypeNode) {
|
|
451
|
+
const narrowed = input;
|
|
452
|
+
return this.transformFunctionType(narrowed);
|
|
453
|
+
}
|
|
454
|
+
break;
|
|
455
|
+
case 186 /* SyntaxKind.ConstructorType */:
|
|
456
|
+
if (input instanceof ConstructorTypeNode) {
|
|
457
|
+
const narrowed = input;
|
|
458
|
+
return this.transformConstructorType(narrowed);
|
|
459
|
+
}
|
|
460
|
+
break;
|
|
461
|
+
case 206 /* SyntaxKind.ImportType */:
|
|
462
|
+
if (input instanceof ImportTypeNode) {
|
|
463
|
+
const narrowed = input;
|
|
464
|
+
return this.transformImportType(narrowed);
|
|
465
|
+
}
|
|
466
|
+
break;
|
|
467
|
+
case 187 /* SyntaxKind.TypeQuery */:
|
|
468
|
+
this.checkEntityNameVisibility(input.exprName, this.enclosingDeclaration);
|
|
469
|
+
return this.visitEachChild(input);
|
|
470
|
+
case 190 /* SyntaxKind.TupleType */: {
|
|
471
|
+
const result = this.visitEachChild(input);
|
|
472
|
+
if (isOriginalNodeSingleLine(this.context, input))
|
|
473
|
+
this.context.addEmitFlags(result, 1 /* EmitFlags.SingleLine */);
|
|
474
|
+
return result;
|
|
475
|
+
}
|
|
476
|
+
case 308 /* SyntaxKind.JSDocTypeExpression */:
|
|
477
|
+
return this.visitNode(input.type);
|
|
478
|
+
case 310 /* SyntaxKind.JSDocAllType */:
|
|
479
|
+
return this.replaceWith(input, this.keyword(132 /* SyntaxKind.AnyKeyword */));
|
|
480
|
+
case 311 /* SyntaxKind.JSDocNullableType */:
|
|
481
|
+
// `?T` is `T | null`.
|
|
482
|
+
return this.replaceWith(input, this.createUnion(this.visitNode(input.type), this.factory.created(new LiteralTypeNode(this.factory.createKeywordExpression(105 /* SyntaxKind.NullKeyword */)))));
|
|
483
|
+
case 312 /* SyntaxKind.JSDocNonNullableType */:
|
|
484
|
+
return this.visitNode(input.type);
|
|
485
|
+
case 313 /* SyntaxKind.JSDocOptionalType */:
|
|
486
|
+
// `T=` is `T | undefined`.
|
|
487
|
+
return this.replaceWith(input, this.createUnion(this.visitNode(input.type), this.keyword(157 /* SyntaxKind.UndefinedKeyword */)));
|
|
488
|
+
case 314 /* SyntaxKind.JSDocVariadicType */:
|
|
489
|
+
// `...T` is `T[]`.
|
|
490
|
+
return this.replaceWith(input, this.factory.created(new ArrayTypeNode(this.visitNode(input.type))));
|
|
491
|
+
case 317 /* SyntaxKind.JSDocTypeLiteral */: {
|
|
492
|
+
const members = this.visitArray(input.jsdocPropertyTags ?? []);
|
|
493
|
+
return this.replaceWith(input, this.factory.created(new TypeLiteralNode(this.factory.createNodeList(members))));
|
|
494
|
+
}
|
|
495
|
+
case 340 /* SyntaxKind.JSDocPropertyTag */: {
|
|
496
|
+
const tag = input;
|
|
497
|
+
const type = tag.typeExpression === undefined ? undefined : this.visitNode(tag.typeExpression);
|
|
498
|
+
return this.replaceWith(input, this.factory.created(new PropertySignatureDeclaration(undefined, this.visitNode(tag.name), undefined, type, undefined)));
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
return this.visitEachChild(input);
|
|
502
|
+
}
|
|
503
|
+
createUnion(first, second) {
|
|
504
|
+
return this.factory.created(new UnionTypeNode(this.factory.createNodeList([first, second])));
|
|
505
|
+
}
|
|
506
|
+
replaceWith(original, replacement) {
|
|
507
|
+
this.context.setOriginal(replacement, original);
|
|
508
|
+
return replacement;
|
|
509
|
+
}
|
|
510
|
+
keyword(kind) {
|
|
511
|
+
return this.factory.created(new KeywordTypeNode(kind));
|
|
512
|
+
}
|
|
513
|
+
checkName(node) {
|
|
514
|
+
const tracker = this.tracker;
|
|
515
|
+
const oldContext = tracker.diagnosticContext;
|
|
516
|
+
if (!this.suppressNewDiagnosticContexts)
|
|
517
|
+
tracker.diagnosticContext = declarationNameContext(node);
|
|
518
|
+
const name = getName(node);
|
|
519
|
+
tracker.errorNameNode = name;
|
|
520
|
+
this.checkEntityNameVisibility(getExpression(name), this.enclosingDeclaration);
|
|
521
|
+
if (!this.suppressNewDiagnosticContexts)
|
|
522
|
+
tracker.diagnosticContext = oldContext;
|
|
523
|
+
tracker.errorNameNode = undefined;
|
|
524
|
+
return true;
|
|
525
|
+
}
|
|
526
|
+
checkEntityNameVisibility(entityName, enclosingDeclaration) {
|
|
527
|
+
return this.tracker.handleSymbolAccessibilityError(this.resolver.isEntityNameVisible(entityName, enclosingDeclaration));
|
|
528
|
+
}
|
|
529
|
+
transformMappedType(input) {
|
|
530
|
+
// The printer needs a template type.
|
|
531
|
+
const type = input.type === undefined ? this.keyword(132 /* SyntaxKind.AnyKeyword */) : this.visitNode(input.type);
|
|
532
|
+
return this.factory.updateMappedTypeNode(input, input.readonlyToken, this.visitNode(input.typeParameter), this.visitNode(input.nameType), input.questionToken, type, undefined);
|
|
533
|
+
}
|
|
534
|
+
transformHeritageClause(clause) {
|
|
535
|
+
const types = clause.types.nodes;
|
|
536
|
+
const retained = [];
|
|
537
|
+
for (const type of types) {
|
|
538
|
+
const expression = type.expression;
|
|
539
|
+
if (isEntityNameExpression(expression, false) ||
|
|
540
|
+
(clause.token === 95 /* SyntaxKind.ExtendsKeyword */ && expression.kind === 105 /* SyntaxKind.NullKeyword */))
|
|
541
|
+
retained.push(type);
|
|
542
|
+
}
|
|
543
|
+
if (retained.length === 0)
|
|
544
|
+
return undefined;
|
|
545
|
+
if (retained.length === types.length)
|
|
546
|
+
return this.visitEachChild(clause);
|
|
547
|
+
return this.factory.updateHeritageClause(clause, clause.token, this.visitNodes(this.factory.createNodeList(retained)));
|
|
548
|
+
}
|
|
549
|
+
transformImportType(input) {
|
|
550
|
+
if (!isLiteralImportTypeNode(input))
|
|
551
|
+
return input;
|
|
552
|
+
const argument = input.argument;
|
|
553
|
+
return this.factory.updateImportTypeNode(input, input.isTypeOf, this.factory.updateLiteralTypeNode(argument, this.rewriteModuleSpecifier(input, argument.literal)), input.attributes, input.qualifier, this.visitNodes(input.typeArguments));
|
|
554
|
+
}
|
|
555
|
+
transformConstructorType(input) {
|
|
556
|
+
return this.factory.updateConstructorTypeNode(input, this.ensureModifiers(input), this.visitNodes(input.typeParameters), this.updateParamList(input, input.parameters), this.visitNode(input.type));
|
|
557
|
+
}
|
|
558
|
+
transformFunctionType(input) {
|
|
559
|
+
return this.factory.updateFunctionTypeNode(input, this.visitNodes(input.typeParameters), this.updateParamList(input, input.parameters), this.visitNode(input.type));
|
|
560
|
+
}
|
|
561
|
+
transformConditionalType(input) {
|
|
562
|
+
const checkType = this.visitNode(input.checkType);
|
|
563
|
+
const extendsType = this.visitNode(input.extendsType);
|
|
564
|
+
const previousEnclosingDeclaration = this.enclosingDeclaration;
|
|
565
|
+
this.enclosingDeclaration = input.trueType;
|
|
566
|
+
const trueType = this.visitNode(input.trueType);
|
|
567
|
+
this.enclosingDeclaration = previousEnclosingDeclaration;
|
|
568
|
+
const falseType = this.visitNode(input.falseType);
|
|
569
|
+
return this.factory.updateConditionalTypeNode(input, checkType, extendsType, trueType, falseType);
|
|
570
|
+
}
|
|
571
|
+
transformTypeParameter(input) {
|
|
572
|
+
// Type parameters of private methods lose their constraints and defaults.
|
|
573
|
+
const parent = input.parent;
|
|
574
|
+
if (parent !== undefined &&
|
|
575
|
+
parent.kind === 175 /* SyntaxKind.MethodDeclaration */ &&
|
|
576
|
+
this.isPrivate(parent) &&
|
|
577
|
+
(input.defaultType !== undefined || input.constraint !== undefined))
|
|
578
|
+
return this.factory.updateTypeParameterDeclaration(input, input.modifiers, input.name, undefined, input.expression, undefined);
|
|
579
|
+
return this.visitEachChild(input);
|
|
580
|
+
}
|
|
581
|
+
transformVariableDeclaration(input) {
|
|
582
|
+
const file = this.tracker.currentSourceFile;
|
|
583
|
+
if (file !== undefined &&
|
|
584
|
+
file.commonJSModuleIndicator !== undefined &&
|
|
585
|
+
isVariableDeclarationInitializedToRequire(input))
|
|
586
|
+
return this.transformCjsRequireVariableDeclaration(input);
|
|
587
|
+
if (input.name instanceof BindingPattern)
|
|
588
|
+
return this.recreateBindingPattern(input.name);
|
|
589
|
+
// Variable types don't enter new diagnostic contexts.
|
|
590
|
+
this.suppressNewDiagnosticContexts = true;
|
|
591
|
+
return this.factory.updateVariableDeclaration(input, input.name, undefined, this.ensureType(input, false), this.ensureNoInitializer(input));
|
|
592
|
+
}
|
|
593
|
+
recreateBindingPattern(pattern) {
|
|
594
|
+
const results = [];
|
|
595
|
+
for (const element of pattern.elements.nodes) {
|
|
596
|
+
if (element === undefined || !(element instanceof BindingElement))
|
|
597
|
+
continue;
|
|
598
|
+
const result = this.recreateBindingElement(element);
|
|
599
|
+
if (result instanceof SyntaxList)
|
|
600
|
+
for (const child of result.children)
|
|
601
|
+
results.push(child);
|
|
602
|
+
else if (result !== undefined)
|
|
603
|
+
results.push(result);
|
|
604
|
+
}
|
|
605
|
+
if (results.length === 0)
|
|
606
|
+
return undefined;
|
|
607
|
+
if (results.length === 1)
|
|
608
|
+
return results[0];
|
|
609
|
+
return this.factory.createSyntaxList(results);
|
|
610
|
+
}
|
|
611
|
+
recreateBindingElement(element) {
|
|
612
|
+
const name = element.name;
|
|
613
|
+
if (name === undefined || !this.getBindingNameVisible(element))
|
|
614
|
+
return undefined;
|
|
615
|
+
if (name instanceof BindingPattern) {
|
|
616
|
+
const narrowed = name;
|
|
617
|
+
return this.recreateBindingPattern(narrowed);
|
|
618
|
+
}
|
|
619
|
+
return this.factory.createVariableDeclaration(name, undefined, this.ensureType(element, false), undefined);
|
|
620
|
+
}
|
|
621
|
+
transformIndexSignature(input) {
|
|
622
|
+
const type = this.visitNode(input.type) ?? this.keyword(132 /* SyntaxKind.AnyKeyword */);
|
|
623
|
+
return this.factory.updateIndexSignatureDeclaration(input, this.ensureModifiers(input), this.updateParamList(input, input.parameters), type);
|
|
624
|
+
}
|
|
625
|
+
transformCallSignature(input) {
|
|
626
|
+
return this.factory.updateCallSignatureDeclaration(input, this.ensureTypeParams(input, input.typeParameters), this.updateParamList(input, input.parameters), this.ensureType(input, false));
|
|
627
|
+
}
|
|
628
|
+
transformPropertySignature(input) {
|
|
629
|
+
if (input.name instanceof PrivateIdentifier)
|
|
630
|
+
return undefined;
|
|
631
|
+
const result = this.factory.updatePropertySignatureDeclaration(input, this.ensureModifiers(input), input.name, input.postfixToken, this.ensureType(input, false), this.ensureNoInitializer(input));
|
|
632
|
+
this.preservePartialJsDoc(result, input);
|
|
633
|
+
return result;
|
|
634
|
+
}
|
|
635
|
+
/** Writes the description of a property from a JSDoc `@property` tag as its comment. */
|
|
636
|
+
preservePartialJsDoc(updated, original) {
|
|
637
|
+
if ((original.flags & 8 /* NodeFlags.Reparsed */) === 0)
|
|
638
|
+
return false;
|
|
639
|
+
const docs = getJSDoc(getSourceFileOfNode(original), original);
|
|
640
|
+
if (docs.length === 0 || !(docs[0] instanceof JSDoc))
|
|
641
|
+
return false;
|
|
642
|
+
const description = getTextOfJSDocComment(docs[0].comment);
|
|
643
|
+
if (description === '')
|
|
644
|
+
return false;
|
|
645
|
+
const comment = '*\n * ' + description.split('\n').join('\n * ') + '\n ';
|
|
646
|
+
this.context.addSyntheticLeadingComment(updated, 3 /* SyntaxKind.MultiLineCommentTrivia */, comment, true);
|
|
647
|
+
return true;
|
|
648
|
+
}
|
|
649
|
+
transformPropertyDeclaration(input) {
|
|
650
|
+
if (input.name instanceof PrivateIdentifier)
|
|
651
|
+
return undefined;
|
|
652
|
+
// Definite assignment assertions (`!`) are dropped.
|
|
653
|
+
const postfix = input.postfixToken;
|
|
654
|
+
return this.factory.updatePropertyDeclaration(input, this.ensureModifiers(input), input.name, postfix !== undefined && postfix.kind === 53 /* SyntaxKind.ExclamationToken */ ? undefined : postfix, this.ensureType(input, false), this.ensureNoInitializer(input));
|
|
655
|
+
}
|
|
656
|
+
transformSetAccessor(input) {
|
|
657
|
+
if (input.name instanceof PrivateIdentifier)
|
|
658
|
+
return undefined;
|
|
659
|
+
return this.factory.updateSetAccessorDeclaration(input, this.ensureModifiers(input), input.name, undefined, this.updateAccessorParamList(input, this.isPrivate(this.parseNode(input))), undefined, undefined, undefined);
|
|
660
|
+
}
|
|
661
|
+
transformGetAccessor(input) {
|
|
662
|
+
if (input.name instanceof PrivateIdentifier)
|
|
663
|
+
return undefined;
|
|
664
|
+
return this.factory.updateGetAccessorDeclaration(input, this.ensureModifiers(input), input.name, undefined, this.updateAccessorParamList(input, this.isPrivate(this.parseNode(input))), this.ensureType(input, false), undefined, undefined);
|
|
665
|
+
}
|
|
666
|
+
updateAccessorParamList(input, isPrivate) {
|
|
667
|
+
const params = [];
|
|
668
|
+
const parameters = getParameters(input).nodes;
|
|
669
|
+
if (!isPrivate) {
|
|
670
|
+
const thisParameter = parameters.length > 0 && isThisParameterNode(parameters[0]) ? parameters[0] : undefined;
|
|
671
|
+
if (thisParameter !== undefined)
|
|
672
|
+
params.push(this.ensureParameterNode(thisParameter));
|
|
673
|
+
}
|
|
674
|
+
if (input.kind === 179 /* SyntaxKind.SetAccessor */) {
|
|
675
|
+
let valueParameter = undefined;
|
|
676
|
+
if (!isPrivate) {
|
|
677
|
+
if (params.length === 1 && parameters.length >= 2)
|
|
678
|
+
valueParameter = this.ensureParameterNode(parameters[1]);
|
|
679
|
+
else if (params.length === 0 && parameters.length >= 1)
|
|
680
|
+
valueParameter = this.ensureParameterNode(parameters[0]);
|
|
681
|
+
}
|
|
682
|
+
// A missing value parameter is `value: any`, untyped when private.
|
|
683
|
+
valueParameter ??= this.factory.created(new ParameterDeclaration(undefined, undefined, this.factory.createIdentifier('value'), undefined, isPrivate ? undefined : this.keyword(132 /* SyntaxKind.AnyKeyword */), undefined));
|
|
684
|
+
params.push(valueParameter);
|
|
685
|
+
}
|
|
686
|
+
return this.factory.createNodeList(params);
|
|
687
|
+
}
|
|
688
|
+
transformConstructor(input) {
|
|
689
|
+
return this.factory.updateConstructorDeclaration(input, this.ensureModifiers(input), undefined, this.updateParamList(input, input.parameters), undefined, undefined, undefined);
|
|
690
|
+
}
|
|
691
|
+
transformConstructSignature(input) {
|
|
692
|
+
return this.factory.updateConstructSignatureDeclaration(input, this.ensureTypeParams(input, input.typeParameters), this.updateParamList(input, input.parameters), this.ensureType(input, false));
|
|
693
|
+
}
|
|
694
|
+
/** A private method is written as a property without a type. */
|
|
695
|
+
omitPrivateMethodType(input) {
|
|
696
|
+
const symbol = getSymbol(input);
|
|
697
|
+
if (symbol !== undefined && symbol.declarations.length > 0 && symbol.declarations[0] !== input)
|
|
698
|
+
return undefined;
|
|
699
|
+
const result = this.factory.created(new PropertyDeclaration(this.ensureModifiers(input), getName(input), undefined, undefined, undefined));
|
|
700
|
+
this.context.assignCommentRange(result, input);
|
|
701
|
+
return result;
|
|
702
|
+
}
|
|
703
|
+
transformMethodSignature(input) {
|
|
704
|
+
if (this.isPrivate(this.parseNode(input)))
|
|
705
|
+
return this.omitPrivateMethodType(input);
|
|
706
|
+
if (input.name instanceof PrivateIdentifier)
|
|
707
|
+
return undefined;
|
|
708
|
+
return this.factory.updateMethodSignatureDeclaration(input, this.ensureModifiers(input), input.name, input.postfixToken, this.ensureTypeParams(input, input.typeParameters), this.updateParamList(input, input.parameters), this.ensureType(input, false));
|
|
709
|
+
}
|
|
710
|
+
transformMethodDeclaration(input) {
|
|
711
|
+
if (this.isPrivate(this.parseNode(input)))
|
|
712
|
+
return this.omitPrivateMethodType(input);
|
|
713
|
+
if (input.name instanceof PrivateIdentifier)
|
|
714
|
+
return undefined;
|
|
715
|
+
return this.factory.updateMethodDeclaration(input, this.ensureModifiers(input), undefined, input.name, input.postfixToken, this.ensureTypeParams(input, input.typeParameters), this.updateParamList(input, input.parameters), this.ensureType(input, false), undefined, undefined);
|
|
716
|
+
}
|
|
717
|
+
// Top-level declarations
|
|
718
|
+
visitDeclarationStatement(input) {
|
|
719
|
+
if (input === undefined)
|
|
720
|
+
return undefined;
|
|
721
|
+
if (this.shouldStripInternal(input))
|
|
722
|
+
return undefined;
|
|
723
|
+
if (input instanceof ExportDeclaration) {
|
|
724
|
+
if (input.parent !== undefined && input.parent.kind === 307 /* SyntaxKind.SourceFile */)
|
|
725
|
+
this.resultHasExternalModuleIndicator = true;
|
|
726
|
+
this.resultHasScopeMarker = true;
|
|
727
|
+
return this.factory.updateExportDeclaration(input, input.modifiers, input.isTypeOnly, input.exportClause, this.rewriteModuleSpecifier(input, input.moduleSpecifier), this.tryGetResolutionModeOverride(input.attributes));
|
|
728
|
+
}
|
|
729
|
+
if (input instanceof ExportAssignment)
|
|
730
|
+
return this.transformExportAssignment(input, input, input.expression, input.isExportEquals);
|
|
731
|
+
// Transformed later, when it's known which unexported declarations are referenced.
|
|
732
|
+
const original = this.context.mostOriginal(input);
|
|
733
|
+
if (this.lateStatementReplacements.get(original) === undefined)
|
|
734
|
+
this.lateStatementReplacements.set(original, this.transformTopLevelDeclaration(input));
|
|
735
|
+
return input;
|
|
736
|
+
}
|
|
737
|
+
/** `export =`, `export default` or `module.exports =` of `expression`. */
|
|
738
|
+
transformExportAssignment(input, assignment, expression, isExportEquals) {
|
|
739
|
+
if (input.parent !== undefined && input.parent.kind === 307 /* SyntaxKind.SourceFile */)
|
|
740
|
+
this.resultHasExternalModuleIndicator = true;
|
|
741
|
+
this.resultHasScopeMarker = true;
|
|
742
|
+
if (expression instanceof Identifier &&
|
|
743
|
+
input.parent !== undefined &&
|
|
744
|
+
(input.parent.kind === 307 /* SyntaxKind.SourceFile */ || input.parent.kind === 269 /* SyntaxKind.ModuleBlock */)) {
|
|
745
|
+
const result = this.factory.created(new ExportAssignment(undefined, isExportEquals, undefined, expression));
|
|
746
|
+
this.context.assignCommentRange(result, input);
|
|
747
|
+
return result;
|
|
748
|
+
}
|
|
749
|
+
const unwrapped = skipOuterExpressions(expression);
|
|
750
|
+
const name = this.getNameOfExportedExpression(unwrapped, isExportEquals);
|
|
751
|
+
// Class and function expressions become declarations.
|
|
752
|
+
if (unwrapped instanceof ClassExpression || isFunctionLike(unwrapped)) {
|
|
753
|
+
const declaration = unwrapped instanceof ClassExpression
|
|
754
|
+
? this.transformClassExpressionToDeclaration(unwrapped, name, this.declareModifiers())
|
|
755
|
+
: this.transformFunctionLikeToDeclaration(unwrapped, name, this.declareModifiers());
|
|
756
|
+
this.context.assignCommentRange(declaration, input);
|
|
757
|
+
const exportAssignment = this.factory.created(new ExportAssignment(undefined, isExportEquals, undefined, name));
|
|
758
|
+
this.context.addEmitFlags(exportAssignment, 384 /* EmitFlags.NoComments */);
|
|
759
|
+
return this.factory.createSyntaxList([exportAssignment, declaration]);
|
|
760
|
+
}
|
|
761
|
+
// Other expressions are written as a `_default` variable with the expression's type.
|
|
762
|
+
const tracker = this.tracker;
|
|
763
|
+
tracker.diagnosticContext = new DiagnosticContext(3 /* DiagnosticContextKind.DefaultExport */, input, undefined);
|
|
764
|
+
this.cjsExportAssignmentName = name;
|
|
765
|
+
tracker.pushErrorFallbackNode(assignment);
|
|
766
|
+
let initializer = undefined;
|
|
767
|
+
if (isPrimitiveLiteralValue(skipParenthesesOnly(expression), true))
|
|
768
|
+
initializer = this.resolver.createLiteralConstValue(this.context, this.parseNode(assignment), tracker);
|
|
769
|
+
const type = initializer === undefined ? this.ensureType(assignment, false) : undefined;
|
|
770
|
+
const declaration = this.factory.createVariableDeclaration(name, undefined, type, initializer);
|
|
771
|
+
tracker.popErrorFallbackNode();
|
|
772
|
+
const statement = this.createConstStatement(declaration);
|
|
773
|
+
const exportAssignment = this.factory.created(new ExportAssignment(undefined, isExportEquals, undefined, name));
|
|
774
|
+
this.context.assignCommentRange(statement, input);
|
|
775
|
+
return this.factory.createSyntaxList([statement, exportAssignment]);
|
|
776
|
+
}
|
|
777
|
+
createConstStatement(declaration) {
|
|
778
|
+
return this.factory.createVariableStatement(this.declareModifiers(), this.factory.createVariableDeclarationList(this.factory.createNodeList([declaration]), 2 /* NodeFlags.Const */));
|
|
779
|
+
}
|
|
780
|
+
transformFunctionLikeToDeclaration(fn, name, modifiers) {
|
|
781
|
+
const fullSignature = getFullSignature(fn);
|
|
782
|
+
return this.factory.created(new FunctionDeclaration(modifiers, undefined, name, this.ensureTypeParams(fn, getTypeParameters(fn)), this.updateParamList(fn, getParameters(fn)), this.ensureType(fn, false), fullSignature === undefined ? undefined : this.visitNode(fullSignature), undefined));
|
|
783
|
+
}
|
|
784
|
+
/** A class expression as a declaration, for `export default (class {})` and CommonJS exports. */
|
|
785
|
+
transformClassExpressionToDeclaration(classExpression, name, modifiers) {
|
|
786
|
+
const previousEnclosingDeclaration = this.enclosingDeclaration;
|
|
787
|
+
this.enclosingDeclaration = classExpression;
|
|
788
|
+
const previousInClassExpression = this.inClassExpressionDeclaration;
|
|
789
|
+
this.inClassExpressionDeclaration = true;
|
|
790
|
+
const extraMembers = isInJSFile(classExpression) ? this.collectThisPropertyAssignments(classExpression) : [];
|
|
791
|
+
const members = this.buildClassMembers(classExpression, extraMembers);
|
|
792
|
+
const typeParameters = this.ensureTypeParams(classExpression, classExpression.typeParameters);
|
|
793
|
+
const heritageClauses = this.visitNodes(classExpression.heritageClauses);
|
|
794
|
+
this.enclosingDeclaration = previousEnclosingDeclaration;
|
|
795
|
+
this.inClassExpressionDeclaration = previousInClassExpression;
|
|
796
|
+
return this.factory.created(new ClassDeclaration(modifiers, name, typeParameters, heritageClauses, members));
|
|
797
|
+
}
|
|
798
|
+
declareModifiers() {
|
|
799
|
+
return this.factory.createModifierList(this.needsDeclare ? [this.factory.createToken(137 /* SyntaxKind.DeclareKeyword */)] : []);
|
|
800
|
+
}
|
|
801
|
+
/** The name of the variable for an exported expression: its own name if it has one, else `_default`. */
|
|
802
|
+
getNameOfExportedExpression(expression, isExportEquals) {
|
|
803
|
+
const name = this.tryGetNameOfAssignedExpression(expression);
|
|
804
|
+
if (name !== undefined) {
|
|
805
|
+
this.cjsExportAssignmentName = name;
|
|
806
|
+
return name;
|
|
807
|
+
}
|
|
808
|
+
const file = this.tracker.currentSourceFile;
|
|
809
|
+
// Only JavaScript names `export =` expressions `_exports`.
|
|
810
|
+
const base = isExportEquals && file !== undefined && file.isJS() ? '_exports' : '_default';
|
|
811
|
+
const result = this.factory.createUniqueName(base, 16 /* GeneratedIdentifierFlags.Optimistic */);
|
|
812
|
+
this.cjsExportAssignmentName = result;
|
|
813
|
+
return result;
|
|
814
|
+
}
|
|
815
|
+
/** The name of a named function or class expression, unique if it would shadow something. */
|
|
816
|
+
tryGetNameOfAssignedExpression(expression) {
|
|
817
|
+
let text = '';
|
|
818
|
+
if (expression.kind !== 212 /* SyntaxKind.PropertyAccessExpression */) {
|
|
819
|
+
const name = getName(expression);
|
|
820
|
+
if (name instanceof Identifier)
|
|
821
|
+
text = name.text;
|
|
822
|
+
}
|
|
823
|
+
if (expression instanceof Identifier)
|
|
824
|
+
text = expression.text;
|
|
825
|
+
if (text === '' || text === 'default')
|
|
826
|
+
return undefined;
|
|
827
|
+
if (this.resolver.isNameResolvable(this.enclosingDeclaration, text))
|
|
828
|
+
return this.factory.createUniqueName(text, 16 /* GeneratedIdentifierFlags.Optimistic */);
|
|
829
|
+
return this.factory.createIdentifier(text);
|
|
830
|
+
}
|
|
831
|
+
rewriteModuleSpecifier(parent, input) {
|
|
832
|
+
if (input === undefined)
|
|
833
|
+
return undefined;
|
|
834
|
+
if (parent.kind !== 268 /* SyntaxKind.ModuleDeclaration */ && parent.kind !== 206 /* SyntaxKind.ImportType */)
|
|
835
|
+
this.resultHasExternalModuleIndicator = true;
|
|
836
|
+
return input;
|
|
837
|
+
}
|
|
838
|
+
tryGetResolutionModeOverride(node) {
|
|
839
|
+
if (node === undefined)
|
|
840
|
+
return undefined;
|
|
841
|
+
return this.resolver.getResolutionModeOverride(node) !== undefined ? node : undefined;
|
|
842
|
+
}
|
|
843
|
+
/** The replacement statements for a direct child of a source file or module block. */
|
|
844
|
+
transformTopLevelDeclaration(input) {
|
|
845
|
+
if (input === undefined)
|
|
846
|
+
return undefined;
|
|
847
|
+
const tracker = this.tracker;
|
|
848
|
+
const marked = tracker.lateMarkedStatements;
|
|
849
|
+
if (marked.length > 0) {
|
|
850
|
+
const remaining = [];
|
|
851
|
+
for (const node of marked)
|
|
852
|
+
if (node !== input)
|
|
853
|
+
remaining.push(node);
|
|
854
|
+
tracker.lateMarkedStatements = remaining;
|
|
855
|
+
}
|
|
856
|
+
if (this.shouldStripInternal(input))
|
|
857
|
+
return undefined;
|
|
858
|
+
if (input instanceof ImportEqualsDeclaration) {
|
|
859
|
+
const narrowed = input;
|
|
860
|
+
return this.transformImportEquals(narrowed);
|
|
861
|
+
}
|
|
862
|
+
if (input instanceof ImportDeclaration) {
|
|
863
|
+
const result = this.transformImportDeclaration(input);
|
|
864
|
+
// `import x = require()` in JavaScript is written as an import declaration.
|
|
865
|
+
if (result !== undefined && result.kind !== 273 /* SyntaxKind.ImportDeclaration */) {
|
|
866
|
+
// A copy of the declaration, as tsgo's clone: with its position and emit node.
|
|
867
|
+
const clone = this.factory.created(new ImportDeclaration(273 /* SyntaxKind.ImportDeclaration */, result.modifiers, result.importClause, result.moduleSpecifier, result.attributes));
|
|
868
|
+
clone.pos = result.pos;
|
|
869
|
+
clone.end = result.end;
|
|
870
|
+
this.context.setOriginal(clone, result);
|
|
871
|
+
return clone;
|
|
872
|
+
}
|
|
873
|
+
return result;
|
|
874
|
+
}
|
|
875
|
+
if (isDeclaration(input) && this.isDeclarationAndNotVisible(input))
|
|
876
|
+
return undefined;
|
|
877
|
+
if (isFunctionLike(input) && this.resolver.isImplementationOfOverload(input))
|
|
878
|
+
return undefined;
|
|
879
|
+
const original = this.context.mostOriginal(input);
|
|
880
|
+
if (this.expandoHosts.has(original))
|
|
881
|
+
return this.createFullExpandoBlock(original);
|
|
882
|
+
const previousEnclosingDeclaration = this.enclosingDeclaration;
|
|
883
|
+
if (isEnclosingDeclaration(input))
|
|
884
|
+
this.enclosingDeclaration = input;
|
|
885
|
+
const oldContext = tracker.diagnosticContext;
|
|
886
|
+
const oldName = tracker.errorNameNode;
|
|
887
|
+
if (canProduceDiagnostics(input))
|
|
888
|
+
tracker.diagnosticContext = declarationContext(input);
|
|
889
|
+
const saveNeedsDeclare = this.needsDeclare;
|
|
890
|
+
let result = undefined;
|
|
891
|
+
switch (input.kind) {
|
|
892
|
+
case 266 /* SyntaxKind.TypeAliasDeclaration */:
|
|
893
|
+
case 345 /* SyntaxKind.JSTypeAliasDeclaration */:
|
|
894
|
+
if (input instanceof TypeAliasDeclaration) {
|
|
895
|
+
const narrowed = input;
|
|
896
|
+
result = this.transformTypeAlias(narrowed);
|
|
897
|
+
}
|
|
898
|
+
break;
|
|
899
|
+
case 265 /* SyntaxKind.InterfaceDeclaration */:
|
|
900
|
+
if (input instanceof InterfaceDeclaration) {
|
|
901
|
+
const narrowed = input;
|
|
902
|
+
result = this.transformInterface(narrowed);
|
|
903
|
+
}
|
|
904
|
+
break;
|
|
905
|
+
case 263 /* SyntaxKind.FunctionDeclaration */:
|
|
906
|
+
if (input instanceof FunctionDeclaration) {
|
|
907
|
+
const narrowed = input;
|
|
908
|
+
result = this.transformFunctionDeclaration(narrowed);
|
|
909
|
+
}
|
|
910
|
+
break;
|
|
911
|
+
case 268 /* SyntaxKind.ModuleDeclaration */:
|
|
912
|
+
if (input instanceof ModuleDeclaration) {
|
|
913
|
+
const narrowed = input;
|
|
914
|
+
result = this.transformModuleDeclaration(narrowed);
|
|
915
|
+
}
|
|
916
|
+
break;
|
|
917
|
+
case 264 /* SyntaxKind.ClassDeclaration */:
|
|
918
|
+
if (input instanceof ClassDeclaration) {
|
|
919
|
+
const narrowed = input;
|
|
920
|
+
result = this.transformClassDeclaration(narrowed);
|
|
921
|
+
}
|
|
922
|
+
break;
|
|
923
|
+
case 244 /* SyntaxKind.VariableStatement */:
|
|
924
|
+
if (input instanceof VariableStatement) {
|
|
925
|
+
const narrowed = input;
|
|
926
|
+
result = this.transformVariableStatement(narrowed);
|
|
927
|
+
}
|
|
928
|
+
break;
|
|
929
|
+
case 267 /* SyntaxKind.EnumDeclaration */:
|
|
930
|
+
if (input instanceof EnumDeclaration) {
|
|
931
|
+
const narrowed = input;
|
|
932
|
+
result = this.transformEnumDeclaration(narrowed);
|
|
933
|
+
}
|
|
934
|
+
break;
|
|
935
|
+
default:
|
|
936
|
+
throw new Error('Unhandled top-level node in declaration emit');
|
|
937
|
+
}
|
|
938
|
+
this.enclosingDeclaration = previousEnclosingDeclaration;
|
|
939
|
+
tracker.diagnosticContext = oldContext;
|
|
940
|
+
this.needsDeclare = saveNeedsDeclare;
|
|
941
|
+
tracker.errorNameNode = oldName;
|
|
942
|
+
return result;
|
|
943
|
+
}
|
|
944
|
+
transformTypeAlias(input) {
|
|
945
|
+
this.needsDeclare = false;
|
|
946
|
+
return this.factory.updateTypeAliasDeclaration(input, this.ensureModifiers(input), input.name, this.visitNodes(input.typeParameters), this.visitNode(input.type));
|
|
947
|
+
}
|
|
948
|
+
transformInterface(input) {
|
|
949
|
+
return this.factory.updateInterfaceDeclaration(input, this.ensureModifiers(input), input.name, this.visitNodes(input.typeParameters), this.visitNodes(input.heritageClauses), this.visitNodes(input.members));
|
|
950
|
+
}
|
|
951
|
+
transformFunctionDeclaration(input) {
|
|
952
|
+
if (this.resolver.isExpandoFunctionDeclaration(input))
|
|
953
|
+
this.tracker.reportExpandoFunctionErrors(input);
|
|
954
|
+
return this.factory.updateFunctionDeclaration(input, this.ensureModifiers(input), undefined, input.name, this.ensureTypeParams(input, input.typeParameters), this.updateParamList(input, input.parameters), this.ensureType(input, false), undefined, undefined);
|
|
955
|
+
}
|
|
956
|
+
transformModuleDeclaration(input) {
|
|
957
|
+
const modifiers = this.ensureModifiers(input);
|
|
958
|
+
const saveNeedsDeclare = this.needsDeclare;
|
|
959
|
+
this.needsDeclare = false;
|
|
960
|
+
const body = input.body;
|
|
961
|
+
let keyword = input.keyword;
|
|
962
|
+
if (keyword !== 162 /* SyntaxKind.GlobalKeyword */ && !(input.name instanceof StringLiteral))
|
|
963
|
+
keyword = 145 /* SyntaxKind.NamespaceKeyword */;
|
|
964
|
+
if (body !== undefined && body instanceof ModuleBlock) {
|
|
965
|
+
const oldNeedsScopeFix = this.needsScopeFixMarker;
|
|
966
|
+
const oldHasScopeFix = this.resultHasScopeMarker;
|
|
967
|
+
this.resultHasScopeMarker = false;
|
|
968
|
+
this.needsScopeFixMarker = false;
|
|
969
|
+
const statements = this.visitNodes(body.statements);
|
|
970
|
+
let lateStatements = this.transformAndReplaceLatePaintedStatements(statements);
|
|
971
|
+
// In a `declare`d namespace everything is exported already; unexported "privates" don't matter.
|
|
972
|
+
if ((input.flags & 8388608 /* NodeFlags.Ambient */) !== 0)
|
|
973
|
+
this.needsScopeFixMarker = false;
|
|
974
|
+
// With an `export {}` or `export =` there's nothing to do. Otherwise, if some statements
|
|
975
|
+
// are unexported, add `export {}`; if all are exported, drop the redundant `export`s.
|
|
976
|
+
if (!isGlobalScopeAugmentation(input) && !this.resultHasScopeMarker && !hasScopeMarker(lateStatements)) {
|
|
977
|
+
if (this.needsScopeFixMarker) {
|
|
978
|
+
const nodes = lateStatements.nodes.slice();
|
|
979
|
+
nodes.push(this.createEmptyExports());
|
|
980
|
+
lateStatements = this.factory.createNodeList(nodes);
|
|
981
|
+
}
|
|
982
|
+
else
|
|
983
|
+
lateStatements = this.stripExportModifiersOfList(lateStatements);
|
|
984
|
+
}
|
|
985
|
+
const updatedBody = this.factory.updateModuleBlock(body, lateStatements);
|
|
986
|
+
this.needsDeclare = saveNeedsDeclare;
|
|
987
|
+
this.needsScopeFixMarker = oldNeedsScopeFix;
|
|
988
|
+
this.resultHasScopeMarker = oldHasScopeFix;
|
|
989
|
+
return this.factory.updateModuleDeclaration(input, modifiers, keyword, input.name, updatedBody);
|
|
990
|
+
}
|
|
991
|
+
if (body !== undefined) {
|
|
992
|
+
// A nested `namespace a.b` is transformed now: it needs no visibility painting.
|
|
993
|
+
this.visitNode(body);
|
|
994
|
+
const original = this.context.mostOriginal(body);
|
|
995
|
+
const inner = this.lateStatementReplacements.get(original);
|
|
996
|
+
this.lateStatementReplacements.delete(original);
|
|
997
|
+
this.needsDeclare = saveNeedsDeclare;
|
|
998
|
+
return this.factory.updateModuleDeclaration(input, modifiers, keyword, input.name, inner);
|
|
999
|
+
}
|
|
1000
|
+
this.needsDeclare = saveNeedsDeclare;
|
|
1001
|
+
return this.factory.updateModuleDeclaration(input, modifiers, keyword, input.name, undefined);
|
|
1002
|
+
}
|
|
1003
|
+
stripExportModifiersOfList(list) {
|
|
1004
|
+
const nodes = [];
|
|
1005
|
+
let changed = false;
|
|
1006
|
+
for (const statement of list.nodes) {
|
|
1007
|
+
const stripped = this.stripExportModifiers(statement);
|
|
1008
|
+
if (stripped !== statement)
|
|
1009
|
+
changed = true;
|
|
1010
|
+
nodes.push(stripped);
|
|
1011
|
+
}
|
|
1012
|
+
return changed ? new NodeList(list.pos, list.end, nodes) : list;
|
|
1013
|
+
}
|
|
1014
|
+
stripExportModifiers(statement) {
|
|
1015
|
+
const parseNode = this.context.parseNode(statement);
|
|
1016
|
+
// `export import` stays: imports aren't implicitly exported. So does `export default`.
|
|
1017
|
+
if (statement.kind === 272 /* SyntaxKind.ImportEqualsDeclaration */ ||
|
|
1018
|
+
(parseNode !== undefined && this.resolver.getEffectiveDeclarationFlags(parseNode, 2048 /* ModifierFlags.Default */) !== 0) ||
|
|
1019
|
+
!canHaveModifiers(statement))
|
|
1020
|
+
return statement;
|
|
1021
|
+
const flags = getCombinedModifierFlags(statement);
|
|
1022
|
+
if ((flags & 32 /* ModifierFlags.Export */) === 0)
|
|
1023
|
+
return statement;
|
|
1024
|
+
return this.replaceModifiers(statement, this.createModifiers(flags & (131071 /* ModifierFlags.All */ ^ 32 /* ModifierFlags.Export */)));
|
|
1025
|
+
}
|
|
1026
|
+
/** `node` with other modifiers; for statements that can be exported. */
|
|
1027
|
+
replaceModifiers(node, modifiers) {
|
|
1028
|
+
if (node === undefined)
|
|
1029
|
+
return node;
|
|
1030
|
+
const factory = this.factory;
|
|
1031
|
+
if (node instanceof VariableStatement)
|
|
1032
|
+
return factory.updateVariableStatement(node, modifiers, node.declarationList);
|
|
1033
|
+
if (node instanceof FunctionDeclaration)
|
|
1034
|
+
return factory.updateFunctionDeclaration(node, modifiers, node.asteriskToken, node.name, node.typeParameters, node.parameters, node.type, node.fullSignature, node.body);
|
|
1035
|
+
if (node instanceof ClassDeclaration)
|
|
1036
|
+
return factory.updateClassDeclaration(node, modifiers, node.name, node.typeParameters, node.heritageClauses, node.members);
|
|
1037
|
+
if (node instanceof InterfaceDeclaration)
|
|
1038
|
+
return factory.updateInterfaceDeclaration(node, modifiers, node.name, node.typeParameters, node.heritageClauses, node.members);
|
|
1039
|
+
if (node instanceof TypeAliasDeclaration)
|
|
1040
|
+
return factory.updateTypeAliasDeclaration(node, modifiers, node.name, node.typeParameters, node.type);
|
|
1041
|
+
if (node instanceof EnumDeclaration)
|
|
1042
|
+
return factory.updateEnumDeclaration(node, modifiers, node.name, node.members);
|
|
1043
|
+
if (node instanceof ModuleDeclaration)
|
|
1044
|
+
return factory.updateModuleDeclaration(node, modifiers, node.keyword, node.name, node.body);
|
|
1045
|
+
if (node instanceof ImportDeclaration)
|
|
1046
|
+
return factory.updateImportDeclaration(node, modifiers, node.importClause, node.moduleSpecifier, node.attributes);
|
|
1047
|
+
if (node instanceof ExportDeclaration)
|
|
1048
|
+
return factory.updateExportDeclaration(node, modifiers, node.isTypeOnly, node.exportClause, node.moduleSpecifier, node.attributes);
|
|
1049
|
+
if (node instanceof ExportAssignment)
|
|
1050
|
+
return factory.updateExportAssignment(node, modifiers, node.isExportEquals, node.type, node.expression);
|
|
1051
|
+
return node;
|
|
1052
|
+
}
|
|
1053
|
+
/** Class members: parameter properties, a `#private` marker and the visited members. */
|
|
1054
|
+
/** The members of a class declaration or expression. */
|
|
1055
|
+
buildClassMembers(classNode, extraMembers) {
|
|
1056
|
+
const classMembers = getMembers(classNode);
|
|
1057
|
+
const tracker = this.tracker;
|
|
1058
|
+
const parameterProperties = [];
|
|
1059
|
+
const constructor = getFirstConstructorWithBody(classMembers);
|
|
1060
|
+
if (constructor !== undefined) {
|
|
1061
|
+
const oldContext = tracker.diagnosticContext;
|
|
1062
|
+
for (const parameter of constructor.parameters.nodes) {
|
|
1063
|
+
if (!(parameter instanceof ParameterDeclaration))
|
|
1064
|
+
continue;
|
|
1065
|
+
if (!hasSyntacticModifier(parameter, 31 /* ModifierFlags.ParameterPropertyModifier */) ||
|
|
1066
|
+
this.shouldStripInternal(parameter))
|
|
1067
|
+
continue;
|
|
1068
|
+
tracker.diagnosticContext = declarationContext(parameter);
|
|
1069
|
+
if (parameter.name instanceof Identifier) {
|
|
1070
|
+
const property = this.factory.created(new PropertyDeclaration(this.ensureModifiers(parameter), parameter.name, parameter.questionToken, this.ensureType(parameter, false), this.ensureNoInitializer(parameter)));
|
|
1071
|
+
this.context.assignCommentRange(property, parameter);
|
|
1072
|
+
parameterProperties.push(property);
|
|
1073
|
+
}
|
|
1074
|
+
else if (parameter.name instanceof BindingPattern)
|
|
1075
|
+
// A binding pattern is an error, but written as properties anyway.
|
|
1076
|
+
this.walkBindingPattern(parameter.name, parameter, parameterProperties);
|
|
1077
|
+
}
|
|
1078
|
+
tracker.diagnosticContext = oldContext;
|
|
1079
|
+
}
|
|
1080
|
+
const members = [];
|
|
1081
|
+
// A class with private names gets a `#private` member to keep it nominal.
|
|
1082
|
+
for (const member of classMembers.nodes) {
|
|
1083
|
+
const name = getName(member);
|
|
1084
|
+
if (name instanceof PrivateIdentifier) {
|
|
1085
|
+
members.push(this.factory.created(new PropertyDeclaration(undefined, this.factory.created(new PrivateIdentifier('#private')), undefined, undefined, undefined)));
|
|
1086
|
+
break;
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
const lateIndexes = this.resolver.createLateBoundIndexSignatures(this.context, classNode, this.enclosingDeclaration, 531469 /* NodeBuilderFlags.DeclarationEmit */, tracker);
|
|
1090
|
+
for (const index of lateIndexes)
|
|
1091
|
+
members.push(index);
|
|
1092
|
+
for (const property of parameterProperties)
|
|
1093
|
+
members.push(property);
|
|
1094
|
+
for (const member of extraMembers)
|
|
1095
|
+
members.push(member);
|
|
1096
|
+
const visited = this.visitNodes(classMembers);
|
|
1097
|
+
if (visited !== undefined)
|
|
1098
|
+
for (const member of visited.nodes)
|
|
1099
|
+
members.push(member);
|
|
1100
|
+
return this.factory.createNodeList(members);
|
|
1101
|
+
}
|
|
1102
|
+
walkBindingPattern(pattern, parameter, result) {
|
|
1103
|
+
for (const element of pattern.elements.nodes) {
|
|
1104
|
+
if (!(element instanceof BindingElement) || element.name === undefined)
|
|
1105
|
+
continue;
|
|
1106
|
+
if (element.name instanceof BindingPattern) {
|
|
1107
|
+
this.walkBindingPattern(element.name, parameter, result);
|
|
1108
|
+
continue;
|
|
1109
|
+
}
|
|
1110
|
+
result.push(this.factory.created(new PropertyDeclaration(this.ensureModifiers(parameter), element.name, undefined, this.ensureType(element, false), undefined)));
|
|
1111
|
+
}
|
|
1112
|
+
return true;
|
|
1113
|
+
}
|
|
1114
|
+
transformClassDeclaration(input) {
|
|
1115
|
+
const previousEnclosingDeclaration = this.enclosingDeclaration;
|
|
1116
|
+
this.enclosingDeclaration = defined(input);
|
|
1117
|
+
const tracker = this.tracker;
|
|
1118
|
+
tracker.errorNameNode = input.name;
|
|
1119
|
+
tracker.pushErrorFallbackNode(input);
|
|
1120
|
+
const modifiers = this.ensureModifiers(input);
|
|
1121
|
+
const typeParameters = this.ensureTypeParams(input, input.typeParameters);
|
|
1122
|
+
const extraMembers = isInJSFile(input) ? this.collectThisPropertyAssignments(input) : [];
|
|
1123
|
+
const members = this.buildClassMembers(input, extraMembers);
|
|
1124
|
+
const extendsClause = getClassExtendsHeritageElement(input);
|
|
1125
|
+
let result;
|
|
1126
|
+
if (extendsClause !== undefined &&
|
|
1127
|
+
!isEntityNameExpression(extendsClause.expression, false) &&
|
|
1128
|
+
extendsClause.expression.kind !== 105 /* SyntaxKind.NullKeyword */) {
|
|
1129
|
+
// `class C extends f() {}` becomes `declare const C_base: T; class C extends C_base {}`.
|
|
1130
|
+
tracker.reportInferenceFallback(extendsClause.expression);
|
|
1131
|
+
const oldId = input.name !== undefined && nodeIsPresent(input.name) && input.name.text.length > 0
|
|
1132
|
+
? input.name.text
|
|
1133
|
+
: 'default';
|
|
1134
|
+
const newId = this.factory.createUniqueName(oldId + '_base', 16 /* GeneratedIdentifierFlags.Optimistic */);
|
|
1135
|
+
tracker.diagnosticContext = new DiagnosticContext(4 /* DiagnosticContextKind.ExtendsExpression */, extendsClause, input.name);
|
|
1136
|
+
const declaration = this.factory.createVariableDeclaration(newId, undefined, this.resolver.createTypeOfExpression(this.context, extendsClause.expression, input, 531469 /* NodeBuilderFlags.DeclarationEmit */, tracker) ?? this.keyword(132 /* SyntaxKind.AnyKeyword */), undefined);
|
|
1137
|
+
const statement = this.factory.createVariableStatement(this.needsDeclare
|
|
1138
|
+
? this.factory.createModifierList([this.factory.createToken(137 /* SyntaxKind.DeclareKeyword */)])
|
|
1139
|
+
: undefined, this.factory.createVariableDeclarationList(this.factory.createNodeList([declaration]), 2 /* NodeFlags.Const */));
|
|
1140
|
+
const clause = extendsClause.parent;
|
|
1141
|
+
const heritageClause = this.factory.updateHeritageClause(clause, clause.token, this.factory.createNodeList([
|
|
1142
|
+
defined(this.factory.updateExpressionWithTypeArguments(extendsClause, newId, this.visitNodes(extendsClause.typeArguments))),
|
|
1143
|
+
]));
|
|
1144
|
+
const heritageClauses = [defined(heritageClause)];
|
|
1145
|
+
// The rest are `implements` clauses.
|
|
1146
|
+
const retained = this.visitNodes(input.heritageClauses);
|
|
1147
|
+
if (retained !== undefined)
|
|
1148
|
+
for (const other of retained.nodes)
|
|
1149
|
+
heritageClauses.push(other);
|
|
1150
|
+
result = this.factory.createSyntaxList([
|
|
1151
|
+
statement,
|
|
1152
|
+
defined(this.factory.updateClassDeclaration(input, modifiers, input.name, typeParameters, this.factory.createNodeList(heritageClauses), members)),
|
|
1153
|
+
]);
|
|
1154
|
+
}
|
|
1155
|
+
else
|
|
1156
|
+
result = defined(this.factory.updateClassDeclaration(input, modifiers, input.name, typeParameters, this.visitNodes(input.heritageClauses), members));
|
|
1157
|
+
tracker.popErrorFallbackNode();
|
|
1158
|
+
this.enclosingDeclaration = previousEnclosingDeclaration;
|
|
1159
|
+
return result;
|
|
1160
|
+
}
|
|
1161
|
+
transformVariableStatement(input) {
|
|
1162
|
+
const declarations = input.declarationList.declarations.nodes;
|
|
1163
|
+
let visible = false;
|
|
1164
|
+
for (const declaration of declarations)
|
|
1165
|
+
if (this.getBindingNameVisible(declaration)) {
|
|
1166
|
+
visible = true;
|
|
1167
|
+
break;
|
|
1168
|
+
}
|
|
1169
|
+
if (!visible)
|
|
1170
|
+
return undefined;
|
|
1171
|
+
let normal = declarations;
|
|
1172
|
+
const imports = [];
|
|
1173
|
+
const file = this.tracker.currentSourceFile;
|
|
1174
|
+
if (file !== undefined && file.commonJSModuleIndicator !== undefined) {
|
|
1175
|
+
// `const x = require("m")` declarations become imports.
|
|
1176
|
+
normal = [];
|
|
1177
|
+
for (const declaration of declarations) {
|
|
1178
|
+
if (isVariableDeclarationInitializedToRequire(declaration)) {
|
|
1179
|
+
const visited = this.visit(declaration);
|
|
1180
|
+
if (visited !== undefined)
|
|
1181
|
+
imports.push(visited);
|
|
1182
|
+
}
|
|
1183
|
+
else
|
|
1184
|
+
normal.push(declaration);
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
const nodes = this.visitArray(normal);
|
|
1188
|
+
if (nodes.length === 0)
|
|
1189
|
+
return imports.length > 0 ? this.factory.createSyntaxList(imports) : undefined;
|
|
1190
|
+
const list = this.factory.createNodeList(nodes);
|
|
1191
|
+
const modifiers = this.ensureModifiers(input);
|
|
1192
|
+
const declarationList = input.declarationList;
|
|
1193
|
+
let updatedList;
|
|
1194
|
+
const blockScope = getCombinedNodeFlags(declarationList) & 7 /* NodeFlags.BlockScoped */;
|
|
1195
|
+
if (blockScope === 4 /* NodeFlags.Using */ || blockScope === 6 /* NodeFlags.AwaitUsing */) {
|
|
1196
|
+
// `using` declarations are written as `const`.
|
|
1197
|
+
updatedList = this.factory.createVariableDeclarationList(list, 2 /* NodeFlags.Const */);
|
|
1198
|
+
this.context.setOriginal(updatedList, declarationList);
|
|
1199
|
+
this.context.setCommentRange(updatedList, declarationList.pos, declarationList.end);
|
|
1200
|
+
updatedList.pos = declarationList.pos;
|
|
1201
|
+
updatedList.end = declarationList.end;
|
|
1202
|
+
}
|
|
1203
|
+
else
|
|
1204
|
+
updatedList = this.factory.updateVariableDeclarationList(declarationList, list, declarationList.flags);
|
|
1205
|
+
const result = this.factory.updateVariableStatement(input, modifiers, updatedList);
|
|
1206
|
+
if (imports.length === 0)
|
|
1207
|
+
return result;
|
|
1208
|
+
imports.push(defined(result));
|
|
1209
|
+
return this.factory.createSyntaxList(imports);
|
|
1210
|
+
}
|
|
1211
|
+
transformEnumDeclaration(input) {
|
|
1212
|
+
const members = [];
|
|
1213
|
+
for (const member of input.members.nodes) {
|
|
1214
|
+
if (member === undefined || !(member instanceof EnumMember) || this.shouldStripInternal(member))
|
|
1215
|
+
continue;
|
|
1216
|
+
// Initializers are rewritten to their constant values.
|
|
1217
|
+
const value = this.resolver.getEnumMemberValue(member);
|
|
1218
|
+
if (this.tracker.isolatedDeclarations &&
|
|
1219
|
+
member.initializer !== undefined &&
|
|
1220
|
+
value.hasExternalReferences &&
|
|
1221
|
+
member.name.kind !== 168 /* SyntaxKind.ComputedPropertyName */)
|
|
1222
|
+
this.tracker.addDiagnostic(createDiagnosticForNode(member, 1785 /* Diagnostics.Enum_member_initializers_must_be_computable_without_references_to_external_symbols_with_isolatedDeclarations */, []));
|
|
1223
|
+
let initializer = undefined;
|
|
1224
|
+
if (value.kind === 1 /* ConstantKind.Number */)
|
|
1225
|
+
initializer = this.createNumber(value.number);
|
|
1226
|
+
else if (value.kind === 2 /* ConstantKind.String */)
|
|
1227
|
+
initializer = this.factory.createStringLiteral(value.string, 0 /* TokenFlags.None */);
|
|
1228
|
+
const result = defined(this.factory.updateEnumMember(member, member.name, initializer));
|
|
1229
|
+
this.context.assignCommentRange(result, member);
|
|
1230
|
+
members.push(result);
|
|
1231
|
+
}
|
|
1232
|
+
return this.factory.updateEnumDeclaration(input, this.ensureModifiers(input), input.name, this.factory.createNodeList(members));
|
|
1233
|
+
}
|
|
1234
|
+
/** A number as an expression: a literal, `-literal`, `Infinity`, `-Infinity` or `NaN`. */
|
|
1235
|
+
createNumber(value) {
|
|
1236
|
+
const factory = this.factory;
|
|
1237
|
+
if (value !== value)
|
|
1238
|
+
return factory.createIdentifier('NaN');
|
|
1239
|
+
if (value === Infinity)
|
|
1240
|
+
return factory.createIdentifier('Infinity');
|
|
1241
|
+
if (value === -Infinity)
|
|
1242
|
+
return factory.created(new PrefixUnaryExpression(40 /* SyntaxKind.MinusToken */, factory.createIdentifier('Infinity')));
|
|
1243
|
+
if (value < 0 || (value === 0 && 1 / value < 0))
|
|
1244
|
+
return factory.created(new PrefixUnaryExpression(40 /* SyntaxKind.MinusToken */, factory.createNumericLiteral(String(-value))));
|
|
1245
|
+
return factory.createNumericLiteral(String(value));
|
|
1246
|
+
}
|
|
1247
|
+
// JavaScript and assigned properties
|
|
1248
|
+
/** `const x = require("m")` as `import x = require("m")`, `const { a } = require("m")` as `import { a } from "m"`. */
|
|
1249
|
+
transformCjsRequireVariableDeclaration(input) {
|
|
1250
|
+
const call = skipParenthesesOnly(input.initializer);
|
|
1251
|
+
const specifier = this.rewriteModuleSpecifier(input, getArguments(call).nodes[0]);
|
|
1252
|
+
const name = input.name;
|
|
1253
|
+
if (name instanceof Identifier)
|
|
1254
|
+
return this.factory.created(new ImportEqualsDeclaration(undefined, false, name, this.factory.created(new ExternalModuleReference(specifier))));
|
|
1255
|
+
if (!(name instanceof BindingPattern) || name.kind === 208 /* SyntaxKind.ArrayBindingPattern */)
|
|
1256
|
+
return undefined;
|
|
1257
|
+
const specifiers = [];
|
|
1258
|
+
for (const element of name.elements.nodes) {
|
|
1259
|
+
// Nested destructuring is skipped.
|
|
1260
|
+
if (!(element instanceof BindingElement) || !(element.name instanceof Identifier))
|
|
1261
|
+
continue;
|
|
1262
|
+
specifiers.push(this.factory.created(new ImportSpecifier(false, element.propertyName, element.name)));
|
|
1263
|
+
}
|
|
1264
|
+
const clause = this.factory.created(new ImportClause(undefined, undefined, this.factory.created(new NamedImports(this.factory.createNodeList(specifiers)))));
|
|
1265
|
+
return this.factory.created(new ImportDeclaration(273 /* SyntaxKind.ImportDeclaration */, undefined, clause, specifier, undefined));
|
|
1266
|
+
}
|
|
1267
|
+
/** Finds `module.exports =` assignments anywhere in the file. */
|
|
1268
|
+
visitModuleExportsAssignment(node) {
|
|
1269
|
+
const file = this.tracker.currentSourceFile;
|
|
1270
|
+
if (file.commonJSModuleIndicator === undefined ||
|
|
1271
|
+
getAssignmentDeclarationKind(node) !== 1 /* JSDeclarationKind.ModuleExports */)
|
|
1272
|
+
return false;
|
|
1273
|
+
const scope = this.enterDiagnosticContext(node);
|
|
1274
|
+
const result = this.transformExportAssignment(node.parent, node, node.right, true);
|
|
1275
|
+
this.exitDiagnosticContext(scope);
|
|
1276
|
+
if (result === undefined)
|
|
1277
|
+
return false;
|
|
1278
|
+
this.cjsExportAssignment = result;
|
|
1279
|
+
this.resultHasScopeMarker = true;
|
|
1280
|
+
this.resultHasExternalModuleIndicator = true;
|
|
1281
|
+
return true;
|
|
1282
|
+
}
|
|
1283
|
+
/** Finds assigned properties (`f.x = 1`) and CommonJS exports (`exports.x = 1`) anywhere in the file. */
|
|
1284
|
+
visitNestedExpression(node) {
|
|
1285
|
+
if (node === undefined)
|
|
1286
|
+
return false;
|
|
1287
|
+
const kind = getAssignmentDeclarationKind(node);
|
|
1288
|
+
if (kind === 0 /* JSDeclarationKind.None */)
|
|
1289
|
+
return false;
|
|
1290
|
+
const scope = this.enterDiagnosticContext(node);
|
|
1291
|
+
const file = this.tracker.currentSourceFile;
|
|
1292
|
+
switch (kind) {
|
|
1293
|
+
case 4 /* JSDeclarationKind.Property */:
|
|
1294
|
+
if (node instanceof BinaryExpression)
|
|
1295
|
+
this.transformExpandoAssignment(node);
|
|
1296
|
+
break;
|
|
1297
|
+
case 2 /* JSDeclarationKind.ExportsProperty */:
|
|
1298
|
+
if (file.commonJSModuleIndicator !== undefined) {
|
|
1299
|
+
const name = getElementOrPropertyAccessName(node.left);
|
|
1300
|
+
if (name !== undefined) {
|
|
1301
|
+
const result = this.transformCommonJSExport(node, this.getNameExpressionPreferringIdentifier(name));
|
|
1302
|
+
if (result !== undefined)
|
|
1303
|
+
this.cjsExportMembers.push(result);
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
break;
|
|
1307
|
+
case 6 /* JSDeclarationKind.ObjectDefinePropertyExports */:
|
|
1308
|
+
if (file.commonJSModuleIndicator !== undefined) {
|
|
1309
|
+
const result = this.transformCommonJSExport(node, this.getNameExpressionPreferringIdentifier(getArguments(node).nodes[1]));
|
|
1310
|
+
if (result !== undefined)
|
|
1311
|
+
this.cjsExportMembers.push(result);
|
|
1312
|
+
}
|
|
1313
|
+
break;
|
|
1314
|
+
}
|
|
1315
|
+
this.exitDiagnosticContext(scope);
|
|
1316
|
+
return true;
|
|
1317
|
+
}
|
|
1318
|
+
/** A property name as an identifier when it can be one; keywords other than `default` stay strings. */
|
|
1319
|
+
getNameExpressionPreferringIdentifier(name) {
|
|
1320
|
+
let result = name;
|
|
1321
|
+
if (name instanceof NumericLiteral)
|
|
1322
|
+
result = this.factory.createStringLiteral(name.text, 0 /* TokenFlags.None */);
|
|
1323
|
+
if ((result.kind === 10 /* SyntaxKind.StringLiteral */ || result.kind === 14 /* SyntaxKind.NoSubstitutionTemplateLiteral */) &&
|
|
1324
|
+
isIdentifierText(getText(result))) {
|
|
1325
|
+
const text = getText(result);
|
|
1326
|
+
const keyword = identifierToKeywordKind(text);
|
|
1327
|
+
if (keyword === 0 /* SyntaxKind.Unknown */ ||
|
|
1328
|
+
keyword === 79 /* SyntaxKind.Identifier */ ||
|
|
1329
|
+
keyword === 89 /* SyntaxKind.DefaultKeyword */) {
|
|
1330
|
+
// A parse tree node, so names it uses are resolved where it's written.
|
|
1331
|
+
const identifier = new Identifier(text);
|
|
1332
|
+
identifier.parent = name.parent;
|
|
1333
|
+
return identifier;
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
return result;
|
|
1337
|
+
}
|
|
1338
|
+
transformCommonJSExport(input, name) {
|
|
1339
|
+
const result = this.transformCommonJSExportWorker(input, name);
|
|
1340
|
+
return result === undefined ? undefined : this.wrapInCJSExportNamespace(result);
|
|
1341
|
+
}
|
|
1342
|
+
transformCommonJSExportWorker(input, name) {
|
|
1343
|
+
if (input === undefined)
|
|
1344
|
+
return undefined;
|
|
1345
|
+
const nameText = name.kind === 79 /* SyntaxKind.Identifier */ || name.kind === 10 /* SyntaxKind.StringLiteral */ ? getText(name) : '';
|
|
1346
|
+
if (nameText !== '' && this.witnessedCjsExports.has(nameText))
|
|
1347
|
+
return undefined;
|
|
1348
|
+
this.witnessedCjsExports.add(nameText);
|
|
1349
|
+
this.resultHasExternalModuleIndicator = true;
|
|
1350
|
+
this.resultHasScopeMarker = true;
|
|
1351
|
+
const tracker = this.tracker;
|
|
1352
|
+
// At the top level, `exports.x = y` is written as `export { y as x }`.
|
|
1353
|
+
const statement = input.parent;
|
|
1354
|
+
if (isCommonJSAliasExport(input) &&
|
|
1355
|
+
statement !== undefined &&
|
|
1356
|
+
statement.kind === 245 /* SyntaxKind.ExpressionStatement */ &&
|
|
1357
|
+
statement.parent !== undefined &&
|
|
1358
|
+
statement.parent.kind === 307 /* SyntaxKind.SourceFile */)
|
|
1359
|
+
return this.transformBinaryExpressionToExportDeclaration(input, name);
|
|
1360
|
+
if (input instanceof BinaryExpression) {
|
|
1361
|
+
const right = skipParenthesesOnly(input.right);
|
|
1362
|
+
if (right instanceof ClassExpression)
|
|
1363
|
+
return this.transformCommonJSClassExport(input, right, name);
|
|
1364
|
+
}
|
|
1365
|
+
if (name instanceof Identifier) {
|
|
1366
|
+
if (name.text === 'default') {
|
|
1367
|
+
// `declare const _default: T; export default _default;`
|
|
1368
|
+
const newId = this.factory.createUniqueName('_default', 16 /* GeneratedIdentifierFlags.Optimistic */);
|
|
1369
|
+
tracker.diagnosticContext = new DiagnosticContext(3 /* DiagnosticContextKind.DefaultExport */, input, undefined);
|
|
1370
|
+
tracker.pushErrorFallbackNode(input);
|
|
1371
|
+
const declaration = this.factory.createVariableDeclaration(newId, undefined, this.ensureType(input, false), undefined);
|
|
1372
|
+
tracker.popErrorFallbackNode();
|
|
1373
|
+
const variable = this.createConstStatement(declaration);
|
|
1374
|
+
const assignment = this.factory.created(new ExportAssignment(getModifiers(input), false, undefined, newId));
|
|
1375
|
+
this.context.assignCommentRange(variable, input);
|
|
1376
|
+
this.context.addEmitFlags(assignment, 384 /* EmitFlags.NoComments */);
|
|
1377
|
+
return this.factory.createSyntaxList([variable, assignment]);
|
|
1378
|
+
}
|
|
1379
|
+
const referenced = this.resolver.getReferencedValueDeclaration(name);
|
|
1380
|
+
// Inlined as `export var x: T` unless the name refers to something else.
|
|
1381
|
+
if (referenced === input || referenced === undefined) {
|
|
1382
|
+
tracker.pushErrorFallbackNode(input);
|
|
1383
|
+
const declaration = this.factory.createVariableDeclaration(name, undefined, this.ensureType(input, false), undefined);
|
|
1384
|
+
tracker.popErrorFallbackNode();
|
|
1385
|
+
const modifiers = [this.factory.createToken(94 /* SyntaxKind.ExportKeyword */)];
|
|
1386
|
+
if (this.needsDeclare)
|
|
1387
|
+
modifiers.push(this.factory.createToken(137 /* SyntaxKind.DeclareKeyword */));
|
|
1388
|
+
return this.factory.createVariableStatement(this.factory.createModifierList(modifiers), this.factory.createVariableDeclarationList(this.factory.createNodeList([declaration]), 0 /* NodeFlags.None */));
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
// `declare const _exported: T; export { _exported as "name" };`
|
|
1392
|
+
const newId = this.factory.createUniqueName('_exported', 16 /* GeneratedIdentifierFlags.Optimistic */);
|
|
1393
|
+
tracker.diagnosticContext = new DiagnosticContext(3 /* DiagnosticContextKind.DefaultExport */, input, undefined);
|
|
1394
|
+
tracker.pushErrorFallbackNode(input);
|
|
1395
|
+
const declaration = this.factory.createVariableDeclaration(newId, undefined, this.ensureType(input, false), undefined);
|
|
1396
|
+
tracker.popErrorFallbackNode();
|
|
1397
|
+
const variable = this.createConstStatement(declaration);
|
|
1398
|
+
const exportDeclaration = this.createExportDeclaration(newId, name);
|
|
1399
|
+
this.context.assignCommentRange(variable, input);
|
|
1400
|
+
this.context.addEmitFlags(exportDeclaration, 384 /* EmitFlags.NoComments */);
|
|
1401
|
+
return this.factory.createSyntaxList([variable, exportDeclaration]);
|
|
1402
|
+
}
|
|
1403
|
+
/** `exports.K = class K {}` as a class declaration, in a namespace if its name differs or is used inside. */
|
|
1404
|
+
transformCommonJSClassExport(input, right, name) {
|
|
1405
|
+
const factory = this.factory;
|
|
1406
|
+
const tracker = this.tracker;
|
|
1407
|
+
const classExpressionName = right.name;
|
|
1408
|
+
if (classExpressionName !== undefined && classExpressionName.text.length > 0) {
|
|
1409
|
+
// Watch for the class naming itself in its members' types.
|
|
1410
|
+
tracker.watchedClassSymbol = right.symbol;
|
|
1411
|
+
tracker.classSymbolTracked = false;
|
|
1412
|
+
const className = factory.createIdentifier(classExpressionName.text);
|
|
1413
|
+
const classDeclaration = this.transformClassExpressionToDeclaration(right, className, factory.createModifierList([factory.createToken(94 /* SyntaxKind.ExportKeyword */)]));
|
|
1414
|
+
this.context.assignCommentRange(classDeclaration, input);
|
|
1415
|
+
const namesDiffer = !(name instanceof Identifier) || classExpressionName.text !== name.text;
|
|
1416
|
+
const needsIsolation = namesDiffer || tracker.classSymbolTracked;
|
|
1417
|
+
tracker.watchedClassSymbol = undefined;
|
|
1418
|
+
tracker.classSymbolTracked = false;
|
|
1419
|
+
if (needsIsolation) {
|
|
1420
|
+
const namespaceName = factory.createUniqueName('_ns', 16 /* GeneratedIdentifierFlags.Optimistic */);
|
|
1421
|
+
const namespace = factory.created(new ModuleDeclaration(this.declareModifiers(), 145 /* SyntaxKind.NamespaceKeyword */, namespaceName, factory.created(new ModuleBlock(factory.createNodeList([classDeclaration])))));
|
|
1422
|
+
const aliasBase = name instanceof Identifier && isIdentifierText('_' + name.text) ? '_' + name.text : '_exported';
|
|
1423
|
+
const alias = factory.createUniqueName(aliasBase, 16 /* GeneratedIdentifierFlags.Optimistic */);
|
|
1424
|
+
const importEquals = factory.created(new ImportEqualsDeclaration(undefined, false, alias, factory.created(new QualifiedName(namespaceName, className))));
|
|
1425
|
+
const exportDeclaration = this.createExportDeclaration(alias, name);
|
|
1426
|
+
this.context.addEmitFlags(exportDeclaration, 384 /* EmitFlags.NoComments */);
|
|
1427
|
+
return factory.createSyntaxList([namespace, importEquals, exportDeclaration]);
|
|
1428
|
+
}
|
|
1429
|
+
const modifiers = [factory.createToken(94 /* SyntaxKind.ExportKeyword */)];
|
|
1430
|
+
if (this.needsDeclare)
|
|
1431
|
+
modifiers.push(factory.createToken(137 /* SyntaxKind.DeclareKeyword */));
|
|
1432
|
+
return this.replaceModifiers(classDeclaration, factory.createModifierList(modifiers));
|
|
1433
|
+
}
|
|
1434
|
+
const modifiers = [factory.createToken(94 /* SyntaxKind.ExportKeyword */)];
|
|
1435
|
+
if (this.needsDeclare)
|
|
1436
|
+
modifiers.push(factory.createToken(137 /* SyntaxKind.DeclareKeyword */));
|
|
1437
|
+
const className = name !== undefined && name instanceof Identifier
|
|
1438
|
+
? name
|
|
1439
|
+
: factory.createUniqueName('_class', 16 /* GeneratedIdentifierFlags.Optimistic */);
|
|
1440
|
+
const classDeclaration = this.transformClassExpressionToDeclaration(right, className, factory.createModifierList(modifiers));
|
|
1441
|
+
this.context.assignCommentRange(classDeclaration, input);
|
|
1442
|
+
if (name instanceof Identifier)
|
|
1443
|
+
return classDeclaration;
|
|
1444
|
+
// A name that isn't an identifier is exported by a specifier.
|
|
1445
|
+
const exportDeclaration = this.createExportDeclaration(className, name);
|
|
1446
|
+
this.context.addEmitFlags(exportDeclaration, 384 /* EmitFlags.NoComments */);
|
|
1447
|
+
return factory.createSyntaxList([classDeclaration, exportDeclaration]);
|
|
1448
|
+
}
|
|
1449
|
+
createExportDeclaration(local, name) {
|
|
1450
|
+
const factory = this.factory;
|
|
1451
|
+
return factory.created(new ExportDeclaration(undefined, false, factory.created(new NamedExports(factory.createNodeList([factory.created(new ExportSpecifier(false, local, name))]))), undefined, undefined));
|
|
1452
|
+
}
|
|
1453
|
+
/** `exports.x = y` and `f.x = y` as `export { y as x }`. */
|
|
1454
|
+
transformBinaryExpressionToExportDeclaration(input, name) {
|
|
1455
|
+
let propertyName = input.right;
|
|
1456
|
+
// Keeps the declaration the name refers to.
|
|
1457
|
+
this.tracker.handleSymbolAccessibilityError(this.resolver.isEntityNameVisible(propertyName, this.enclosingDeclaration));
|
|
1458
|
+
if (name instanceof Identifier && propertyName instanceof Identifier && propertyName.text === name.text)
|
|
1459
|
+
propertyName = undefined;
|
|
1460
|
+
return this.createExportDeclaration(propertyName, name);
|
|
1461
|
+
}
|
|
1462
|
+
/** CommonJS exports of a file with `module.exports =` are members of a namespace of its name. */
|
|
1463
|
+
wrapInCJSExportNamespace(content) {
|
|
1464
|
+
const name = this.cjsExportAssignmentName;
|
|
1465
|
+
if (name === undefined)
|
|
1466
|
+
return content;
|
|
1467
|
+
const members = content instanceof SyntaxList ? content.children : [content];
|
|
1468
|
+
const stripped = [];
|
|
1469
|
+
for (const member of members)
|
|
1470
|
+
stripped.push(this.stripDeclareModifier(member));
|
|
1471
|
+
return this.factory.created(new ModuleDeclaration(this.declareModifiers(), 145 /* SyntaxKind.NamespaceKeyword */, name, this.factory.created(new ModuleBlock(this.factory.createNodeList(stripped)))));
|
|
1472
|
+
}
|
|
1473
|
+
stripDeclareModifier(node) {
|
|
1474
|
+
const modifiers = getModifiers(node);
|
|
1475
|
+
if (modifiers === undefined || (modifiers.modifierFlags & 128 /* ModifierFlags.Ambient */) === 0)
|
|
1476
|
+
return node;
|
|
1477
|
+
const kept = [];
|
|
1478
|
+
for (const modifier of modifiers.nodes)
|
|
1479
|
+
if (modifier.kind !== 137 /* SyntaxKind.DeclareKeyword */)
|
|
1480
|
+
kept.push(modifier);
|
|
1481
|
+
return this.replaceModifiers(node, this.factory.createModifierList(kept));
|
|
1482
|
+
}
|
|
1483
|
+
/** Writes `f.x = 1` as a namespace member of `f`, kept with `f` if it's emitted. */
|
|
1484
|
+
transformExpandoAssignment(node) {
|
|
1485
|
+
const symbol = node.symbol;
|
|
1486
|
+
if (symbol === undefined || (symbol.flags & 67108864 /* SymbolFlags.Assignment */) === 0)
|
|
1487
|
+
return false;
|
|
1488
|
+
const left = node.left;
|
|
1489
|
+
const namespace = getLeftmostAccessExpression(left);
|
|
1490
|
+
if (!(namespace instanceof Identifier))
|
|
1491
|
+
return false;
|
|
1492
|
+
const declaration = this.resolver.getReferencedValueDeclaration(namespace);
|
|
1493
|
+
if (declaration === undefined || this.shouldStripInternal(declaration))
|
|
1494
|
+
return false;
|
|
1495
|
+
if (declaration instanceof VariableDeclaration && declaration.type !== undefined)
|
|
1496
|
+
return false;
|
|
1497
|
+
if (declaration instanceof FunctionDeclaration && declaration.fullSignature !== undefined)
|
|
1498
|
+
return false;
|
|
1499
|
+
// A variable gets a type instead, unless it's a function.
|
|
1500
|
+
if (declaration instanceof VariableDeclaration &&
|
|
1501
|
+
(declaration.initializer === undefined || !isFunctionLike(declaration.initializer)))
|
|
1502
|
+
return false;
|
|
1503
|
+
const host = getSymbol(declaration);
|
|
1504
|
+
if (host === undefined)
|
|
1505
|
+
return false;
|
|
1506
|
+
const name = this.factory.createIdentifier(namespace.text);
|
|
1507
|
+
const property = this.tryGetPropertyName(left);
|
|
1508
|
+
if (property === '' || !isIdentifierText(property))
|
|
1509
|
+
return false;
|
|
1510
|
+
if (isDeclaration(declaration) && this.isDeclarationAndNotVisible(declaration))
|
|
1511
|
+
return false;
|
|
1512
|
+
if (declaration instanceof FunctionDeclaration && !shouldEmitFunctionProperties(declaration))
|
|
1513
|
+
return false;
|
|
1514
|
+
this.transformExpandoHost(name, declaration);
|
|
1515
|
+
const exportName = this.factory.createIdentifier(property);
|
|
1516
|
+
let localName = this.tryGetNameOfAssignedExpression(node);
|
|
1517
|
+
// The property's name works as the local name if it neither conflicts nor is a keyword.
|
|
1518
|
+
if (localName === undefined &&
|
|
1519
|
+
!this.resolver.isNameResolvable(this.enclosingDeclaration, property) &&
|
|
1520
|
+
!isNonContextualKeyword(stringToToken(property)))
|
|
1521
|
+
localName = exportName;
|
|
1522
|
+
if (localName === undefined || isNonContextualKeyword(stringToToken(localName.text)))
|
|
1523
|
+
localName = this.factory.createGeneratedNameForNode(node, 0 /* GeneratedIdentifierFlags.None */, '', '');
|
|
1524
|
+
const hostId = this.getExpandoHostId(declaration);
|
|
1525
|
+
const scope = this.enterDiagnosticContext(node);
|
|
1526
|
+
let members = this.expandoMembers.get(hostId);
|
|
1527
|
+
if (members === undefined) {
|
|
1528
|
+
members = [];
|
|
1529
|
+
this.expandoMembers.set(hostId, members);
|
|
1530
|
+
}
|
|
1531
|
+
if (node.right instanceof Identifier) {
|
|
1532
|
+
// Alias-like: `export { name }` or `export { name as alias }`.
|
|
1533
|
+
members.push(this.transformBinaryExpressionToExportDeclaration(node, exportName));
|
|
1534
|
+
this.exitDiagnosticContext(scope);
|
|
1535
|
+
return true;
|
|
1536
|
+
}
|
|
1537
|
+
let hasExport = false;
|
|
1538
|
+
for (const member of members)
|
|
1539
|
+
if (member.kind === 279 /* SyntaxKind.ExportDeclaration */)
|
|
1540
|
+
hasExport = true;
|
|
1541
|
+
const variable = this.factory.createVariableStatement(hasExport ? this.factory.createModifierList([this.factory.createToken(94 /* SyntaxKind.ExportKeyword */)]) : undefined, this.factory.createVariableDeclarationList(this.factory.createNodeList([
|
|
1542
|
+
this.factory.createVariableDeclaration(localName, undefined, this.ensureType(node, false), undefined),
|
|
1543
|
+
]), 0 /* NodeFlags.None */));
|
|
1544
|
+
const statements = [variable];
|
|
1545
|
+
const local = defined(localName);
|
|
1546
|
+
if (local.text !== exportName.text || this.context.hasAutoGenerateInfo(local))
|
|
1547
|
+
statements.push(this.createExportDeclaration(local, exportName));
|
|
1548
|
+
// Members stay exported after an `export {}` is added.
|
|
1549
|
+
if (statements.length > 1 && !hasExport)
|
|
1550
|
+
for (let i = 0; i < members.length; i++)
|
|
1551
|
+
members[i] = this.replaceModifiers(members[i], this.createModifiers(32 /* ModifierFlags.Export */ | getCombinedModifierFlags(members[i])));
|
|
1552
|
+
for (const statement of statements)
|
|
1553
|
+
members.push(statement);
|
|
1554
|
+
this.exitDiagnosticContext(scope);
|
|
1555
|
+
return true;
|
|
1556
|
+
}
|
|
1557
|
+
tryGetPropertyName(node) {
|
|
1558
|
+
if (node instanceof ElementAccessExpression)
|
|
1559
|
+
return this.resolver.getElementAccessExpressionName(node);
|
|
1560
|
+
if (node instanceof PropertyAccessExpression && node.name instanceof Identifier)
|
|
1561
|
+
return node.name.text;
|
|
1562
|
+
return '';
|
|
1563
|
+
}
|
|
1564
|
+
getExpandoHostId(declaration) {
|
|
1565
|
+
const root = declaration instanceof VariableDeclaration ? declaration.parent.parent : declaration;
|
|
1566
|
+
return this.context.mostOriginal(root);
|
|
1567
|
+
}
|
|
1568
|
+
/** The function a property is assigned to, written as a declaration once. */
|
|
1569
|
+
transformExpandoHost(name, declaration) {
|
|
1570
|
+
if (name === undefined)
|
|
1571
|
+
return false;
|
|
1572
|
+
const root = declaration instanceof VariableDeclaration ? declaration.parent.parent : declaration;
|
|
1573
|
+
const id = this.getExpandoHostId(declaration);
|
|
1574
|
+
if (this.expandoHosts.has(id))
|
|
1575
|
+
return false;
|
|
1576
|
+
const saveNeedsDeclare = this.needsDeclare;
|
|
1577
|
+
this.needsDeclare = true;
|
|
1578
|
+
let flags = this.ensureModifierFlags(root);
|
|
1579
|
+
this.needsDeclare = saveNeedsDeclare;
|
|
1580
|
+
const defaultExport = (flags & 32 /* ModifierFlags.Export */) !== 0 && (flags & 2048 /* ModifierFlags.Default */) !== 0;
|
|
1581
|
+
if (defaultExport)
|
|
1582
|
+
flags = (flags | 128 /* ModifierFlags.Ambient */) & ~(2048 /* ModifierFlags.Default */ | 32 /* ModifierFlags.Export */);
|
|
1583
|
+
const scope = this.enterDiagnosticContext(declaration);
|
|
1584
|
+
const modifiers = this.createModifiers(flags);
|
|
1585
|
+
const replacement = [];
|
|
1586
|
+
if (declaration instanceof FunctionDeclaration)
|
|
1587
|
+
replacement.push(defined(this.factory.updateFunctionDeclaration(declaration, modifiers, declaration.asteriskToken, declaration.name, this.ensureTypeParams(declaration, declaration.typeParameters), this.updateParamList(declaration, declaration.parameters), this.ensureType(declaration, false), undefined, undefined)));
|
|
1588
|
+
else if (isFunctionValuedVariable(declaration)) {
|
|
1589
|
+
const fn = getInitializer(declaration);
|
|
1590
|
+
replacement.push(this.factory.created(new FunctionDeclaration(modifiers, asteriskOf(fn), this.factory.createIdentifier(name.text), this.ensureTypeParams(fn, getTypeParameters(fn)), this.updateParamList(fn, getParameters(fn)), this.ensureType(fn, false), undefined, undefined)));
|
|
1591
|
+
}
|
|
1592
|
+
else {
|
|
1593
|
+
this.expandoHosts.set(id, this.transformTopLevelDeclaration(declaration));
|
|
1594
|
+
this.exitDiagnosticContext(scope);
|
|
1595
|
+
return true;
|
|
1596
|
+
}
|
|
1597
|
+
this.tracker.reportExpandoFunctionErrors(declaration);
|
|
1598
|
+
if (defaultExport) {
|
|
1599
|
+
if (declaration.parent !== undefined && declaration.parent.kind === 307 /* SyntaxKind.SourceFile */)
|
|
1600
|
+
this.resultHasExternalModuleIndicator = true;
|
|
1601
|
+
this.resultHasScopeMarker = true;
|
|
1602
|
+
replacement.push(this.factory.created(new ExportAssignment(undefined, false, undefined, name)));
|
|
1603
|
+
}
|
|
1604
|
+
this.expandoHosts.set(id, this.factory.createSyntaxList(replacement));
|
|
1605
|
+
if (this.lateStatementReplacements.has(id))
|
|
1606
|
+
this.lateStatementReplacements.set(id, this.createFullExpandoBlock(id));
|
|
1607
|
+
this.exitDiagnosticContext(scope);
|
|
1608
|
+
return true;
|
|
1609
|
+
}
|
|
1610
|
+
/** A function with its assigned properties as a namespace of the same name. */
|
|
1611
|
+
createFullExpandoBlock(id) {
|
|
1612
|
+
const host = this.expandoHosts.get(id);
|
|
1613
|
+
const addOns = this.expandoMembers.get(id);
|
|
1614
|
+
if (addOns === undefined || host === undefined)
|
|
1615
|
+
return host;
|
|
1616
|
+
let name = undefined;
|
|
1617
|
+
let modifiers = undefined;
|
|
1618
|
+
const statements = host instanceof SyntaxList ? host.children : [host];
|
|
1619
|
+
for (const statement of statements) {
|
|
1620
|
+
const statementName = getName(statement);
|
|
1621
|
+
if (statementName === undefined)
|
|
1622
|
+
continue;
|
|
1623
|
+
name = this.factory.cloneNode(statementName, statementName.deepClone());
|
|
1624
|
+
const statementModifiers = getModifiers(statement);
|
|
1625
|
+
if (statementModifiers !== undefined)
|
|
1626
|
+
modifiers = this.factory.createModifierList(statementModifiers.nodes.slice());
|
|
1627
|
+
break;
|
|
1628
|
+
}
|
|
1629
|
+
if (name === undefined)
|
|
1630
|
+
return host;
|
|
1631
|
+
const result = statements.slice();
|
|
1632
|
+
result.push(this.factory.created(new ModuleDeclaration(modifiers, 145 /* SyntaxKind.NamespaceKeyword */, name, this.factory.created(new ModuleBlock(this.factory.createNodeList(addOns))))));
|
|
1633
|
+
return this.factory.createSyntaxList(result);
|
|
1634
|
+
}
|
|
1635
|
+
/** The `this.x = …` members of a JavaScript class, as property declarations. */
|
|
1636
|
+
collectThisPropertyAssignments(classNode) {
|
|
1637
|
+
const members = getMembers(classNode).nodes;
|
|
1638
|
+
this.seenProperties.clear();
|
|
1639
|
+
for (const member of members) {
|
|
1640
|
+
const name = getName(member);
|
|
1641
|
+
if (name !== undefined)
|
|
1642
|
+
this.seenProperties.add(thisPropertyKey(name, member, isStatic(member)));
|
|
1643
|
+
}
|
|
1644
|
+
this.thisPropertyAssignments = [];
|
|
1645
|
+
const walker = new ThisPropertyWalker(this, classNode);
|
|
1646
|
+
for (const member of members)
|
|
1647
|
+
member.forEachChild(walker);
|
|
1648
|
+
const result = this.thisPropertyAssignments;
|
|
1649
|
+
this.thisPropertyAssignments = [];
|
|
1650
|
+
this.seenProperties.clear();
|
|
1651
|
+
return result;
|
|
1652
|
+
}
|
|
1653
|
+
/** Collects a `this.x = …` assignment of a member of `classNode`. */
|
|
1654
|
+
visitThisPropertyAssignment(node, classNode) {
|
|
1655
|
+
if (getAssignmentDeclarationKind(node) !== 3 /* JSDeclarationKind.ThisProperty */)
|
|
1656
|
+
return false;
|
|
1657
|
+
const container = getThisContainer(node, false, false);
|
|
1658
|
+
if (container.parent !== classNode)
|
|
1659
|
+
return false;
|
|
1660
|
+
const isStaticMember = hasSyntacticModifier(container, 256 /* ModifierFlags.Static */) ||
|
|
1661
|
+
container.kind === 176 /* SyntaxKind.ClassStaticBlockDeclaration */;
|
|
1662
|
+
const assignment = node;
|
|
1663
|
+
let name = getElementOrPropertyAccessName(assignment.left);
|
|
1664
|
+
if (name === undefined)
|
|
1665
|
+
return false;
|
|
1666
|
+
const key = thisPropertyKey(name, node, isStaticMember);
|
|
1667
|
+
if (this.resolver.getReferencedMemberValueDeclaration(node) === undefined || this.seenProperties.has(key))
|
|
1668
|
+
return false;
|
|
1669
|
+
this.seenProperties.add(key);
|
|
1670
|
+
const heritage = getHeritageClauses(classNode);
|
|
1671
|
+
if (heritage !== undefined && heritage.nodes.length > 0 && !isClassExtendingNull(heritage)) {
|
|
1672
|
+
// A base class may provide the member; that needs types.
|
|
1673
|
+
this.tracker.reportInferenceFallback(classNode);
|
|
1674
|
+
if (this.resolver.isThisPropertyAssignmentDeclarationRedundant(node))
|
|
1675
|
+
return false;
|
|
1676
|
+
}
|
|
1677
|
+
if (hasDynamicName(node))
|
|
1678
|
+
return false;
|
|
1679
|
+
if (getTextOfPropertyName(name) === 'constructor')
|
|
1680
|
+
return false;
|
|
1681
|
+
if (name instanceof Identifier && !isIdentifierText(name.text))
|
|
1682
|
+
name = this.factory.createStringLiteralFromNode(name);
|
|
1683
|
+
const property = this.factory.created(new PropertyDeclaration(isStaticMember
|
|
1684
|
+
? this.factory.createModifierList([this.factory.createToken(125 /* SyntaxKind.StaticKeyword */)])
|
|
1685
|
+
: undefined, name, undefined, this.ensureType(node, false), undefined));
|
|
1686
|
+
if (node.parent !== undefined && node.parent.kind === 245 /* SyntaxKind.ExpressionStatement */)
|
|
1687
|
+
this.context.assignCommentRange(property, node.parent);
|
|
1688
|
+
this.thisPropertyAssignments.push(property);
|
|
1689
|
+
return true;
|
|
1690
|
+
}
|
|
1691
|
+
// Modifiers, types and parameters
|
|
1692
|
+
createModifiers(flags) {
|
|
1693
|
+
const nodes = [];
|
|
1694
|
+
for (const kind of modifierKindsInOrder)
|
|
1695
|
+
if ((flags & modifierToFlag(kind)) !== 0)
|
|
1696
|
+
nodes.push(this.factory.createToken((kind | 0)));
|
|
1697
|
+
return nodes.length === 0 ? undefined : this.factory.createModifierList(nodes);
|
|
1698
|
+
}
|
|
1699
|
+
ensureModifiers(node) {
|
|
1700
|
+
const parseNode = this.parseNode(node);
|
|
1701
|
+
const currentFlags = getCombinedModifierFlags(parseNode) & 131071 /* ModifierFlags.All */;
|
|
1702
|
+
const newFlags = this.ensureModifierFlags(node);
|
|
1703
|
+
if (currentFlags === newFlags) {
|
|
1704
|
+
// Decorators are dropped.
|
|
1705
|
+
const modifiers = getModifiers(node);
|
|
1706
|
+
if (modifiers === undefined)
|
|
1707
|
+
return undefined;
|
|
1708
|
+
let reusable = true;
|
|
1709
|
+
for (const modifier of modifiers.nodes)
|
|
1710
|
+
if (isModifier(modifier) && (modifier.flags & 8 /* NodeFlags.Reparsed */) !== 0) {
|
|
1711
|
+
reusable = false;
|
|
1712
|
+
break;
|
|
1713
|
+
}
|
|
1714
|
+
if (reusable) {
|
|
1715
|
+
const kept = [];
|
|
1716
|
+
for (const modifier of modifiers.nodes)
|
|
1717
|
+
if (isModifier(modifier))
|
|
1718
|
+
kept.push(modifier);
|
|
1719
|
+
return this.factory.createModifierList(kept);
|
|
1720
|
+
}
|
|
1721
|
+
}
|
|
1722
|
+
return this.createModifiers(newFlags);
|
|
1723
|
+
}
|
|
1724
|
+
ensureModifierFlags(node) {
|
|
1725
|
+
// No `async` or `override` in declaration files, and `public` is implied.
|
|
1726
|
+
let mask = 131071 /* ModifierFlags.All */ ^ (1 /* ModifierFlags.Public */ | 1024 /* ModifierFlags.Async */ | 16 /* ModifierFlags.Override */);
|
|
1727
|
+
let additions = this.needsDeclare && node.kind !== 265 /* SyntaxKind.InterfaceDeclaration */ ? 128 /* ModifierFlags.Ambient */ : 0 /* ModifierFlags.None */;
|
|
1728
|
+
if (node.parent === undefined || node.parent.kind !== 307 /* SyntaxKind.SourceFile */) {
|
|
1729
|
+
mask ^= 128 /* ModifierFlags.Ambient */;
|
|
1730
|
+
additions = 0 /* ModifierFlags.None */;
|
|
1731
|
+
}
|
|
1732
|
+
if (isImplicitlyExportedJSDocDeclaration(node))
|
|
1733
|
+
additions |= 32 /* ModifierFlags.Export */;
|
|
1734
|
+
return maskModifierFlags(node, mask, additions);
|
|
1735
|
+
}
|
|
1736
|
+
ensureTypeParams(node, params) {
|
|
1737
|
+
if (this.isPrivate(this.parseNode(node)))
|
|
1738
|
+
return undefined;
|
|
1739
|
+
const typeParameters = this.visitNodes(params);
|
|
1740
|
+
if (typeParameters !== undefined || getFullSignature(node) === undefined)
|
|
1741
|
+
return typeParameters;
|
|
1742
|
+
// A JSDoc `@type` signature declares the type parameters.
|
|
1743
|
+
const tracker = this.tracker;
|
|
1744
|
+
const oldErrorNameNode = tracker.errorNameNode;
|
|
1745
|
+
tracker.errorNameNode = getName(node);
|
|
1746
|
+
const oldContext = tracker.diagnosticContext;
|
|
1747
|
+
if (!this.suppressNewDiagnosticContexts && canProduceDiagnostics(node))
|
|
1748
|
+
tracker.diagnosticContext = declarationContext(node);
|
|
1749
|
+
const nodes = this.resolver.createTypeParametersOfSignatureDeclaration(this.context, node, this.enclosingDeclaration, 531469 /* NodeBuilderFlags.DeclarationEmit */, tracker);
|
|
1750
|
+
tracker.errorNameNode = oldErrorNameNode;
|
|
1751
|
+
if (!this.suppressNewDiagnosticContexts)
|
|
1752
|
+
tracker.diagnosticContext = oldContext;
|
|
1753
|
+
return nodes === undefined ? undefined : this.factory.createNodeList(nodes);
|
|
1754
|
+
}
|
|
1755
|
+
updateParamList(node, params) {
|
|
1756
|
+
if (this.isPrivate(this.parseNode(node)) || params.nodes.length === 0)
|
|
1757
|
+
return this.factory.createNodeList([]);
|
|
1758
|
+
const results = [];
|
|
1759
|
+
for (const parameter of params.nodes)
|
|
1760
|
+
results.push(this.ensureParameterNode(parameter));
|
|
1761
|
+
return this.factory.createNodeList(results);
|
|
1762
|
+
}
|
|
1763
|
+
ensureParameterNode(node) {
|
|
1764
|
+
if (node === undefined)
|
|
1765
|
+
return node;
|
|
1766
|
+
return node instanceof ParameterDeclaration ? this.ensureParameter(node) : node;
|
|
1767
|
+
}
|
|
1768
|
+
ensureParameter(p) {
|
|
1769
|
+
const tracker = this.tracker;
|
|
1770
|
+
const oldContext = tracker.diagnosticContext;
|
|
1771
|
+
if (!this.suppressNewDiagnosticContexts)
|
|
1772
|
+
tracker.diagnosticContext = declarationContext(p);
|
|
1773
|
+
let questionToken = undefined;
|
|
1774
|
+
if (this.resolver.isOptionalParameter(p))
|
|
1775
|
+
questionToken = p.questionToken ?? this.factory.createToken(57 /* SyntaxKind.QuestionToken */);
|
|
1776
|
+
const result = this.factory.updateParameterDeclaration(p, undefined, p.dotDotDotToken, this.visitBindingName(p.name), questionToken, this.ensureType(p, true), this.ensureNoInitializer(p));
|
|
1777
|
+
tracker.diagnosticContext = oldContext;
|
|
1778
|
+
return result;
|
|
1779
|
+
}
|
|
1780
|
+
visitBindingName(node) {
|
|
1781
|
+
if (node instanceof BindingPattern) {
|
|
1782
|
+
const elements = [];
|
|
1783
|
+
let changed = false;
|
|
1784
|
+
for (const element of node.elements.nodes) {
|
|
1785
|
+
const visited = this.visitBindingName(element);
|
|
1786
|
+
if (visited !== element)
|
|
1787
|
+
changed = true;
|
|
1788
|
+
elements.push(visited);
|
|
1789
|
+
}
|
|
1790
|
+
return changed
|
|
1791
|
+
? this.factory.updateBindingPattern(node, new NodeList(node.elements.pos, node.elements.end, elements))
|
|
1792
|
+
: node;
|
|
1793
|
+
}
|
|
1794
|
+
if (node instanceof BindingElement) {
|
|
1795
|
+
const propertyName = node.propertyName;
|
|
1796
|
+
if (propertyName instanceof ComputedPropertyName && isEntityNameExpression(propertyName.expression, false))
|
|
1797
|
+
this.checkEntityNameVisibility(propertyName.expression, this.enclosingDeclaration);
|
|
1798
|
+
return this.factory.updateBindingElement(node, node.dotDotDotToken, propertyName, node.name === undefined ? undefined : this.visitBindingName(node.name), undefined);
|
|
1799
|
+
}
|
|
1800
|
+
return node;
|
|
1801
|
+
}
|
|
1802
|
+
/** The type of a declaration: its annotation, or the resolver's type; `any` if neither. */
|
|
1803
|
+
ensureType(node, ignorePrivate) {
|
|
1804
|
+
// Private declarations have no types, except private parameter properties' parameters.
|
|
1805
|
+
if (!ignorePrivate && this.isPrivate(this.parseNode(node)))
|
|
1806
|
+
return undefined;
|
|
1807
|
+
// Literal constants are written with their initializer instead.
|
|
1808
|
+
if (this.shouldPrintWithInitializer(node))
|
|
1809
|
+
return undefined;
|
|
1810
|
+
const type = getType(node);
|
|
1811
|
+
if (node.kind !== 278 /* SyntaxKind.ExportAssignment */ &&
|
|
1812
|
+
node.kind !== 209 /* SyntaxKind.BindingElement */ &&
|
|
1813
|
+
type !== undefined &&
|
|
1814
|
+
(node.kind !== 170 /* SyntaxKind.Parameter */ ||
|
|
1815
|
+
!this.resolver.requiresAddingImplicitUndefined(node, this.enclosingDeclaration))) {
|
|
1816
|
+
const file = this.tracker.currentSourceFile;
|
|
1817
|
+
if (file === undefined || !file.isJS())
|
|
1818
|
+
return this.visitNode(type);
|
|
1819
|
+
// JSDoc types can need rewriting, which the resolver does.
|
|
1820
|
+
const rewritten = this.resolver.tryJSTypeNodeToTypeNode(this.context, type, this.enclosingDeclaration, this.nodeBuilderFlags(), this.tracker);
|
|
1821
|
+
if (rewritten !== undefined)
|
|
1822
|
+
return rewritten;
|
|
1823
|
+
}
|
|
1824
|
+
const tracker = this.tracker;
|
|
1825
|
+
const oldErrorNameNode = tracker.errorNameNode;
|
|
1826
|
+
tracker.errorNameNode = getName(node);
|
|
1827
|
+
const oldContext = tracker.diagnosticContext;
|
|
1828
|
+
if (!this.suppressNewDiagnosticContexts && canProduceDiagnostics(node))
|
|
1829
|
+
tracker.diagnosticContext = declarationContext(node);
|
|
1830
|
+
let typeNode = undefined;
|
|
1831
|
+
if (hasInferredType(node))
|
|
1832
|
+
typeNode = this.resolver.createTypeOfDeclaration(this.context, node, this.enclosingDeclaration, this.nodeBuilderFlags(), tracker);
|
|
1833
|
+
else if (isFunctionLike(node))
|
|
1834
|
+
typeNode = this.resolver.createReturnTypeOfSignatureDeclaration(this.context, node, this.enclosingDeclaration, this.nodeBuilderFlags(), tracker);
|
|
1835
|
+
tracker.errorNameNode = oldErrorNameNode;
|
|
1836
|
+
if (!this.suppressNewDiagnosticContexts)
|
|
1837
|
+
tracker.diagnosticContext = oldContext;
|
|
1838
|
+
return typeNode ?? this.keyword(132 /* SyntaxKind.AnyKeyword */);
|
|
1839
|
+
}
|
|
1840
|
+
nodeBuilderFlags() {
|
|
1841
|
+
return this.inClassExpressionDeclaration
|
|
1842
|
+
? 531469 /* NodeBuilderFlags.DeclarationEmit */ & ~2048 /* NodeBuilderFlags.WriteClassExpressionAsTypeLiteral */
|
|
1843
|
+
: 531469 /* NodeBuilderFlags.DeclarationEmit */;
|
|
1844
|
+
}
|
|
1845
|
+
shouldPrintWithInitializer(node) {
|
|
1846
|
+
return (this.canHaveLiteralInitializer(node) &&
|
|
1847
|
+
getInitializer(node) !== undefined &&
|
|
1848
|
+
this.resolver.isLiteralConstDeclaration(this.context.mostOriginal(node)));
|
|
1849
|
+
}
|
|
1850
|
+
canHaveLiteralInitializer(node) {
|
|
1851
|
+
switch (node.kind) {
|
|
1852
|
+
case 173 /* SyntaxKind.PropertyDeclaration */:
|
|
1853
|
+
case 172 /* SyntaxKind.PropertySignature */:
|
|
1854
|
+
return !this.isPrivate(node);
|
|
1855
|
+
case 170 /* SyntaxKind.Parameter */:
|
|
1856
|
+
case 261 /* SyntaxKind.VariableDeclaration */:
|
|
1857
|
+
return true;
|
|
1858
|
+
}
|
|
1859
|
+
return false;
|
|
1860
|
+
}
|
|
1861
|
+
ensureNoInitializer(node) {
|
|
1862
|
+
if (!this.shouldPrintWithInitializer(node))
|
|
1863
|
+
return undefined;
|
|
1864
|
+
if (!isPrimitiveLiteralValue(skipParenthesesOnly(getInitializer(node)), true))
|
|
1865
|
+
this.tracker.reportInferenceFallback(node);
|
|
1866
|
+
return this.resolver.createLiteralConstValue(this.context, this.parseNode(node), this.tracker);
|
|
1867
|
+
}
|
|
1868
|
+
transformImportEquals(declaration) {
|
|
1869
|
+
if (!this.resolver.isDeclarationVisible(declaration))
|
|
1870
|
+
return undefined;
|
|
1871
|
+
const reference = declaration.moduleReference;
|
|
1872
|
+
if (reference instanceof ExternalModuleReference)
|
|
1873
|
+
return this.factory.updateImportEqualsDeclaration(declaration, declaration.modifiers, declaration.isTypeOnly, declaration.name, this.factory.updateExternalModuleReference(reference, this.rewriteModuleSpecifier(declaration, reference.expression)));
|
|
1874
|
+
const tracker = this.tracker;
|
|
1875
|
+
const oldContext = tracker.diagnosticContext;
|
|
1876
|
+
tracker.diagnosticContext = declarationContext(declaration);
|
|
1877
|
+
this.checkEntityNameVisibility(reference, this.enclosingDeclaration);
|
|
1878
|
+
tracker.diagnosticContext = oldContext;
|
|
1879
|
+
return declaration;
|
|
1880
|
+
}
|
|
1881
|
+
transformImportDeclaration(declaration) {
|
|
1882
|
+
const clause = declaration.importClause;
|
|
1883
|
+
// `import "mod"` may be needed for its side effects, like global augmentations.
|
|
1884
|
+
if (clause === undefined)
|
|
1885
|
+
return this.updateImport(declaration, undefined);
|
|
1886
|
+
const phaseModifier = clause.phaseModifier === 166 /* SyntaxKind.DeferKeyword */ ? undefined : clause.phaseModifier;
|
|
1887
|
+
// The import clause's visibility is the default binding's.
|
|
1888
|
+
const defaultBinding = clause.name !== undefined && this.resolver.isDeclarationVisible(clause) ? clause.name : undefined;
|
|
1889
|
+
const bindings = clause.namedBindings;
|
|
1890
|
+
if (bindings === undefined) {
|
|
1891
|
+
if (defaultBinding === undefined)
|
|
1892
|
+
return undefined;
|
|
1893
|
+
return this.updateImport(declaration, this.factory.updateImportClause(clause, phaseModifier, defaultBinding, undefined));
|
|
1894
|
+
}
|
|
1895
|
+
if (bindings instanceof NamespaceImport) {
|
|
1896
|
+
const namespace = this.resolver.isDeclarationVisible(bindings) ? bindings : undefined;
|
|
1897
|
+
if (defaultBinding === undefined && namespace === undefined)
|
|
1898
|
+
return undefined;
|
|
1899
|
+
return this.updateImport(declaration, this.factory.updateImportClause(clause, phaseModifier, defaultBinding, namespace));
|
|
1900
|
+
}
|
|
1901
|
+
const visibleBindings = [];
|
|
1902
|
+
if (bindings instanceof NamedImports)
|
|
1903
|
+
for (const element of bindings.elements.nodes)
|
|
1904
|
+
if (this.resolver.isDeclarationVisible(element))
|
|
1905
|
+
visibleBindings.push(element);
|
|
1906
|
+
if (visibleBindings.length > 0 || defaultBinding !== undefined) {
|
|
1907
|
+
const namedImports = visibleBindings.length > 0 && bindings instanceof NamedImports
|
|
1908
|
+
? this.factory.updateNamedImports(bindings, this.factory.createNodeList(visibleBindings))
|
|
1909
|
+
: undefined;
|
|
1910
|
+
return this.updateImport(declaration, this.factory.updateImportClause(clause, phaseModifier, defaultBinding, namedImports));
|
|
1911
|
+
}
|
|
1912
|
+
// A module augmentation of an export may depend on the import.
|
|
1913
|
+
if (this.resolver.isImportRequiredByAugmentation(declaration)) {
|
|
1914
|
+
if (this.tracker.isolatedDeclarations)
|
|
1915
|
+
this.tracker.addDiagnostic(createDiagnosticForNode(declaration, 1790 /* Diagnostics.Declaration_emit_for_this_file_requires_preserving_this_import_for_augmentations_This_is_not_supported_with_isolatedDeclarations */, []));
|
|
1916
|
+
return this.updateImport(declaration, undefined);
|
|
1917
|
+
}
|
|
1918
|
+
return undefined;
|
|
1919
|
+
}
|
|
1920
|
+
updateImport(declaration, clause) {
|
|
1921
|
+
return this.factory.updateImportDeclaration(declaration, declaration.modifiers, clause, this.rewriteModuleSpecifier(declaration, declaration.moduleSpecifier), this.tryGetResolutionModeOverride(declaration.attributes));
|
|
1922
|
+
}
|
|
1923
|
+
// Visibility helpers
|
|
1924
|
+
parseNode(node) {
|
|
1925
|
+
return this.context.parseNode(node) ?? node;
|
|
1926
|
+
}
|
|
1927
|
+
isPrivate(node) {
|
|
1928
|
+
return this.resolver.getEffectiveDeclarationFlags(node, 2 /* ModifierFlags.Private */) !== 0;
|
|
1929
|
+
}
|
|
1930
|
+
isDeclarationAndNotVisible(node) {
|
|
1931
|
+
const parseNode = this.parseNode(node);
|
|
1932
|
+
switch (parseNode.kind) {
|
|
1933
|
+
case 263 /* SyntaxKind.FunctionDeclaration */:
|
|
1934
|
+
case 268 /* SyntaxKind.ModuleDeclaration */:
|
|
1935
|
+
case 265 /* SyntaxKind.InterfaceDeclaration */:
|
|
1936
|
+
case 264 /* SyntaxKind.ClassDeclaration */:
|
|
1937
|
+
case 266 /* SyntaxKind.TypeAliasDeclaration */:
|
|
1938
|
+
case 345 /* SyntaxKind.JSTypeAliasDeclaration */:
|
|
1939
|
+
case 267 /* SyntaxKind.EnumDeclaration */:
|
|
1940
|
+
return !this.resolver.isDeclarationVisible(parseNode);
|
|
1941
|
+
// These filter their members' visibility themselves.
|
|
1942
|
+
case 261 /* SyntaxKind.VariableDeclaration */:
|
|
1943
|
+
return !this.getBindingNameVisible(parseNode);
|
|
1944
|
+
case 176 /* SyntaxKind.ClassStaticBlockDeclaration */:
|
|
1945
|
+
return true;
|
|
1946
|
+
}
|
|
1947
|
+
return false;
|
|
1948
|
+
}
|
|
1949
|
+
getBindingNameVisible(element) {
|
|
1950
|
+
if (element.kind === 233 /* SyntaxKind.OmittedExpression */)
|
|
1951
|
+
return false;
|
|
1952
|
+
const name = getName(element);
|
|
1953
|
+
if (name === undefined)
|
|
1954
|
+
return false;
|
|
1955
|
+
if (name instanceof BindingPattern) {
|
|
1956
|
+
// Visible if any element was marked visible, as by referenced aliases.
|
|
1957
|
+
for (const child of name.elements.nodes)
|
|
1958
|
+
if (this.getBindingNameVisible(child))
|
|
1959
|
+
return true;
|
|
1960
|
+
return false;
|
|
1961
|
+
}
|
|
1962
|
+
return this.resolver.isDeclarationVisible(element);
|
|
1963
|
+
}
|
|
1964
|
+
shouldStripInternal(node) {
|
|
1965
|
+
const file = this.tracker.currentSourceFile;
|
|
1966
|
+
return this.tracker.stripInternal && file !== undefined && this.isInternalDeclaration(node, file);
|
|
1967
|
+
}
|
|
1968
|
+
/** Whether a declaration has an `@internal` comment. */
|
|
1969
|
+
isInternalDeclaration(node, file) {
|
|
1970
|
+
const parseTreeNode = this.context.mostOriginal(node);
|
|
1971
|
+
if ((parseTreeNode.flags & 16 /* NodeFlags.Synthesized */) !== 0)
|
|
1972
|
+
return false;
|
|
1973
|
+
const text = file.text;
|
|
1974
|
+
if (parseTreeNode instanceof ParameterDeclaration) {
|
|
1975
|
+
// The comment can precede the parameter or follow the previous one's comma:
|
|
1976
|
+
// `a, /** @internal */ public b: string`.
|
|
1977
|
+
const parameters = getParameters(parseTreeNode.parent).nodes;
|
|
1978
|
+
const index = parameters.indexOf(parseTreeNode);
|
|
1979
|
+
let ranges;
|
|
1980
|
+
if (index > 0) {
|
|
1981
|
+
const trailingPos = skipTrivia(text, parameters[index - 1].end + 1, false, true);
|
|
1982
|
+
ranges = commentRanges(getTrailingCommentRanges(text, trailingPos)).concat(commentRanges(getLeadingCommentRanges(text, node.pos)));
|
|
1983
|
+
}
|
|
1984
|
+
else
|
|
1985
|
+
ranges = commentRanges(getTrailingCommentRanges(text, skipTrivia(text, node.pos, false, true)));
|
|
1986
|
+
return ranges.length > 0 && hasInternalAnnotation(text, ranges[ranges.length - 1]);
|
|
1987
|
+
}
|
|
1988
|
+
if (parseTreeNode.kind === 11 /* SyntaxKind.JsxText */)
|
|
1989
|
+
return false;
|
|
1990
|
+
for (const range of commentRanges(getLeadingCommentRanges(text, parseTreeNode.pos)))
|
|
1991
|
+
if (hasInternalAnnotation(text, range))
|
|
1992
|
+
return true;
|
|
1993
|
+
return false;
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
function commentRanges(ranges) {
|
|
1997
|
+
return ranges ?? [];
|
|
1998
|
+
}
|
|
1999
|
+
function hasInternalAnnotation(text, range) {
|
|
2000
|
+
return text.substring(range.pos, range.end).indexOf('@internal') >= 0;
|
|
2001
|
+
}
|
|
2002
|
+
class DiagnosticScope {
|
|
2003
|
+
context;
|
|
2004
|
+
errorNameNode;
|
|
2005
|
+
suppressNewDiagnosticContexts;
|
|
2006
|
+
constructor(context, errorNameNode, suppressNewDiagnosticContexts) {
|
|
2007
|
+
this.context = context;
|
|
2008
|
+
this.errorNameNode = errorNameNode;
|
|
2009
|
+
this.suppressNewDiagnosticContexts = suppressNewDiagnosticContexts;
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
const modifierKindsInOrder = [
|
|
2013
|
+
94 /* SyntaxKind.ExportKeyword */,
|
|
2014
|
+
137 /* SyntaxKind.DeclareKeyword */,
|
|
2015
|
+
89 /* SyntaxKind.DefaultKeyword */,
|
|
2016
|
+
86 /* SyntaxKind.ConstKeyword */,
|
|
2017
|
+
124 /* SyntaxKind.PublicKeyword */,
|
|
2018
|
+
122 /* SyntaxKind.PrivateKeyword */,
|
|
2019
|
+
123 /* SyntaxKind.ProtectedKeyword */,
|
|
2020
|
+
127 /* SyntaxKind.AbstractKeyword */,
|
|
2021
|
+
125 /* SyntaxKind.StaticKeyword */,
|
|
2022
|
+
164 /* SyntaxKind.OverrideKeyword */,
|
|
2023
|
+
148 /* SyntaxKind.ReadonlyKeyword */,
|
|
2024
|
+
128 /* SyntaxKind.AccessorKeyword */,
|
|
2025
|
+
133 /* SyntaxKind.AsyncKeyword */,
|
|
2026
|
+
102 /* SyntaxKind.InKeyword */,
|
|
2027
|
+
147 /* SyntaxKind.OutKeyword */,
|
|
2028
|
+
];
|
|
2029
|
+
function preservedReferences(references) {
|
|
2030
|
+
const result = [];
|
|
2031
|
+
for (const reference of references)
|
|
2032
|
+
if (reference.preserve)
|
|
2033
|
+
result.push(new FileReference(-1, -1, reference.fileName, reference.resolutionMode, reference.preserve));
|
|
2034
|
+
return result;
|
|
2035
|
+
}
|
|
2036
|
+
/** Whether a node is a declaration (type parameters only when attached). */
|
|
2037
|
+
function isDeclaration(node) {
|
|
2038
|
+
if (node.kind === 169 /* SyntaxKind.TypeParameter */)
|
|
2039
|
+
return node.parent !== undefined;
|
|
2040
|
+
return isDeclarationNode(node);
|
|
2041
|
+
}
|
|
2042
|
+
function isEntityName(node) {
|
|
2043
|
+
return node.kind === 79 /* SyntaxKind.Identifier */ || node.kind === 167 /* SyntaxKind.QualifiedName */;
|
|
2044
|
+
}
|
|
2045
|
+
function isThisParameterNode(node) {
|
|
2046
|
+
return node instanceof ParameterDeclaration && node.name instanceof Identifier && node.name.text === 'this';
|
|
2047
|
+
}
|
|
2048
|
+
/** Declarations whose scope names in their types are resolved from. */
|
|
2049
|
+
function isEnclosingDeclaration(node) {
|
|
2050
|
+
switch (node.kind) {
|
|
2051
|
+
case 307 /* SyntaxKind.SourceFile */:
|
|
2052
|
+
case 266 /* SyntaxKind.TypeAliasDeclaration */:
|
|
2053
|
+
case 345 /* SyntaxKind.JSTypeAliasDeclaration */:
|
|
2054
|
+
case 268 /* SyntaxKind.ModuleDeclaration */:
|
|
2055
|
+
case 264 /* SyntaxKind.ClassDeclaration */:
|
|
2056
|
+
case 265 /* SyntaxKind.InterfaceDeclaration */:
|
|
2057
|
+
case 182 /* SyntaxKind.IndexSignature */:
|
|
2058
|
+
case 201 /* SyntaxKind.MappedType */:
|
|
2059
|
+
case 261 /* SyntaxKind.VariableDeclaration */:
|
|
2060
|
+
return true;
|
|
2061
|
+
}
|
|
2062
|
+
return isFunctionLike(node);
|
|
2063
|
+
}
|
|
2064
|
+
function isLateVisibilityPaintedStatement(node) {
|
|
2065
|
+
switch (node.kind) {
|
|
2066
|
+
case 273 /* SyntaxKind.ImportDeclaration */:
|
|
2067
|
+
case 346 /* SyntaxKind.JSImportDeclaration */:
|
|
2068
|
+
case 272 /* SyntaxKind.ImportEqualsDeclaration */:
|
|
2069
|
+
case 244 /* SyntaxKind.VariableStatement */:
|
|
2070
|
+
case 264 /* SyntaxKind.ClassDeclaration */:
|
|
2071
|
+
case 263 /* SyntaxKind.FunctionDeclaration */:
|
|
2072
|
+
case 268 /* SyntaxKind.ModuleDeclaration */:
|
|
2073
|
+
case 266 /* SyntaxKind.TypeAliasDeclaration */:
|
|
2074
|
+
case 345 /* SyntaxKind.JSTypeAliasDeclaration */:
|
|
2075
|
+
case 265 /* SyntaxKind.InterfaceDeclaration */:
|
|
2076
|
+
case 267 /* SyntaxKind.EnumDeclaration */:
|
|
2077
|
+
return true;
|
|
2078
|
+
}
|
|
2079
|
+
return false;
|
|
2080
|
+
}
|
|
2081
|
+
function isExternalModuleIndicator(node) {
|
|
2082
|
+
return (isAnyImportOrReExport(node) ||
|
|
2083
|
+
node.kind === 278 /* SyntaxKind.ExportAssignment */ ||
|
|
2084
|
+
hasSyntacticModifier(node, 32 /* ModifierFlags.Export */));
|
|
2085
|
+
}
|
|
2086
|
+
/** Whether a statement in a namespace without `export {}` would stop being exported implicitly. */
|
|
2087
|
+
function needsScopeMarker(node) {
|
|
2088
|
+
return (!isAnyImportOrReExport(node) &&
|
|
2089
|
+
node.kind !== 278 /* SyntaxKind.ExportAssignment */ &&
|
|
2090
|
+
!hasSyntacticModifier(node, 32 /* ModifierFlags.Export */) &&
|
|
2091
|
+
!isAmbientModule(node));
|
|
2092
|
+
}
|
|
2093
|
+
function hasScopeMarker(statements) {
|
|
2094
|
+
for (const statement of statements.nodes)
|
|
2095
|
+
if (statement.kind === 278 /* SyntaxKind.ExportAssignment */ || statement.kind === 279 /* SyntaxKind.ExportDeclaration */)
|
|
2096
|
+
return true;
|
|
2097
|
+
return false;
|
|
2098
|
+
}
|
|
2099
|
+
function maskModifierFlags(node, mask, additions) {
|
|
2100
|
+
let flags = (getCombinedModifierFlags(node) & mask) | additions;
|
|
2101
|
+
// A default export keeps `export` even where exports are stripped, and never has `declare`.
|
|
2102
|
+
if ((flags & 2048 /* ModifierFlags.Default */) !== 0 && (flags & 32 /* ModifierFlags.Export */) === 0)
|
|
2103
|
+
flags ^= 32 /* ModifierFlags.Export */;
|
|
2104
|
+
if ((flags & 2048 /* ModifierFlags.Default */) !== 0 && (flags & 128 /* ModifierFlags.Ambient */) !== 0)
|
|
2105
|
+
flags ^= 128 /* ModifierFlags.Ambient */;
|
|
2106
|
+
return flags;
|
|
2107
|
+
}
|
|
2108
|
+
function isImplicitlyExportedJSDocDeclaration(node) {
|
|
2109
|
+
const parent = node.parent;
|
|
2110
|
+
if (parent === undefined || parent.kind !== 307 /* SyntaxKind.SourceFile */)
|
|
2111
|
+
return false;
|
|
2112
|
+
if (!isExternalOrCommonJSModule(parent))
|
|
2113
|
+
return false;
|
|
2114
|
+
if (node.kind === 345 /* SyntaxKind.JSTypeAliasDeclaration */)
|
|
2115
|
+
return true;
|
|
2116
|
+
return node.kind === 268 /* SyntaxKind.ModuleDeclaration */ && (node.flags & 8 /* NodeFlags.Reparsed */) !== 0;
|
|
2117
|
+
}
|
|
2118
|
+
function canHaveModifiers(node) {
|
|
2119
|
+
switch (node.kind) {
|
|
2120
|
+
case 244 /* SyntaxKind.VariableStatement */:
|
|
2121
|
+
case 263 /* SyntaxKind.FunctionDeclaration */:
|
|
2122
|
+
case 264 /* SyntaxKind.ClassDeclaration */:
|
|
2123
|
+
case 265 /* SyntaxKind.InterfaceDeclaration */:
|
|
2124
|
+
case 266 /* SyntaxKind.TypeAliasDeclaration */:
|
|
2125
|
+
case 267 /* SyntaxKind.EnumDeclaration */:
|
|
2126
|
+
case 268 /* SyntaxKind.ModuleDeclaration */:
|
|
2127
|
+
case 272 /* SyntaxKind.ImportEqualsDeclaration */:
|
|
2128
|
+
case 273 /* SyntaxKind.ImportDeclaration */:
|
|
2129
|
+
case 346 /* SyntaxKind.JSImportDeclaration */:
|
|
2130
|
+
case 278 /* SyntaxKind.ExportAssignment */:
|
|
2131
|
+
case 279 /* SyntaxKind.ExportDeclaration */:
|
|
2132
|
+
return true;
|
|
2133
|
+
}
|
|
2134
|
+
return false;
|
|
2135
|
+
}
|
|
2136
|
+
/** Kinds whose type the resolver writes from the declaration rather than a signature. */
|
|
2137
|
+
function hasInferredType(node) {
|
|
2138
|
+
switch (node.kind) {
|
|
2139
|
+
case 170 /* SyntaxKind.Parameter */:
|
|
2140
|
+
case 172 /* SyntaxKind.PropertySignature */:
|
|
2141
|
+
case 173 /* SyntaxKind.PropertyDeclaration */:
|
|
2142
|
+
case 209 /* SyntaxKind.BindingElement */:
|
|
2143
|
+
case 212 /* SyntaxKind.PropertyAccessExpression */:
|
|
2144
|
+
case 213 /* SyntaxKind.ElementAccessExpression */:
|
|
2145
|
+
case 227 /* SyntaxKind.BinaryExpression */:
|
|
2146
|
+
case 214 /* SyntaxKind.CallExpression */:
|
|
2147
|
+
case 261 /* SyntaxKind.VariableDeclaration */:
|
|
2148
|
+
case 278 /* SyntaxKind.ExportAssignment */:
|
|
2149
|
+
case 303 /* SyntaxKind.PropertyAssignment */:
|
|
2150
|
+
case 304 /* SyntaxKind.ShorthandPropertyAssignment */:
|
|
2151
|
+
case 333 /* SyntaxKind.JSDocParameterTag */:
|
|
2152
|
+
case 340 /* SyntaxKind.JSDocPropertyTag */:
|
|
2153
|
+
return true;
|
|
2154
|
+
}
|
|
2155
|
+
return false;
|
|
2156
|
+
}
|
|
2157
|
+
/** A literal that can be an initializer in a declaration file: a string, number, bigint or boolean. */
|
|
2158
|
+
function isPrimitiveLiteralValue(node, includeBigInt) {
|
|
2159
|
+
switch (node.kind) {
|
|
2160
|
+
case 111 /* SyntaxKind.TrueKeyword */:
|
|
2161
|
+
case 96 /* SyntaxKind.FalseKeyword */:
|
|
2162
|
+
case 8 /* SyntaxKind.NumericLiteral */:
|
|
2163
|
+
case 10 /* SyntaxKind.StringLiteral */:
|
|
2164
|
+
case 14 /* SyntaxKind.NoSubstitutionTemplateLiteral */:
|
|
2165
|
+
return true;
|
|
2166
|
+
case 9 /* SyntaxKind.BigIntLiteral */:
|
|
2167
|
+
return includeBigInt;
|
|
2168
|
+
case 225 /* SyntaxKind.PrefixUnaryExpression */: {
|
|
2169
|
+
const unary = node;
|
|
2170
|
+
const operand = unary.operand;
|
|
2171
|
+
if (unary.operator === 40 /* SyntaxKind.MinusToken */)
|
|
2172
|
+
return operand instanceof NumericLiteral || (includeBigInt && operand.kind === 9 /* SyntaxKind.BigIntLiteral */);
|
|
2173
|
+
if (unary.operator === 39 /* SyntaxKind.PlusToken */)
|
|
2174
|
+
return operand instanceof NumericLiteral;
|
|
2175
|
+
return false;
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
2178
|
+
return false;
|
|
2179
|
+
}
|
|
2180
|
+
function skipParenthesesOnly(node) {
|
|
2181
|
+
let current = node;
|
|
2182
|
+
while (current.kind === 218 /* SyntaxKind.ParenthesizedExpression */)
|
|
2183
|
+
current = getExpression(current);
|
|
2184
|
+
return current;
|
|
2185
|
+
}
|
|
2186
|
+
/** Skips what passes an expression's type through: parentheses, assignments and commas. */
|
|
2187
|
+
function skipOuterExpressions(node) {
|
|
2188
|
+
let current = node;
|
|
2189
|
+
for (;;) {
|
|
2190
|
+
if (current.kind === 218 /* SyntaxKind.ParenthesizedExpression */)
|
|
2191
|
+
current = getExpression(current);
|
|
2192
|
+
else if (current instanceof BinaryExpression &&
|
|
2193
|
+
(current.operatorToken.kind === 63 /* SyntaxKind.EqualsToken */ || current.operatorToken.kind === 27 /* SyntaxKind.CommaToken */))
|
|
2194
|
+
current = current.right;
|
|
2195
|
+
else
|
|
2196
|
+
return current;
|
|
2197
|
+
}
|
|
2198
|
+
}
|
|
2199
|
+
function getFirstConstructorWithBody(members) {
|
|
2200
|
+
for (const member of members.nodes)
|
|
2201
|
+
if (member !== undefined && member instanceof ConstructorDeclaration && nodeIsPresent(member.body))
|
|
2202
|
+
return member;
|
|
2203
|
+
return undefined;
|
|
2204
|
+
}
|
|
2205
|
+
function getClassExtendsHeritageElement(node) {
|
|
2206
|
+
const clauses = getHeritageClauses(node);
|
|
2207
|
+
if (clauses === undefined)
|
|
2208
|
+
return undefined;
|
|
2209
|
+
for (const clause of clauses.nodes)
|
|
2210
|
+
if (clause instanceof HeritageClause && clause.token === 95 /* SyntaxKind.ExtendsKeyword */) {
|
|
2211
|
+
const first = clause.types.nodes.length > 0 ? clause.types.nodes[0] : undefined;
|
|
2212
|
+
return first !== undefined && first instanceof ExpressionWithTypeArguments ? first : undefined;
|
|
2213
|
+
}
|
|
2214
|
+
return undefined;
|
|
2215
|
+
}
|
|
2216
|
+
/** Whether the original of `node` is on one line of its source file. */
|
|
2217
|
+
function isOriginalNodeSingleLine(context, node) {
|
|
2218
|
+
const original = context.mostOriginal(node);
|
|
2219
|
+
if ((original.flags & 16 /* NodeFlags.Synthesized */) !== 0 || original.pos < 0)
|
|
2220
|
+
return false;
|
|
2221
|
+
const text = getSourceFileOfNode(original).text;
|
|
2222
|
+
for (let i = original.pos; i < original.end; i++) {
|
|
2223
|
+
const ch = text.charCodeAt(i);
|
|
2224
|
+
if (ch === 10 || ch === 13)
|
|
2225
|
+
return false;
|
|
2226
|
+
}
|
|
2227
|
+
return true;
|
|
2228
|
+
}
|
|
2229
|
+
/** A node known to be present, checked where its type allows `undefined` at runtime. */
|
|
2230
|
+
function defined(node) {
|
|
2231
|
+
if (node === undefined)
|
|
2232
|
+
throw new Error('Expected a node');
|
|
2233
|
+
return node;
|
|
2234
|
+
}
|
|
2235
|
+
function asteriskOf(fn) {
|
|
2236
|
+
if (fn !== undefined && fn instanceof FunctionExpression)
|
|
2237
|
+
return fn.asteriskToken;
|
|
2238
|
+
return undefined;
|
|
2239
|
+
}
|
|
2240
|
+
/** `const f = function () {}` or `const f = () => {}`. */
|
|
2241
|
+
function isFunctionValuedVariable(node) {
|
|
2242
|
+
if (node.kind !== 261 /* SyntaxKind.VariableDeclaration */)
|
|
2243
|
+
return false;
|
|
2244
|
+
const initializer = getInitializer(node);
|
|
2245
|
+
return (initializer !== undefined &&
|
|
2246
|
+
(initializer.kind === 219 /* SyntaxKind.FunctionExpression */ || initializer.kind === 220 /* SyntaxKind.ArrowFunction */));
|
|
2247
|
+
}
|
|
2248
|
+
function appendFlattened(result, node) {
|
|
2249
|
+
if (node instanceof SyntaxList)
|
|
2250
|
+
for (const child of node.children)
|
|
2251
|
+
result.push(child);
|
|
2252
|
+
else
|
|
2253
|
+
result.push(node);
|
|
2254
|
+
return true;
|
|
2255
|
+
}
|
|
2256
|
+
/** The JSDoc `@type` signature of a function-like declaration. */
|
|
2257
|
+
function getFullSignature(node) {
|
|
2258
|
+
if (node instanceof FunctionExpression)
|
|
2259
|
+
return node.fullSignature;
|
|
2260
|
+
if (node instanceof ArrowFunction)
|
|
2261
|
+
return node.fullSignature;
|
|
2262
|
+
if (node instanceof FunctionDeclaration)
|
|
2263
|
+
return node.fullSignature;
|
|
2264
|
+
if (node instanceof MethodDeclaration)
|
|
2265
|
+
return node.fullSignature;
|
|
2266
|
+
if (node instanceof ConstructorDeclaration)
|
|
2267
|
+
return node.fullSignature;
|
|
2268
|
+
if (node instanceof GetAccessorDeclaration)
|
|
2269
|
+
return node.fullSignature;
|
|
2270
|
+
if (node instanceof SetAccessorDeclaration)
|
|
2271
|
+
return node.fullSignature;
|
|
2272
|
+
return undefined;
|
|
2273
|
+
}
|
|
2274
|
+
function isCommonJSAliasExport(node) {
|
|
2275
|
+
if (!(node instanceof BinaryExpression) || !(node.right instanceof Identifier))
|
|
2276
|
+
return false;
|
|
2277
|
+
const symbol = node.symbol;
|
|
2278
|
+
return symbol !== undefined && symbol.declarations.length === 1;
|
|
2279
|
+
}
|
|
2280
|
+
function getLeftmostAccessExpression(node) {
|
|
2281
|
+
let current = node;
|
|
2282
|
+
while (current.kind === 212 /* SyntaxKind.PropertyAccessExpression */ || current.kind === 213 /* SyntaxKind.ElementAccessExpression */)
|
|
2283
|
+
current = getExpression(current);
|
|
2284
|
+
return current;
|
|
2285
|
+
}
|
|
2286
|
+
/** Whether properties assigned to a function are emitted: one of its declarations has a body. */
|
|
2287
|
+
function shouldEmitFunctionProperties(fn) {
|
|
2288
|
+
if (fn.body !== undefined)
|
|
2289
|
+
return true;
|
|
2290
|
+
const symbol = fn.symbol;
|
|
2291
|
+
if (symbol === undefined)
|
|
2292
|
+
return false;
|
|
2293
|
+
for (const declaration of symbol.declarations)
|
|
2294
|
+
if (declaration instanceof FunctionDeclaration && declaration.body !== undefined)
|
|
2295
|
+
return true;
|
|
2296
|
+
return false;
|
|
2297
|
+
}
|
|
2298
|
+
function isNonContextualKeyword(kind) {
|
|
2299
|
+
return (kind >= 82 /* SyntaxKind.FirstKeyword */ &&
|
|
2300
|
+
kind <= 166 /* SyntaxKind.LastKeyword */ &&
|
|
2301
|
+
!(kind >= 127 /* SyntaxKind.FirstContextualKeyword */ && kind <= 166 /* SyntaxKind.LastContextualKeyword */));
|
|
2302
|
+
}
|
|
2303
|
+
function isClassExtendingNull(heritage) {
|
|
2304
|
+
if (heritage.nodes.length !== 1)
|
|
2305
|
+
return false;
|
|
2306
|
+
for (const type of heritage.nodes[0].types.nodes)
|
|
2307
|
+
if (getExpression(type).kind === 105 /* SyntaxKind.NullKeyword */)
|
|
2308
|
+
return true;
|
|
2309
|
+
return false;
|
|
2310
|
+
}
|
|
2311
|
+
function getTextOfPropertyName(name) {
|
|
2312
|
+
switch (name.kind) {
|
|
2313
|
+
case 79 /* SyntaxKind.Identifier */:
|
|
2314
|
+
case 80 /* SyntaxKind.PrivateIdentifier */:
|
|
2315
|
+
case 10 /* SyntaxKind.StringLiteral */:
|
|
2316
|
+
case 8 /* SyntaxKind.NumericLiteral */:
|
|
2317
|
+
case 14 /* SyntaxKind.NoSubstitutionTemplateLiteral */:
|
|
2318
|
+
return getText(name);
|
|
2319
|
+
}
|
|
2320
|
+
return '';
|
|
2321
|
+
}
|
|
2322
|
+
/** The key of a class member for deduplicating `this.x` members: name, staticness and privateness. */
|
|
2323
|
+
function thisPropertyKey(name, node, isStaticMember) {
|
|
2324
|
+
const prefix = (isStaticMember ? 's' : 'i') + (name instanceof PrivateIdentifier ? '#' : '.');
|
|
2325
|
+
if (!(name instanceof ComputedPropertyName) && name.kind !== 213 /* SyntaxKind.ElementAccessExpression */) {
|
|
2326
|
+
const text = getTextOfPropertyName(name);
|
|
2327
|
+
if (text !== '' || name instanceof StringLiteral)
|
|
2328
|
+
return prefix + text;
|
|
2329
|
+
}
|
|
2330
|
+
return prefix + '@' + node.pos + ':' + node.end;
|
|
2331
|
+
}
|
|
2332
|
+
/** Walks a whole file for `module.exports =`, assigned properties and CommonJS exports. */
|
|
2333
|
+
class ExpressionWalker extends NodeVisitor {
|
|
2334
|
+
transformer;
|
|
2335
|
+
kind;
|
|
2336
|
+
constructor(transformer, kind) {
|
|
2337
|
+
super();
|
|
2338
|
+
this.transformer = transformer;
|
|
2339
|
+
this.kind = kind;
|
|
2340
|
+
}
|
|
2341
|
+
visitNode(node) {
|
|
2342
|
+
// Only assignments and `Object.defineProperty` calls declare anything.
|
|
2343
|
+
if (node.kind === 227 /* SyntaxKind.BinaryExpression */ || node.kind === 214 /* SyntaxKind.CallExpression */) {
|
|
2344
|
+
if (this.kind === 0 /* ExpressionWalkerKind.ModuleExports */)
|
|
2345
|
+
this.transformer.visitModuleExportsAssignment(node);
|
|
2346
|
+
else
|
|
2347
|
+
this.transformer.visitNestedExpression(node);
|
|
2348
|
+
}
|
|
2349
|
+
return node.forEachChild(this);
|
|
2350
|
+
}
|
|
2351
|
+
}
|
|
2352
|
+
/** Walks a class member for `this.x = …` assignments. */
|
|
2353
|
+
class ThisPropertyWalker extends NodeVisitor {
|
|
2354
|
+
transformer;
|
|
2355
|
+
classNode;
|
|
2356
|
+
constructor(transformer, classNode) {
|
|
2357
|
+
super();
|
|
2358
|
+
this.transformer = transformer;
|
|
2359
|
+
this.classNode = classNode;
|
|
2360
|
+
}
|
|
2361
|
+
visitNode(node) {
|
|
2362
|
+
this.transformer.visitThisPropertyAssignment(node, this.classNode);
|
|
2363
|
+
return node.forEachChild(this);
|
|
2364
|
+
}
|
|
2365
|
+
}
|
|
2366
|
+
/** The text of a JSDoc comment: its text and links, without trailing whitespace. */
|
|
2367
|
+
function getTextOfJSDocComment(comment) {
|
|
2368
|
+
if (comment === undefined)
|
|
2369
|
+
return '';
|
|
2370
|
+
let text = '';
|
|
2371
|
+
for (const n of comment.nodes) {
|
|
2372
|
+
if (n instanceof JSDocText)
|
|
2373
|
+
text += n.text;
|
|
2374
|
+
else if (n.kind === 319 /* SyntaxKind.JSDocLink */ ||
|
|
2375
|
+
n.kind === 320 /* SyntaxKind.JSDocLinkCode */ ||
|
|
2376
|
+
n.kind === 321 /* SyntaxKind.JSDocLinkPlain */)
|
|
2377
|
+
text += getSourceTextOfNode(getSourceFileOfNode(n), n);
|
|
2378
|
+
}
|
|
2379
|
+
return text.trimEnd();
|
|
2380
|
+
}
|