Skip to content

Add declaration transform stage that combines repeated import type nodes into single import declarations #49730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6268,12 +6268,16 @@ namespace ts {
const lastId = isIdentifier(nonRootParts) ? nonRootParts : nonRootParts.right;
lastId.typeArguments = undefined;
}
return factory.createImportTypeNode(lit, assertion, nonRootParts as EntityName, typeParameterNodes as readonly TypeNode[], isTypeOf);
const node = factory.createImportTypeNode(lit, assertion, nonRootParts as EntityName, typeParameterNodes as readonly TypeNode[], isTypeOf);
node.nextContainer = context.enclosingDeclaration; // `nextContainer` is used in the binder to track containers - reuse it on import types to smuggle out original context info
return node;
}
else {
const splitNode = getTopmostIndexedAccessType(nonRootParts);
const qualifier = (splitNode.objectType as TypeReferenceNode).typeName;
return factory.createIndexedAccessTypeNode(factory.createImportTypeNode(lit, assertion, qualifier, typeParameterNodes as readonly TypeNode[], isTypeOf), splitNode.indexType);
const node = factory.createImportTypeNode(lit, assertion, qualifier, typeParameterNodes as readonly TypeNode[], isTypeOf);
node.nextContainer = context.enclosingDeclaration; // `nextContainer` is used in the binder to track containers - reuse it on import types to smuggle out original context info
return factory.createIndexedAccessTypeNode(node, splitNode.indexType);
}
}

Expand Down Expand Up @@ -43273,6 +43277,9 @@ namespace ts {
createLiteralConstValue,
isSymbolAccessible,
isEntityNameVisible,
resolveName(name, location, meaning, excludeGlobals) {
return resolveName(getParseTreeNode(location), escapeLeadingUnderscores(name), meaning, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ false, excludeGlobals);
},
getConstantValue: nodeIn => {
const node = getParseTreeNode(nodeIn, canHaveConstantValue);
return node ? getConstantValue(node) : undefined;
Expand Down
10 changes: 10 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,16 @@ namespace ts {
defaultValueDescription: false,
description: Diagnostics.Create_sourcemaps_for_d_ts_files
},
{
name: "prettyDeclaration",
type: "boolean",
affectsEmit: true,
affectsMultiFileEmitBuildInfo: true,
category: Diagnostics.Emit,
transpileOptionValue: undefined,
description: Diagnostics.Enable_extra_transform_stages_that_make_declaration_emit_output_prettier,
defaultValueDescription: Diagnostics.true_if_declaration_is_true_false_otherwise,
},
{
name: "emitDeclarationOnly",
type: "boolean",
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5835,6 +5835,14 @@
"category": "Message",
"code": 6803
},
"Enable extra transform stages that make declaration emit output prettier.": {
"category": "Message",
"code": 6804
},
"true if declaration is true, false otherwise.": {
"category": "Message",
"code": 6805
},

"one of:": {
"category": "Message",
Expand Down
1 change: 1 addition & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ namespace ts {
createLiteralConstValue: notImplemented,
isSymbolAccessible: notImplemented,
isEntityNameVisible: notImplemented,
resolveName: notImplemented,
// Returns the constant value this property access resolves to: notImplemented, or 'undefined' for a non-constant
getConstantValue: notImplemented,
getReferencedValueDeclaration: notImplemented,
Expand Down
14 changes: 10 additions & 4 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5354,10 +5354,16 @@ namespace ts {

// @api
function updateBundle(node: Bundle, sourceFiles: readonly SourceFile[], prepends: readonly (UnparsedSource | InputFiles)[] = emptyArray) {
return node.sourceFiles !== sourceFiles
|| node.prepends !== prepends
? update(createBundle(sourceFiles, prepends), node)
: node;
if (node.sourceFiles !== sourceFiles
|| node.prepends !== prepends) {
const newNode = update(createBundle(sourceFiles, prepends), node);
newNode.syntheticFileReferences = node.syntheticFileReferences;
newNode.syntheticLibReferences = node.syntheticLibReferences;
newNode.syntheticTypeReferences = node.syntheticTypeReferences;
newNode.hasNoDefaultLib = node.hasNoDefaultLib;
return newNode;
}
return node;
}

// @api
Expand Down
7 changes: 5 additions & 2 deletions src/compiler/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace ts {
export function getTransformers(compilerOptions: CompilerOptions, customTransformers?: CustomTransformers, emitOnlyDtsFiles?: boolean): EmitTransformers {
return {
scriptTransformers: getScriptTransformers(compilerOptions, customTransformers, emitOnlyDtsFiles),
declarationTransformers: getDeclarationTransformers(customTransformers),
declarationTransformers: getDeclarationTransformers(compilerOptions, customTransformers),
};
}

Expand Down Expand Up @@ -100,9 +100,12 @@ namespace ts {
return transformers;
}

function getDeclarationTransformers(customTransformers?: CustomTransformers) {
function getDeclarationTransformers(compilerOptions: CompilerOptions, customTransformers?: CustomTransformers) {
const transformers: TransformerFactory<SourceFile | Bundle>[] = [];
transformers.push(transformDeclarations);
if (getEmitPrettyDeclarations(compilerOptions)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isnt checking if this transform is for incremental signature purposes but i think its a good idea to do it this way.. We try to update signatures to d.ts emit hash during emit so not having discrepancies between d.ts emit for incremental signature purpose and emit is better..

transformers.push(transformCoalesceImports);
}
addRange(transformers, customTransformers && map(customTransformers.afterDeclarations, wrapDeclarationTransformerFactory));
return transformers;
}
Expand Down
Loading