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 23, 2023
1 parent 40f9a58 commit 2e596fb
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 15 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;
8 changes: 8 additions & 0 deletions external-declarations/src/code-mod/code-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ export function addTypeAnnotationTransformer(sourceFile: ts.SourceFile, program:
};

function typeToTypeNode(type: ts.Type, enclosingDeclaration: ts.Node) {
if (type.flags & ts.TypeFlags.UniqueESSymbol) {
return ts.factory.createTypeOperatorNode(
ts.SyntaxKind.UniqueKeyword,
ts.factory.createKeywordTypeNode(ts.SyntaxKind.SymbolKeyword));
}
else if (type.flags & ts.TypeFlags.ESSymbolLike) {
return ts.factory.createKeywordTypeNode(ts.SyntaxKind.SymbolKeyword);
}
const typeNode = typeChecker.typeToTypeNode(
type,
enclosingDeclaration,
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
7 changes: 7 additions & 0 deletions src/services/codefixes/fixMissingTypeAnnotationOnExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
textChanges,
Type,
TypeChecker,
TypeFlags,
VariableDeclaration,
VariableStatement,
} from "../_namespaces/ts";
Expand Down Expand Up @@ -504,6 +505,12 @@ function createChainedExpression(expression: ExpressionReverseChain, expressionT
}

function typeToTypeNode(type: Type, enclosingDeclaration: Node, typeChecker: TypeChecker) {
if (type.flags & TypeFlags.UniqueESSymbol) {
return factory.createTypeOperatorNode(SyntaxKind.UniqueKeyword, factory.createKeywordTypeNode(SyntaxKind.SymbolKeyword));
}
else if (type.flags & TypeFlags.ESSymbolLike) {
return factory.createKeywordTypeNode(SyntaxKind.SymbolKeyword);
}
const typeNode = typeChecker.typeToTypeNode(
type,
enclosingDeclaration,
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 2e596fb

Please sign in to comment.