Skip to content
Open
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
25 changes: 23 additions & 2 deletions internal/checker/nodecopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/nodebuilder"
"github.com/microsoft/typescript-go/internal/printer"
"github.com/microsoft/typescript-go/internal/scanner"
)

func (b *NodeBuilderImpl) reuseNode(node *ast.Node) *ast.Node {
Expand All @@ -21,14 +22,34 @@ func (b *NodeBuilderImpl) tryJSTypeNodeToTypeNode(node *ast.Node) *ast.Node {
return b.reuseNode(node)
}

// a wrapper around `reuseNode` that handles renaming `new` to `"new"` so we don't accidentally emit constructor signatures when we don't mean to
// a wrapper around `reuseNode` for property names. It handles renaming `new` to `"new"` so we don't
// accidentally emit constructor signatures when we don't mean to, and normalizes string-literal
// property names whose text is a valid identifier into identifiers, matching the behavior of
// `createPropertyNameNodeForIdentifierOrLiteral` used when constructing fresh property name nodes
// (so that reused names emit consistently regardless of whether their containing type was reused
// from source or rebuilt from a type).
func (b *NodeBuilderImpl) reuseName(node *ast.Node) *ast.Node {
Comment thread
jakebailey marked this conversation as resolved.
res := b.reuseNode(node)
if res != nil && res.Kind == ast.KindIdentifier && node.AsIdentifier().Text == "new" {
if res == nil {
return res
}
if res.Kind == ast.KindIdentifier && node.AsIdentifier().Text == "new" {
str := b.f.NewStringLiteral("new", ast.TokenFlagsNone)
b.e.SetOriginal(str, res)
return b.setTextRange(str, res)
}
Comment thread
jakebailey marked this conversation as resolved.
if res.Kind == ast.KindStringLiteral {
text := res.AsStringLiteral().Text
// Skip normalization for "new" so that reused names like `"new"(): void` on a
// method signature are not converted to an identifier (which would become a
// construct signature). This mirrors the `isMethodNamedNew` guard in
// createPropertyNameNodeForIdentifierOrLiteral.
if text != "new" && scanner.IsIdentifierText(text, core.LanguageVariantStandard) {
ident := b.newIdentifier(text, nil)
b.e.SetOriginal(ident, res)
return b.setTextRange(ident, res)
}
}
Comment thread
jakebailey marked this conversation as resolved.
return res
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [tests/cases/compiler/declarationEmitStringNamedPropertyConsistency.ts] ////

//// [declarationEmitStringNamedPropertyConsistency.ts]
function createSlice<T>(o: T) {
return o
}
export const platformSlice = createSlice({
"query": {
"search" : "",
"user": ""
},
});




//// [declarationEmitStringNamedPropertyConsistency.d.ts]
export declare const platformSlice: {
query: {
search: string;
user: string;
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [tests/cases/compiler/declarationEmitStringNamedPropertyConsistency.ts] ////

=== declarationEmitStringNamedPropertyConsistency.ts ===
function createSlice<T>(o: T) {
>createSlice : Symbol(createSlice, Decl(declarationEmitStringNamedPropertyConsistency.ts, 0, 0))
>T : Symbol(T, Decl(declarationEmitStringNamedPropertyConsistency.ts, 0, 21))
>o : Symbol(o, Decl(declarationEmitStringNamedPropertyConsistency.ts, 0, 24))
>T : Symbol(T, Decl(declarationEmitStringNamedPropertyConsistency.ts, 0, 21))

return o
>o : Symbol(o, Decl(declarationEmitStringNamedPropertyConsistency.ts, 0, 24))
}
export const platformSlice = createSlice({
>platformSlice : Symbol(platformSlice, Decl(declarationEmitStringNamedPropertyConsistency.ts, 3, 12))
>createSlice : Symbol(createSlice, Decl(declarationEmitStringNamedPropertyConsistency.ts, 0, 0))

"query": {
>"query" : Symbol("query", Decl(declarationEmitStringNamedPropertyConsistency.ts, 3, 42))

"search" : "",
>"search" : Symbol("search", Decl(declarationEmitStringNamedPropertyConsistency.ts, 4, 14))

"user": ""
>"user" : Symbol("user", Decl(declarationEmitStringNamedPropertyConsistency.ts, 5, 22))

},
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/compiler/declarationEmitStringNamedPropertyConsistency.ts] ////

=== declarationEmitStringNamedPropertyConsistency.ts ===
function createSlice<T>(o: T) {
>createSlice : <T>(o: T) => T
>o : T

return o
>o : T
}
export const platformSlice = createSlice({
>platformSlice : { query: { search: string; user: string; }; }
>createSlice({ "query": { "search" : "", "user": "" },}) : { query: { search: string; user: string; }; }
>createSlice : <T>(o: T) => T
>{ "query": { "search" : "", "user": "" },} : { query: { search: string; user: string; }; }

"query": {
>"query" : { search: string; user: string; }
>{ "search" : "", "user": "" } : { search: string; user: string; }

"search" : "",
>"search" : string
>"" : ""

"user": ""
>"user" : string
>"" : ""

},
});

Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var b5 = o5["_proto__"];
//// [escapedReservedCompilerNamedIdentifier.d.ts]
declare var __proto__: number;
declare var o: {
"__proto__": number;
__proto__: number;
};
declare var b: number;
declare var o1: {
Expand All @@ -78,7 +78,7 @@ declare var o1: {
declare var b1: number;
declare var ___proto__: number;
declare var o2: {
"___proto__": number;
___proto__: number;
};
declare var b2: number;
declare var o3: {
Expand All @@ -87,7 +87,7 @@ declare var o3: {
declare var b3: number;
declare var _proto__: number;
declare var o4: {
"_proto__": number;
_proto__: number;
};
declare var b4: number;
declare var o5: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

=== index.js ===
const j = require("./package.json");
>j : { name: string; version: string; description: string; main: string; bin: { "cli": string; }; engines: { "node": string; }; scripts: { "scriptname": string; }; devDependencies: { "@ns/dep": string; }; dependencies: { "dep": string; }; repository: string; keywords: string[]; author: string; license: string; homepage: string; config: { "o": string[]; }; }
>require("./package.json") : { name: string; version: string; description: string; main: string; bin: { "cli": string; }; engines: { "node": string; }; scripts: { "scriptname": string; }; devDependencies: { "@ns/dep": string; }; dependencies: { "dep": string; }; repository: string; keywords: string[]; author: string; license: string; homepage: string; config: { "o": string[]; }; }
>j : { name: string; version: string; description: string; main: string; bin: { cli: string; }; engines: { node: string; }; scripts: { scriptname: string; }; devDependencies: { "@ns/dep": string; }; dependencies: { dep: string; }; repository: string; keywords: string[]; author: string; license: string; homepage: string; config: { o: string[]; }; }
>require("./package.json") : { name: string; version: string; description: string; main: string; bin: { cli: string; }; engines: { node: string; }; scripts: { scriptname: string; }; devDependencies: { "@ns/dep": string; }; dependencies: { dep: string; }; repository: string; keywords: string[]; author: string; license: string; homepage: string; config: { o: string[]; }; }
>require : any
>"./package.json" : "./package.json"

module.exports = j;
>module.exports = j : { name: string; version: string; description: string; main: string; bin: { "cli": string; }; engines: { "node": string; }; scripts: { "scriptname": string; }; devDependencies: { "@ns/dep": string; }; dependencies: { "dep": string; }; repository: string; keywords: string[]; author: string; license: string; homepage: string; config: { "o": string[]; }; }
>module.exports : { name: string; version: string; description: string; main: string; bin: { "cli": string; }; engines: { "node": string; }; scripts: { "scriptname": string; }; devDependencies: { "@ns/dep": string; }; dependencies: { "dep": string; }; repository: string; keywords: string[]; author: string; license: string; homepage: string; config: { "o": string[]; }; }
>module : { exports: { name: string; version: string; description: string; main: string; bin: { "cli": string; }; engines: { "node": string; }; scripts: { "scriptname": string; }; devDependencies: { "@ns/dep": string; }; dependencies: { "dep": string; }; repository: string; keywords: string[]; author: string; license: string; homepage: string; config: { "o": string[]; }; }; }
>exports : { name: string; version: string; description: string; main: string; bin: { "cli": string; }; engines: { "node": string; }; scripts: { "scriptname": string; }; devDependencies: { "@ns/dep": string; }; dependencies: { "dep": string; }; repository: string; keywords: string[]; author: string; license: string; homepage: string; config: { "o": string[]; }; }
>j : { name: string; version: string; description: string; main: string; bin: { "cli": string; }; engines: { "node": string; }; scripts: { "scriptname": string; }; devDependencies: { "@ns/dep": string; }; dependencies: { "dep": string; }; repository: string; keywords: string[]; author: string; license: string; homepage: string; config: { "o": string[]; }; }
>module.exports = j : { name: string; version: string; description: string; main: string; bin: { cli: string; }; engines: { node: string; }; scripts: { scriptname: string; }; devDependencies: { "@ns/dep": string; }; dependencies: { dep: string; }; repository: string; keywords: string[]; author: string; license: string; homepage: string; config: { o: string[]; }; }
>module.exports : { name: string; version: string; description: string; main: string; bin: { cli: string; }; engines: { node: string; }; scripts: { scriptname: string; }; devDependencies: { "@ns/dep": string; }; dependencies: { dep: string; }; repository: string; keywords: string[]; author: string; license: string; homepage: string; config: { o: string[]; }; }
>module : { exports: { name: string; version: string; description: string; main: string; bin: { cli: string; }; engines: { node: string; }; scripts: { scriptname: string; }; devDependencies: { "@ns/dep": string; }; dependencies: { dep: string; }; repository: string; keywords: string[]; author: string; license: string; homepage: string; config: { o: string[]; }; }; }
>exports : { name: string; version: string; description: string; main: string; bin: { cli: string; }; engines: { node: string; }; scripts: { scriptname: string; }; devDependencies: { "@ns/dep": string; }; dependencies: { dep: string; }; repository: string; keywords: string[]; author: string; license: string; homepage: string; config: { o: string[]; }; }
>j : { name: string; version: string; description: string; main: string; bin: { cli: string; }; engines: { node: string; }; scripts: { scriptname: string; }; devDependencies: { "@ns/dep": string; }; dependencies: { dep: string; }; repository: string; keywords: string[]; author: string; license: string; homepage: string; config: { o: string[]; }; }

=== package.json ===
{
Expand Down

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions testdata/submoduleAccepted.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1248,9 +1248,6 @@ compiler/defaultDeclarationEmitShadowedNamedCorrectly.js.diff
compiler/es5ExportEqualsDts(target=es2015).js.diff
compiler/declarationEmitNameConflicts.js.diff

## Properties with __proto__-like names now quoted in declaration emit (it's quoted in the originating sourcefile)
compiler/escapedReservedCompilerNamedIdentifier.js.diff

## Nested /*elided*/ type annotation comments removed at deeper nesting levels (`/*elided*/` comments are internal artifacts with no semantic meaning)
## Declaration emit no longer preserves elided comments in nested class expressions
compiler/emitClassExpressionInDeclarationFile.js.diff
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @declaration: true
// @emitDeclarationOnly: true

function createSlice<T>(o: T) {
return o
}
export const platformSlice = createSlice({
"query": {
"search" : "",
Comment thread
jakebailey marked this conversation as resolved.
"user": ""
},
});
Loading