Skip to content

Commit

Permalink
Do not print typeof operator for Symbol types
Browse files Browse the repository at this point in the history
and rather print out symbol or unique symbol

Signed-off-by: Hana Joo <hanajoo@google.com>
  • Loading branch information
h-joo committed Aug 24, 2023
1 parent 40f9a58 commit 82dfd3b
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ function foo() {
export const a = {
value: 0,
array: [1, 2, 3],
fn(value: string): number { return 0; }
fn(value: string): number { return 0; },
} as const;
const b = {
value: 0,
array: [1, 2, 3],
fn(value: string): number { return 0; }
fn(value: string): number { return 0; },
} as const;
export const c: {
value: number;
Expand All @@ -18,12 +18,12 @@ export const c: {
} = {
value: 0,
array: [1, 2, 3],
fn(value: string): number { return 0; }
fn(value: string): number { return 0; },
};
const d = {
value: 0,
array: [1, 2, 3],
fn(value: string): number { return 0; }
fn(value: string): number { return 0; },
};
export const e: {
readonly value: number;
Expand All @@ -32,12 +32,12 @@ export const e: {
} = {
value: foo(),
array: [1, 2, 3],
fn(value: string): number { return 0; }
fn(value: string): number { return 0; },
} as const;
const f = {
value: foo(),
array: [1, 2, 3],
fn(value: string): number { return 0; }
fn(value: string): number { return 0; },
} as const;
export const g: {
value: number;
Expand All @@ -46,10 +46,10 @@ export const g: {
} = {
value: foo(),
array: [1, 2, 3],
fn(value: string): number { return 0; }
fn(value: string): number { return 0; },
};
const h = {
value: foo(),
array: [1, 2, 3],
fn(value: string): number { return 0; }
fn(value: string): number { return 0; },
};
8 changes: 4 additions & 4 deletions external-declarations/fixer-test/expected/object_literals.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
export const a = {
value: 0,
array: [1, 2, 3]
array: [1, 2, 3],
} as const;
const b = {
value: 0,
array: [1, 2, 3]
array: [1, 2, 3],
} as const;
export const c: {
value: number;
array: number[];
} = {
value: 0,
array: [1, 2, 3]
array: [1, 2, 3],
};
const d = {
value: 0,
array: [1, 2, 3]
array: [1, 2, 3],
};
14 changes: 14 additions & 0 deletions external-declarations/fixer-test/expected/symbols.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const a: unique symbol = Symbol();
export function foo(): symbol {
return Symbol();
}
export const z: {
z: symbol;
} = {
z: Symbol(),
};
export const z2: {
readonly z: symbol;
} = {
z: Symbol()
} as const;
14 changes: 14 additions & 0 deletions external-declarations/fixer-test/source/symbols.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

export const a = Symbol();

export function foo() {
return Symbol();
}

export const z = {
z: Symbol(),
};

export const z2 = {
z: Symbol()
} as const;
16 changes: 8 additions & 8 deletions external-declarations/src/code-mod/code-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import * as ts from "typescript";

import { SymbolTracker } from "../compiler/types";

const declarationEmitNodeBuilderFlags =
ts.NodeBuilderFlags.MultilineObjectLiterals |
ts.NodeBuilderFlags.WriteClassExpressionAsTypeLiteral |
ts.NodeBuilderFlags.UseTypeOfFunction |
ts.NodeBuilderFlags.UseStructuralFallback |
ts.NodeBuilderFlags.AllowEmptyTuple |
ts.NodeBuilderFlags.GenerateNamesForShadowedTypeParams |
ts.NodeBuilderFlags.NoTruncation;
const declarationEmitNodeBuilderFlags = ts.NodeBuilderFlags.MultilineObjectLiterals
| ts.NodeBuilderFlags.WriteClassExpressionAsTypeLiteral
| ts.NodeBuilderFlags.UseTypeOfFunction
| ts.NodeBuilderFlags.UseStructuralFallback
| ts.NodeBuilderFlags.AllowEmptyTuple
| ts.NodeBuilderFlags.GenerateNamesForShadowedTypeParams
| ts.NodeBuilderFlags.NoTruncation
| ts.NodeBuilderFlags.AllowUniqueESSymbolType;

function tryGetReturnType(typeChecker: ts.TypeChecker, node: ts.SignatureDeclaration): ts.Type | undefined {
const signature = typeChecker.getSignatureFromDeclaration(node);
Expand Down
2 changes: 1 addition & 1 deletion external-declarations/src/code-mod/fixer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async function main() {
const testFile = normalizePath(allTests[count].file);
const caseData = await loadTestCase(testFile);

const settings: ts.CompilerOptions = {};
const settings: ts.CompilerOptions = { target: ts.ScriptTarget.ES2019 };
setCompilerOptionsFromHarnessSetting(caseData.settings, settings);

function createHarnessTestFile(lastUnit: TestUnitData): TestFile {
Expand Down
16 changes: 8 additions & 8 deletions src/services/codefixes/fixMissingTypeAnnotationOnExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ const canHaveExplicitTypeAnnotation = new Set<SyntaxKind>([
SyntaxKind.ArrayBindingPattern,
]);

const declarationEmitNodeBuilderFlags =
NodeBuilderFlags.MultilineObjectLiterals |
NodeBuilderFlags.WriteClassExpressionAsTypeLiteral |
NodeBuilderFlags.UseTypeOfFunction |
NodeBuilderFlags.UseStructuralFallback |
NodeBuilderFlags.AllowEmptyTuple |
NodeBuilderFlags.GenerateNamesForShadowedTypeParams |
NodeBuilderFlags.NoTruncation;
const declarationEmitNodeBuilderFlags = NodeBuilderFlags.MultilineObjectLiterals
| NodeBuilderFlags.WriteClassExpressionAsTypeLiteral
| NodeBuilderFlags.UseTypeOfFunction
| NodeBuilderFlags.UseStructuralFallback
| NodeBuilderFlags.AllowEmptyTuple
| NodeBuilderFlags.GenerateNamesForShadowedTypeParams
| NodeBuilderFlags.NoTruncation
| NodeBuilderFlags.AllowUniqueESSymbolType;

registerCodeFix({
errorCodes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ verify.codeFixAvailable([
{ description: ts.Diagnostics.Declaration_emit_for_this_file_requires_type_resolution_An_explicit_type_annotation_may_unblock_declaration_emit.message }
]);

// TODO: the typeof operator here is wrong.
verify.codeFix({
description: ts.Diagnostics.Declaration_emit_for_this_file_requires_type_resolution_An_explicit_type_annotation_may_unblock_declaration_emit.message,
index: 0,
newFileContent:
`export const a: typeof a = Symbol();`
`export const a: unique symbol = Symbol();`
});
23 changes: 23 additions & 0 deletions tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports21.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path='fourslash.ts'/>

// @isolatedDeclarations: true
// @declaration: true
// @lib: es2019
//// export const a = {
//// z: Symbol()
//// } as const;

verify.codeFixAvailable([
{ description: ts.Diagnostics.Declaration_emit_for_this_file_requires_type_resolution_An_explicit_type_annotation_may_unblock_declaration_emit.message }
]);

verify.codeFix({
description: ts.Diagnostics.Declaration_emit_for_this_file_requires_type_resolution_An_explicit_type_annotation_may_unblock_declaration_emit.message,
index: 0,
newFileContent:
`export const a: {
readonly z: symbol;
} = {
z: Symbol()
} as const;`
});
21 changes: 21 additions & 0 deletions tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports22.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path='fourslash.ts'/>

// @isolatedDeclarations: true
// @declaration: true
// @lib: es2019
//// export function foo () {
//// return Symbol();
//// }

verify.codeFixAvailable([
{ description: ts.Diagnostics.Declaration_emit_for_this_file_requires_type_resolution_An_explicit_type_annotation_may_unblock_declaration_emit.message }
]);

verify.codeFix({
description: ts.Diagnostics.Declaration_emit_for_this_file_requires_type_resolution_An_explicit_type_annotation_may_unblock_declaration_emit.message,
index: 0,
newFileContent:
`export function foo(): symbol {
return Symbol();
}`
});

0 comments on commit 82dfd3b

Please sign in to comment.