Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 2 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
Copy Markdown
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 1 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 22 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 2 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 3 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 11 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;
Morty Proxy This is a proxified and sanitized view of the page, visit original site.