Skip to content

Force inclusion of ES6, DOM.Iterable #62025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
39 changes: 31 additions & 8 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1821,7 +1821,9 @@ export function createProgram(_rootNamesOrOptions: readonly string[] | CreatePro
}
else {
forEach(options.lib, (libFileName, index) => {
processRootFile(pathForLibFile(libFileName), /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ false, { kind: FileIncludeKind.LibFile, index });
for (const p of pathsForLibFile(libFileName)) {
processRootFile(p, /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ false, { kind: FileIncludeKind.LibFile, index });
}
});
}
}
Expand Down Expand Up @@ -3855,12 +3857,28 @@ export function createProgram(_rootNamesOrOptions: readonly string[] | CreatePro
}
}

function pathForLibFile(libFileName: string): string {
const existing = resolvedLibReferences?.get(libFileName);
if (existing) return existing.actual;
const result = pathForLibFileWorker(libFileName);
(resolvedLibReferences ??= new Map()).set(libFileName, result);
return result.actual;
function pathsForLibFile(libFileName: string): string[] {
const names = [libFileName];
// Force the inclusion of ES6 and DOM's iterable libs.
switch (libFileName) {
case "lib.dom.d.ts":
names.push("lib.dom.iterable.d.ts");
// TODO: auto add dom.asynciterable.d.ts in es2018?
break;
case "lib.es5.d.ts":
names.push("lib.es2015.d.ts");
break;
}

return names.map(pathForLibFile);

function pathForLibFile(libFileName: string): string {
const existing = resolvedLibReferences?.get(libFileName);
if (existing) return existing.actual;
const result = pathForLibFileWorker(libFileName);
(resolvedLibReferences ??= new Map()).set(libFileName, result);
return result.actual;
}
}

function pathForLibFileWorker(libFileName: string): LibResolution {
Expand Down Expand Up @@ -3925,7 +3943,12 @@ export function createProgram(_rootNamesOrOptions: readonly string[] | CreatePro
const libFileName = getLibFileNameFromLibReference(libReference);
if (libFileName) {
// we ignore any 'no-default-lib' reference set on this file.
processRootFile(pathForLibFile(libFileName), /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ true, { kind: FileIncludeKind.LibReferenceDirective, file: file.path, index });
for (const p of pathsForLibFile(libFileName)) {
if (file.fileName === p) {
continue;
}
processRootFile(p, /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ true, { kind: FileIncludeKind.LibReferenceDirective, file: file.path, index });
}
}
else {
programDiagnostics.addFileProcessingDiagnostic({
Expand Down
17 changes: 1 addition & 16 deletions src/compiler/utilitiesPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,22 +325,7 @@ export const targetToLibMap: Map<ScriptTarget, string> = new Map([

export function getDefaultLibFileName(options: CompilerOptions): string {
const target = getEmitScriptTarget(options);
switch (target) {
case ScriptTarget.ESNext:
case ScriptTarget.ES2024:
case ScriptTarget.ES2023:
case ScriptTarget.ES2022:
case ScriptTarget.ES2021:
case ScriptTarget.ES2020:
case ScriptTarget.ES2019:
case ScriptTarget.ES2018:
case ScriptTarget.ES2017:
case ScriptTarget.ES2016:
case ScriptTarget.ES2015:
return targetToLibMap.get(target)!;
default:
return "lib.d.ts";
}
return targetToLibMap.get(target) ?? "lib.d.ts";
}

export function textSpanEnd(span: TextSpan): number {
Expand Down
9 changes: 3 additions & 6 deletions tests/baselines/reference/ES5For-ofTypeCheck10.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
ES5For-ofTypeCheck10.ts(9,6): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later.
ES5For-ofTypeCheck10.ts(14,15): error TS2495: Type 'MyStringIterator' is not an array type or a string type.
ES5For-ofTypeCheck10.ts(14,15): error TS2802: Type 'MyStringIterator' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.


==== ES5For-ofTypeCheck10.ts (2 errors) ====
==== ES5For-ofTypeCheck10.ts (1 errors) ====
// In ES3/5, you cannot for...of over an arbitrary iterable.
class MyStringIterator {
next() {
Expand All @@ -12,12 +11,10 @@ ES5For-ofTypeCheck10.ts(14,15): error TS2495: Type 'MyStringIterator' is not an
};
}
[Symbol.iterator]() {
~~~~~~
!!! error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later.
return this;
}
}

for (var v of new MyStringIterator) { }
~~~~~~~~~~~~~~~~~~~~
!!! error TS2495: Type 'MyStringIterator' is not an array type or a string type.
!!! error TS2802: Type 'MyStringIterator' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.
3 changes: 3 additions & 0 deletions tests/baselines/reference/ES5For-ofTypeCheck10.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class MyStringIterator {
}
[Symbol.iterator]() {
>[Symbol.iterator] : Symbol(MyStringIterator[Symbol.iterator], Decl(ES5For-ofTypeCheck10.ts, 7, 5))
>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))

return this;
>this : Symbol(MyStringIterator, Decl(ES5For-ofTypeCheck10.ts, 0, 0))
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/ES5For-ofTypeCheck10.types
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class MyStringIterator {
[Symbol.iterator]() {
>[Symbol.iterator] : () => this
> : ^^^^^^^^^^
>Symbol.iterator : any
> : ^^^
>Symbol : any
> : ^^^
>iterator : any
> : ^^^
>Symbol.iterator : unique symbol
> : ^^^^^^^^^^^^^
>Symbol : SymbolConstructor
> : ^^^^^^^^^^^^^^^^^
>iterator : unique symbol
> : ^^^^^^^^^^^^^

return this;
>this : this
Expand Down
10 changes: 5 additions & 5 deletions tests/baselines/reference/ES5SymbolProperty1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

=== ES5SymbolProperty1.ts ===
interface SymbolConstructor {
>SymbolConstructor : Symbol(SymbolConstructor, Decl(ES5SymbolProperty1.ts, 0, 0))
>SymbolConstructor : Symbol(SymbolConstructor, Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(ES5SymbolProperty1.ts, 0, 0))

foo: string;
>foo : Symbol(SymbolConstructor.foo, Decl(ES5SymbolProperty1.ts, 0, 29))
}
var Symbol: SymbolConstructor;
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(ES5SymbolProperty1.ts, 3, 3))
>SymbolConstructor : Symbol(SymbolConstructor, Decl(ES5SymbolProperty1.ts, 0, 0))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(ES5SymbolProperty1.ts, 3, 3))
>SymbolConstructor : Symbol(SymbolConstructor, Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(ES5SymbolProperty1.ts, 0, 0))

var obj = {
>obj : Symbol(obj, Decl(ES5SymbolProperty1.ts, 5, 3))

[Symbol.foo]: 0
>[Symbol.foo] : Symbol([Symbol.foo], Decl(ES5SymbolProperty1.ts, 5, 11))
>Symbol.foo : Symbol(SymbolConstructor.foo, Decl(ES5SymbolProperty1.ts, 0, 29))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(ES5SymbolProperty1.ts, 3, 3))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(ES5SymbolProperty1.ts, 3, 3))
>foo : Symbol(SymbolConstructor.foo, Decl(ES5SymbolProperty1.ts, 0, 29))
}

obj[Symbol.foo];
>obj : Symbol(obj, Decl(ES5SymbolProperty1.ts, 5, 3))
>Symbol.foo : Symbol(SymbolConstructor.foo, Decl(ES5SymbolProperty1.ts, 0, 29))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(ES5SymbolProperty1.ts, 3, 3))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(ES5SymbolProperty1.ts, 3, 3))
>foo : Symbol(SymbolConstructor.foo, Decl(ES5SymbolProperty1.ts, 0, 29))

16 changes: 0 additions & 16 deletions tests/baselines/reference/ES5SymbolProperty2.errors.txt

This file was deleted.

3 changes: 3 additions & 0 deletions tests/baselines/reference/ES5SymbolProperty2.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ module M {
>M.C : Symbol(M.C, Decl(ES5SymbolProperty2.ts, 1, 20))
>M : Symbol(M, Decl(ES5SymbolProperty2.ts, 0, 0))
>C : Symbol(M.C, Decl(ES5SymbolProperty2.ts, 1, 20))
>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))

18 changes: 7 additions & 11 deletions tests/baselines/reference/ES5SymbolProperty2.types
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module M {

var Symbol: any;
>Symbol : any
> : ^^^

export class C {
>C : C
Expand All @@ -17,7 +16,6 @@ module M {
>[Symbol.iterator] : () => void
> : ^^^^^^^^^^
>Symbol.iterator : any
> : ^^^
>Symbol : any
> : ^^^
>iterator : any
Expand All @@ -33,16 +31,14 @@ module M {
>C : typeof C
> : ^^^^^^^^
>Symbol.iterator : any
> : ^^^
>Symbol : any
> : ^^^
>iterator : any
> : ^^^
}

(new M.C)[Symbol.iterator];
>(new M.C)[Symbol.iterator] : () => void
> : ^^^^^^^^^^
>(new M.C)[Symbol.iterator] : error
>(new M.C) : M.C
> : ^^^
>new M.C : M.C
Expand All @@ -53,10 +49,10 @@ module M {
> : ^^^^^^^^
>C : typeof M.C
> : ^^^^^^^^^^
>Symbol.iterator : any
> : ^^^
>Symbol : any
> : ^^^
>iterator : any
> : ^^^
>Symbol.iterator : unique symbol
> : ^^^^^^^^^^^^^
>Symbol : SymbolConstructor
> : ^^^^^^^^^^^^^^^^^
>iterator : unique symbol
> : ^^^^^^^^^^^^^

14 changes: 14 additions & 0 deletions tests/baselines/reference/ES5SymbolProperty3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ES5SymbolProperty3.ts(1,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'Symbol' must be of type 'SymbolConstructor', but here has type 'any'.


==== ES5SymbolProperty3.ts (1 errors) ====
var Symbol: any;
~~~~~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Symbol' must be of type 'SymbolConstructor', but here has type 'any'.
!!! related TS6203 lib.es2015.symbol.d.ts:--:--: 'Symbol' was also declared here.

class C {
[Symbol.iterator]() { }
}

(new C)[Symbol.iterator]
10 changes: 7 additions & 3 deletions tests/baselines/reference/ES5SymbolProperty3.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

=== ES5SymbolProperty3.ts ===
var Symbol: any;
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(ES5SymbolProperty3.ts, 0, 3))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(ES5SymbolProperty3.ts, 0, 3))

class C {
>C : Symbol(C, Decl(ES5SymbolProperty3.ts, 0, 16))

[Symbol.iterator]() { }
>[Symbol.iterator] : Symbol(C[Symbol.iterator], Decl(ES5SymbolProperty3.ts, 2, 9))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(ES5SymbolProperty3.ts, 0, 3))
>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(ES5SymbolProperty3.ts, 0, 3))
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
}

(new C)[Symbol.iterator]
>C : Symbol(C, Decl(ES5SymbolProperty3.ts, 0, 16))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(ES5SymbolProperty3.ts, 0, 3))
>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(ES5SymbolProperty3.ts, 0, 3))
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))

25 changes: 14 additions & 11 deletions tests/baselines/reference/ES5SymbolProperty3.types
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

=== ES5SymbolProperty3.ts ===
var Symbol: any;
>Symbol : any
>Symbol : SymbolConstructor
> : ^^^^^^^^^^^^^^^^^

class C {
>C : C
Expand All @@ -11,11 +12,12 @@ class C {
[Symbol.iterator]() { }
>[Symbol.iterator] : () => void
> : ^^^^^^^^^^
>Symbol.iterator : any
>Symbol : any
> : ^^^
>iterator : any
> : ^^^
>Symbol.iterator : unique symbol
> : ^^^^^^^^^^^^^
>Symbol : SymbolConstructor
> : ^^^^^^^^^^^^^^^^^
>iterator : unique symbol
> : ^^^^^^^^^^^^^
}

(new C)[Symbol.iterator]
Expand All @@ -27,9 +29,10 @@ class C {
> : ^
>C : typeof C
> : ^^^^^^^^
>Symbol.iterator : any
>Symbol : any
> : ^^^
>iterator : any
> : ^^^
>Symbol.iterator : unique symbol
> : ^^^^^^^^^^^^^
>Symbol : SymbolConstructor
> : ^^^^^^^^^^^^^^^^^
>iterator : unique symbol
> : ^^^^^^^^^^^^^

14 changes: 14 additions & 0 deletions tests/baselines/reference/ES5SymbolProperty4.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ES5SymbolProperty4.ts(1,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'Symbol' must be of type 'SymbolConstructor', but here has type '{ iterator: string; }'.


==== ES5SymbolProperty4.ts (1 errors) ====
var Symbol: { iterator: string };
~~~~~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Symbol' must be of type 'SymbolConstructor', but here has type '{ iterator: string; }'.
!!! related TS6203 lib.es2015.symbol.d.ts:--:--: 'Symbol' was also declared here.

class C {
[Symbol.iterator]() { }
}

(new C)[Symbol.iterator]
14 changes: 7 additions & 7 deletions tests/baselines/reference/ES5SymbolProperty4.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

=== ES5SymbolProperty4.ts ===
var Symbol: { iterator: string };
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(ES5SymbolProperty4.ts, 0, 3))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(ES5SymbolProperty4.ts, 0, 3))
>iterator : Symbol(iterator, Decl(ES5SymbolProperty4.ts, 0, 13))

class C {
>C : Symbol(C, Decl(ES5SymbolProperty4.ts, 0, 33))

[Symbol.iterator]() { }
>[Symbol.iterator] : Symbol(C[Symbol.iterator], Decl(ES5SymbolProperty4.ts, 2, 9))
>Symbol.iterator : Symbol(iterator, Decl(ES5SymbolProperty4.ts, 0, 13))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(ES5SymbolProperty4.ts, 0, 3))
>iterator : Symbol(iterator, Decl(ES5SymbolProperty4.ts, 0, 13))
>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(ES5SymbolProperty4.ts, 0, 3))
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
}

(new C)[Symbol.iterator]
>C : Symbol(C, Decl(ES5SymbolProperty4.ts, 0, 33))
>Symbol.iterator : Symbol(iterator, Decl(ES5SymbolProperty4.ts, 0, 13))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(ES5SymbolProperty4.ts, 0, 3))
>iterator : Symbol(iterator, Decl(ES5SymbolProperty4.ts, 0, 13))
>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(ES5SymbolProperty4.ts, 0, 3))
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))

Loading
Loading