-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(typescript): Allow references to the global Symbol in computed pr…
…operty names under `isolatedDeclarations` (#9869) fixes #9859 ref: microsoft/TypeScript#58771 Pass `unresolved_mark` to FastDts so it can check whether a `Symbol` ident refs to global Symbol.
- Loading branch information
Showing
13 changed files
with
206 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
swc: major | ||
swc_typescript: major | ||
--- | ||
|
||
fix(typescript): Allow references to the global Symbol in computed property names under isolatedDeclarations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
```==================== .D.TS ==================== | ||
export declare class NumberRange implements Iterable<number> { | ||
private start; | ||
private end; | ||
constructor(start: number, end: number); | ||
[Symbol.iterator](): Iterator<number>; | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export class NumberRange implements Iterable<number> { | ||
private start: number; | ||
private end: number; | ||
|
||
constructor(start: number, end: number) { | ||
this.start = start; | ||
this.end = end; | ||
} | ||
|
||
[Symbol.iterator](): Iterator<number> { | ||
let current = this.start; | ||
const end = this.end; | ||
|
||
return { | ||
next(): IteratorResult<number> { | ||
if (current <= end) { | ||
return { value: current++, done: false }; | ||
} else { | ||
return { value: undefined, done: true }; | ||
} | ||
}, | ||
}; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
crates/swc_typescript/tests/fixture/symbol-properties.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
```==================== .D.TS ==================== | ||
// Correct | ||
export declare const foo: { | ||
[Symbol.iterator]: () => void; | ||
[Symbol.asyncIterator]: () => Promise<void>; | ||
[globalThis.Symbol.iterator]: () => void; | ||
[Symbol.toStringTag]: string; | ||
}; | ||
export declare abstract class Foo { | ||
[Symbol.iterator](): void; | ||
[Symbol.asyncIterator](): Promise<void>; | ||
[globalThis.Symbol.iterator](): void; | ||
get [Symbol.toStringTag](): string; | ||
} | ||
// Incorrect | ||
export declare namespace Foo { | ||
const foo: { | ||
}; | ||
} | ||
export declare function bar(Symbol: { | ||
}, globalThis: { | ||
}): { | ||
}; | ||
==================== Errors ==================== | ||
x TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. | ||
,-[$DIR/tests/fixture/symbol-properties.ts:46:1] | ||
45 | export const foo = { | ||
46 | [Symbol.iterator]: (): void => {}, | ||
: ^^^^^^^^^^^^^^^^^ | ||
47 | [globalThis.Symbol.iterator]: (): void => {}, | ||
`---- | ||
x TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. | ||
,-[$DIR/tests/fixture/symbol-properties.ts:47:1] | ||
46 | [Symbol.iterator]: (): void => {}, | ||
47 | [globalThis.Symbol.iterator]: (): void => {}, | ||
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
48 | }; | ||
`---- | ||
x TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. | ||
,-[$DIR/tests/fixture/symbol-properties.ts:53:1] | ||
52 | return { | ||
53 | [Symbol.iterator]: (): void => {}, | ||
: ^^^^^^^^^^^^^^^^^ | ||
54 | [globalThis.Symbol.iterator]: (): void => {}, | ||
`---- | ||
x TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. | ||
,-[$DIR/tests/fixture/symbol-properties.ts:54:1] | ||
53 | [Symbol.iterator]: (): void => {}, | ||
54 | [globalThis.Symbol.iterator]: (): void => {}, | ||
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
55 | }; | ||
`---- | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Correct | ||
export const foo = { | ||
[Symbol.iterator]: (): void => {}, | ||
[Symbol.asyncIterator]: async (): Promise<void> => {}, | ||
[globalThis.Symbol.iterator]: (): void => {}, | ||
get [Symbol.toStringTag]() { | ||
return "foo"; | ||
}, | ||
}; | ||
|
||
export abstract class Foo { | ||
[Symbol.iterator](): void {} | ||
async [Symbol.asyncIterator](): Promise<void> {} | ||
[globalThis.Symbol.iterator](): void {} | ||
get [Symbol.toStringTag]() { | ||
return "Foo"; | ||
} | ||
} | ||
|
||
// OK because these are not exported | ||
|
||
namespace Foo { | ||
const Symbol = {}; | ||
const globalThis = {}; | ||
|
||
export const foo = { | ||
[Symbol.iterator]: (): void => {}, | ||
[globalThis.Symbol.iterator]: (): void => {}, | ||
}; | ||
} | ||
|
||
function bar(Symbol: {}, globalThis: {}) { | ||
return { | ||
[Symbol.iterator]: (): void => {}, | ||
[globalThis.Symbol.iterator]: (): void => {}, | ||
}; | ||
} | ||
|
||
// Incorrect | ||
|
||
export namespace Foo { | ||
const Symbol = {}; | ||
const globalThis = {}; | ||
|
||
export const foo = { | ||
[Symbol.iterator]: (): void => {}, | ||
[globalThis.Symbol.iterator]: (): void => {}, | ||
}; | ||
} | ||
|
||
export function bar(Symbol: {}, globalThis: {}) { | ||
return { | ||
[Symbol.iterator]: (): void => {}, | ||
[globalThis.Symbol.iterator]: (): void => {}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters