Skip to content
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
4 changes: 4 additions & 0 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -4845,6 +4845,10 @@ func IsImportDeclaration(node *Node) bool {
return node.Kind == KindImportDeclaration
}

func IsJSImportDeclaration(node *Node) bool {
return node.Kind == KindJSImportDeclaration
}

func IsImportDeclarationOrJSImportDeclaration(node *Node) bool {
return node.Kind == KindImportDeclaration || node.Kind == KindJSImportDeclaration
}
Expand Down
12 changes: 11 additions & 1 deletion internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,12 +498,21 @@ func (p *Parser) reparseTopLevelAwait(sourceFile *ast.SourceFile) *ast.Node {
func (p *Parser) parseListIndex(kind ParsingContext, parseElement func(p *Parser, index int) *ast.Node) []*ast.Node {
saveParsingContexts := p.parsingContexts
p.parsingContexts |= 1 << kind
outerReparseList := p.reparseList
p.reparseList = nil
list := make([]*ast.Node, 0, 16)
for i := 0; !p.isListTerminator(kind); i++ {
if p.isListElement(kind, false /*inErrorRecovery*/) {
elt := parseElement(p, i)
if len(p.reparseList) > 0 {
list = append(list, p.reparseList...)
for _, e := range p.reparseList {
// Propagate @typedef type alias declarations outwards to a context that permits them.
if (ast.IsJSTypeAliasDeclaration(e) || ast.IsJSImportDeclaration(e)) && kind != PCSourceElements && kind != PCBlockStatements {
outerReparseList = append(outerReparseList, e)
} else {
list = append(list, e)
}
}
p.reparseList = nil
}
list = append(list, elt)
Expand All @@ -513,6 +522,7 @@ func (p *Parser) parseListIndex(kind ParsingContext, parseElement func(p *Parser
break
}
}
p.reparseList = outerReparseList
p.parsingContexts = saveParsingContexts
return p.nodeSlicePool.Clone(list)
}
Expand Down
25 changes: 25 additions & 0 deletions testdata/baselines/reference/compiler/typedefHoisting.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error TS5055: Cannot write file 'x.js' because it would overwrite input file.
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
error TS5055: Cannot write file 'y.js' because it would overwrite input file.
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.


!!! error TS5055: Cannot write file 'x.js' because it would overwrite input file.
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
!!! error TS5055: Cannot write file 'y.js' because it would overwrite input file.
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
==== x.js (0 errors) ====
class C {
/** @import {Bar} from "./y" */
/** @typedef {Bar[]} Bars */
/** @type {Bars} */
foo = ["abc", "def"]
bar(/** @type {Bar} */ x) {
return x
}
}

==== y.js (0 errors) ====
/** @typedef {string} Bar */
export {}

34 changes: 34 additions & 0 deletions testdata/baselines/reference/compiler/typedefHoisting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [tests/cases/compiler/typedefHoisting.ts] ////

//// [x.js]
class C {
/** @import {Bar} from "./y" */
/** @typedef {Bar[]} Bars */
/** @type {Bars} */
foo = ["abc", "def"]
bar(/** @type {Bar} */ x) {
return x
}
}

//// [y.js]
/** @typedef {string} Bar */
export {}




//// [y.d.ts]
export type Bar = string;
/** @typedef {string} Bar */
export {};
//// [x.d.ts]
import type { Bar } from "./y";
type Bars = Bar[];
declare class C {
/** @import {Bar} from "./y" */
/** @typedef {Bar[]} Bars */
/** @type {Bars} */
foo: Bars;
bar(/** @type {Bar} */ x: Bar): string;
}
26 changes: 26 additions & 0 deletions testdata/baselines/reference/compiler/typedefHoisting.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [tests/cases/compiler/typedefHoisting.ts] ////

=== x.js ===
class C {
>C : Symbol(C, Decl(x.js, 0, 0))

/** @import {Bar} from "./y" */
/** @typedef {Bar[]} Bars */
/** @type {Bars} */
foo = ["abc", "def"]
>foo : Symbol(C.foo, Decl(x.js, 0, 9))

bar(/** @type {Bar} */ x) {
>bar : Symbol(C.bar, Decl(x.js, 4, 24))
>x : Symbol(x, Decl(x.js, 5, 8))

return x
>x : Symbol(x, Decl(x.js, 5, 8))
}
}

=== y.js ===

/** @typedef {string} Bar */
export {}

29 changes: 29 additions & 0 deletions testdata/baselines/reference/compiler/typedefHoisting.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [tests/cases/compiler/typedefHoisting.ts] ////

=== x.js ===
class C {
>C : C

/** @import {Bar} from "./y" */
/** @typedef {Bar[]} Bars */
/** @type {Bars} */
foo = ["abc", "def"]
>foo : Bars
>["abc", "def"] : string[]
>"abc" : "abc"
>"def" : "def"

bar(/** @type {Bar} */ x) {
>bar : (x: string) => string
>x : string

return x
>x : string
}
}

=== y.js ===

/** @typedef {string} Bar */
export {}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ var ooscope2 = s => s.length > 0;


//// [callbackOnConstructor.d.ts]
export type ValueGetter_2 = (name: string) => boolean | number | string | undefined;
export declare class Preferences {
assignability: string;
export type ValueGetter_2 = (name: string) => boolean | number | string | undefined;
/**
* @callback ValueGetter_2
* @param {string} name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

//// [callbackOnConstructor.d.ts]
-export class Preferences {
+export type ValueGetter_2 = (name: string) => boolean | number | string | undefined;
+export declare class Preferences {
assignability: string;
+ export type ValueGetter_2 = (name: string) => boolean | number | string | undefined;
+ /**
+ * @callback ValueGetter_2
+ * @param {string} name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ export class Preferences {
var ooscope2 = s => s.length > 0
>ooscope2 : Symbol(ooscope2, Decl(callbackOnConstructor.js, 12, 3))
>s : Symbol(s, Decl(callbackOnConstructor.js, 12, 14))
>s.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>s : Symbol(s, Decl(callbackOnConstructor.js, 12, 14))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export class Preferences {
/** @type {ValueGetter_2} */
var ooscope2 = s => s.length > 0
>ooscope2 : ValueGetter_2
>s => s.length > 0 : (s: any) => boolean
>s : any
>s => s.length > 0 : (s: string) => boolean
>s : string
>s.length > 0 : boolean
>s.length : any
>s : any
>length : any
>s.length : number
>s : string
>length : number
>0 : 0

Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
templateTagOnClasses.js(7,23): error TS2304: Cannot find name 'T'.
templateTagOnClasses.js(7,29): error TS2304: Cannot find name 'T'.
templateTagOnClasses.js(25,1): error TS2322: Type 'boolean' is not assignable to type 'number'.


==== templateTagOnClasses.js (1 errors) ====
==== templateTagOnClasses.js (3 errors) ====
/**
* @template T
* @typedef {(t: T) => T} Id
*/
/** @template T */
class Foo {
/** @typedef {(t: T) => T} Id2 */
~
!!! error TS2304: Cannot find name 'T'.
~
!!! error TS2304: Cannot find name 'T'.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These errors are not reported by the old compiler, yet it still hoists and generates an invalid .d.ts file.

/** @param {T} x */
constructor (x) {
this.a = x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ class Foo {
* @return {T}
*/
foo(x, y, alpha) {
>foo : (x: T, y: Id<T>, alpha: (t: T) => T) => T
>foo : (x: T, y: Id<T>, alpha: Id2) => T
>x : T
>y : Id<T>
>alpha : (t: T) => T
>alpha : Id2

return alpha(y(x))
>alpha(y(x)) : T
>alpha : (t: T) => T
>alpha : Id2
>y(x) : T
>y : Id<T>
>x : T
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
bug25104.js(1,7): error TS2300: Duplicate identifier 'C'.
bug25104.js(3,19): error TS1005: '}' expected.
bug25104.js(4,26): error TS2300: Duplicate identifier 'C'.
bug25104.js(6,18): error TS1005: '}' expected.


==== bug25104.js (2 errors) ====
==== bug25104.js (4 errors) ====
class C {
~
!!! error TS2300: Duplicate identifier 'C'.
/**
* @typedef {C~A} C~B
~
!!! error TS1005: '}' expected.
* @typedef {object} C~A
~
!!! error TS2300: Duplicate identifier 'C'.
*/
/** @param {C~A} o */
~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,8 @@ exports.Preferences = Preferences;


//// [typedefOnSemicolonClassElement.d.ts]
export type A = string;
export declare class Preferences {
type A = string;
/** @type {A} */
a: A;
}


//// [DtsFileErrors]


dist/typedefOnSemicolonClassElement.d.ts(2,5): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
dist/typedefOnSemicolonClassElement.d.ts(4,8): error TS2693: 'A' only refers to a type, but is being used as a value here.
dist/typedefOnSemicolonClassElement.d.ts(5,1): error TS1128: Declaration or statement expected.


==== dist/typedefOnSemicolonClassElement.d.ts (3 errors) ====
export declare class Preferences {
type A = string;
~~~~
!!! error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
/** @type {A} */
a: A;
~
!!! error TS2693: 'A' only refers to a type, but is being used as a value here.
}
~
!!! error TS1128: Declaration or statement expected.

Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,9 @@

//// [typedefOnSemicolonClassElement.d.ts]
-export class Preferences {
+export type A = string;
+export declare class Preferences {
+ type A = string;
/** @type {A} */
- a: string;
+ a: A;
}
+
+
+//// [DtsFileErrors]
+
+
+dist/typedefOnSemicolonClassElement.d.ts(2,5): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
+dist/typedefOnSemicolonClassElement.d.ts(4,8): error TS2693: 'A' only refers to a type, but is being used as a value here.
+dist/typedefOnSemicolonClassElement.d.ts(5,1): error TS1128: Declaration or statement expected.
+
+
+==== dist/typedefOnSemicolonClassElement.d.ts (3 errors) ====
+ export declare class Preferences {
+ type A = string;
+ ~~~~
+!!! error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
+ /** @type {A} */
+ a: A;
+ ~
+!!! error TS2693: 'A' only refers to a type, but is being used as a value here.
+ }
+ ~
+!!! error TS1128: Declaration or statement expected.
+
}

This file was deleted.

Loading