Skip to content

Commit 23e2962

Browse files
committed
PR comment fixes
1. Add 'export' keyword to variables within the namespace. 2. Omit property creation in the namespace when it exists. 3. Update tests Signed-off-by: Hana Joo <hanajoo@google.com>
1 parent c7e4f7b commit 23e2962

4 files changed

Lines changed: 59 additions & 3 deletions

src/services/codefixes/fixMissingTypeAnnotationOnExports.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,10 @@ function withChanges<T>(
261261
const newProperties = []
262262
for (const symbol of elements) {
263263
if (!isIdentifierText(symbol.name, program.getCompilerOptions().target)) continue;
264+
// If there's an existing variable declaration for this property - skip.
265+
if (symbol.valueDeclaration && isVariableDeclaration(symbol.valueDeclaration)) continue;
264266
newProperties.push(factory.createVariableStatement(
265-
/*modifiers*/ undefined,
267+
[factory.createModifier(SyntaxKind.ExportKeyword)],
266268
factory.createVariableDeclarationList(
267269
[factory.createVariableDeclaration(
268270
symbol.name,

tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports47-expando-functions-3.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ verify.codeFix({
1515
newFileContent:
1616
`function foo(): void {}
1717
declare namespace foo {
18-
var x: number;
19-
var y: number;
18+
export var x: number;
19+
export var y: number;
2020
}
2121
foo.x = 1;
2222
foo.y = 1;`
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @isolatedDeclarations: true
4+
// @declaration: true
5+
// @lib: es2019
6+
7+
// @Filename: /code.ts
8+
////function foo(): void {}
9+
////// cannot name this property because it's an invalid variable name.
10+
////foo["@bar"] = 42;
11+
////foo.x = 1;
12+
13+
verify.codeFix({
14+
description: "Annotate types of properties expando function in a namespace",
15+
index: 0,
16+
newFileContent:
17+
`function foo(): void {}
18+
declare namespace foo {
19+
export var x: number;
20+
}
21+
// cannot name this property because it's an invalid variable name.
22+
foo["@bar"] = 42;
23+
foo.x = 1;`
24+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @isolatedDeclarations: true
4+
// @declaration: true
5+
// @lib: es2019
6+
7+
// @Filename: /code.ts
8+
////function foo(): void {}
9+
////// x already exists, so do not generate code for 'x'
10+
////foo.x = 1;
11+
////foo.y = 1;
12+
////namespace foo {
13+
//// export let x = 42;
14+
////}
15+
16+
verify.codeFix({
17+
description: "Annotate types of properties expando function in a namespace",
18+
index: 0,
19+
newFileContent:
20+
`function foo(): void {}
21+
declare namespace foo {
22+
export var y: number;
23+
}
24+
// x already exists, so do not generate code for 'x'
25+
foo.x = 1;
26+
foo.y = 1;
27+
namespace foo {
28+
export let x = 42;
29+
}`
30+
});

0 commit comments

Comments
 (0)