Skip to content

Fix printing and emit for definite assignment assertions #35095

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

Merged
merged 2 commits into from
Nov 21, 2019
Merged
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
2 changes: 1 addition & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3807,7 +3807,7 @@ namespace ts {
}

// Type annotations are TypeScript syntax.
if (node.type) {
if (node.type || node.exclamationToken) {
Copy link
Contributor

Choose a reason for hiding this comment

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

is this change related to the emitter?
If so, you probably also want to make the same change in computePropertyDeclaration (see #35097 which I erroneously opened)

transformFlags |= TransformFlags.AssertTypeScript;
}

Expand Down
1 change: 1 addition & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2741,6 +2741,7 @@ namespace ts {

function emitVariableDeclaration(node: VariableDeclaration) {
emit(node.name);
emit(node.exclamationToken);
emitTypeAnnotation(node.type);
emitInitializer(node.initializer, node.type ? node.type.end : node.name.end, node);
}
Expand Down
22 changes: 22 additions & 0 deletions src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1937,6 +1937,7 @@ namespace ts {
}

export function createVariableDeclaration(name: string | BindingName, type?: TypeNode, initializer?: Expression) {
/* Internally, one should probably use createTypeScriptVariableDeclaration instead and handle definite assignment assertions */
const node = <VariableDeclaration>createSynthesizedNode(SyntaxKind.VariableDeclaration);
node.name = asName(name);
node.type = type;
Expand All @@ -1945,13 +1946,34 @@ namespace ts {
}

export function updateVariableDeclaration(node: VariableDeclaration, name: BindingName, type: TypeNode | undefined, initializer: Expression | undefined) {
/* Internally, one should probably use updateTypeScriptVariableDeclaration instead and handle definite assignment assertions */
return node.name !== name
|| node.type !== type
|| node.initializer !== initializer
? updateNode(createVariableDeclaration(name, type, initializer), node)
: node;
}

/* @internal */
export function createTypeScriptVariableDeclaration(name: string | BindingName, exclaimationToken?: Token<SyntaxKind.ExclamationToken>, type?: TypeNode, initializer?: Expression) {
const node = <VariableDeclaration>createSynthesizedNode(SyntaxKind.VariableDeclaration);
node.name = asName(name);
node.type = type;
node.initializer = initializer !== undefined ? parenthesizeExpressionForList(initializer) : undefined;
node.exclamationToken = exclaimationToken;
return node;
}

/* @internal */
export function updateTypeScriptVariableDeclaration(node: VariableDeclaration, name: BindingName, exclaimationToken: Token<SyntaxKind.ExclamationToken> | undefined, type: TypeNode | undefined, initializer: Expression | undefined) {
return node.name !== name
|| node.type !== type
|| node.initializer !== initializer
|| node.exclamationToken !== exclaimationToken
? updateNode(createTypeScriptVariableDeclaration(name, exclaimationToken, type, initializer), node)
: node;
}

export function createVariableDeclarationList(declarations: readonly VariableDeclaration[], flags = NodeFlags.None) {
const node = <VariableDeclarationList>createSynthesizedNode(SyntaxKind.VariableDeclarationList);
node.flags |= flags & NodeFlags.BlockScoped;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ namespace ts {
}
shouldEnterSuppressNewDiagnosticsContextContext = true;
suppressNewDiagnosticContexts = true; // Variable declaration types also suppress new diagnostic contexts, provided the contexts wouldn't be made for binding pattern types
return cleanup(updateVariableDeclaration(input, input.name, ensureType(input, input.type), ensureNoInitializer(input)));
return cleanup(updateTypeScriptVariableDeclaration(input, input.name, /*exclaimationToken*/ undefined, ensureType(input, input.type), ensureNoInitializer(input)));
}
case SyntaxKind.TypeParameter: {
if (isPrivateMethodTypeParameter(input) && (input.default || input.constraint)) {
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2188,9 +2188,10 @@ namespace ts {
}

function visitVariableDeclaration(node: VariableDeclaration) {
return updateVariableDeclaration(
return updateTypeScriptVariableDeclaration(
node,
visitNode(node.name, visitor, isBindingName),
/*exclaimationToken*/ undefined,
/*type*/ undefined,
visitNode(node.initializer, visitor, isExpression));
}
Expand Down
11 changes: 11 additions & 0 deletions src/testRunner/unittests/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ namespace ts {
`class A extends B implements C implements D {}`,
ScriptTarget.ES2017
)));

// github #35093
printsCorrectly("definiteAssignmentAssertions", {}, printer => printer.printFile(createSourceFile(
"source.ts",
`class A {
prop!: string;
}

let x!: string;`,
ScriptTarget.ES2017
)));
});

describe("printBundle", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class A {
prop!: string;
}
let x!: string;