diff --git a/tsc/internal/checker/pseudotypenodebuilder.go b/tsc/internal/checker/pseudotypenodebuilder.go index 8f1e2bf1700e1..b894daa6e6ad7 100644 --- a/tsc/internal/checker/pseudotypenodebuilder.go +++ b/tsc/internal/checker/pseudotypenodebuilder.go @@ -174,6 +174,9 @@ func (b *NodeBuilderImpl) pseudoTypeToNode(t *pseudochecker.PseudoType) *ast.Nod return b.f.NewLiteralTypeNode(b.f.NewKeywordExpression(ast.KindFalseKeyword)) case pseudochecker.PseudoTypeKindTrue: return b.f.NewLiteralTypeNode(b.f.NewKeywordExpression(ast.KindTrueKeyword)) + case pseudochecker.PseudoTypeKindRegExp: + // Serialize the global type instead of a bare `RegExp` identifier so a shadowed global is still referenced correctly (as `globalThis.RegExp`) + return b.typeToTypeNode(b.ch.globalRegExpType) case pseudochecker.PseudoTypeKindSingleCallSignature: d := t.AsPseudoTypeSingleCallSignature() signature := b.ch.getSignatureFromDeclaration(d.Signature) @@ -753,6 +756,8 @@ func (b *NodeBuilderImpl) pseudoTypeToType(t *pseudochecker.PseudoType) *Type { return b.ch.falseType case pseudochecker.PseudoTypeKindTrue: return b.ch.trueType + case pseudochecker.PseudoTypeKindRegExp: + return b.ch.globalRegExpType case pseudochecker.PseudoTypeKindStringLiteral, pseudochecker.PseudoTypeKindNumericLiteral, pseudochecker.PseudoTypeKindBigIntLiteral: source := t.AsPseudoTypeLiteral().Node return b.ch.getRegularTypeOfExpression(source) // big shortcut, uses cached expression types where possible diff --git a/tsc/internal/pseudochecker/lookup.go b/tsc/internal/pseudochecker/lookup.go index 3fc7243df41f5..c31c41e16a08c 100644 --- a/tsc/internal/pseudochecker/lookup.go +++ b/tsc/internal/pseudochecker/lookup.go @@ -308,6 +308,9 @@ func (ch *PseudoChecker) typeFromExpression(node *ast.Node) *PseudoType { return NewPseudoTypeMaybeConstLocation(node, PseudoTypeTrue, PseudoTypeBoolean) case ast.KindFalseKeyword: return NewPseudoTypeMaybeConstLocation(node, PseudoTypeFalse, PseudoTypeBoolean) + case ast.KindRegularExpressionLiteral: + // Regular expression literal types don't exist, so the type is the global `RegExp` even in const contexts + return PseudoTypeRegExp } return NewPseudoTypeInferred(node, false) } diff --git a/tsc/internal/pseudochecker/type.go b/tsc/internal/pseudochecker/type.go index 8cbc43a8afb6a..45bff1a624499 100644 --- a/tsc/internal/pseudochecker/type.go +++ b/tsc/internal/pseudochecker/type.go @@ -33,6 +33,7 @@ const ( PseudoTypeKindBoolean PseudoTypeKindFalse PseudoTypeKindTrue + PseudoTypeKindRegExp PseudoTypeKindSingleCallSignature PseudoTypeKindTuple PseudoTypeKindObjectLiteral @@ -77,6 +78,7 @@ var ( PseudoTypeBoolean = newPseudoType(PseudoTypeKindBoolean, &PseudoTypeBase{}) PseudoTypeFalse = newPseudoType(PseudoTypeKindFalse, &PseudoTypeBase{}) PseudoTypeTrue = newPseudoType(PseudoTypeKindTrue, &PseudoTypeBase{}) + PseudoTypeRegExp = newPseudoType(PseudoTypeKindRegExp, &PseudoTypeBase{}) ) // PseudoTypeDirect directly encodes the type referred to by a given TypeNode diff --git a/tsc/testdata/baselines/reference/compiler/isolatedDeclarationsRegExp.js b/tsc/testdata/baselines/reference/compiler/isolatedDeclarationsRegExp.js new file mode 100644 index 0000000000000..9fa9d9a514c3f --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/isolatedDeclarationsRegExp.js @@ -0,0 +1,117 @@ +//// [tests/cases/compiler/isolatedDeclarationsRegExp.ts] //// + +//// [regexp.ts] +export const re = /ab+c/gi; +export let reLet = /x/; +export var reVar = /y/d; + +export const obj = { + /** A pattern */ + pattern: /\d+/, + nested: { inner: /[a-z]/u }, +}; + +export const constObj = { + pattern: /\d+/, + list: [/a/, /b/], +} as const; + +export function returnsRegExp() { + return /ret/; +} + +export const arrow = () => /arrow/; + +export class C { + field = /field/; + optionalField? = /optional/; + static staticField = /static/; +} + +export function withDefault(pattern = /default/, flags: string): void {} +export const arrowWithDefault = (pattern = /default/, count: number): void => {}; + +//// [shadowed.ts] +class RegExp { + exec(): void {} +} +export const local: RegExp = new RegExp(); +export const global = /not the local class/; + +//// [typeShadowed.ts] +type RegExp = string; +export const alias: RegExp = ""; +export const globalType = /not the alias/; + + +//// [regexp.js] +export const re = /ab+c/gi; +export let reLet = /x/; +export var reVar = /y/d; +export const obj = { + /** A pattern */ + pattern: /\d+/, + nested: { inner: /[a-z]/u }, +}; +export const constObj = { + pattern: /\d+/, + list: [/a/, /b/], +}; +export function returnsRegExp() { + return /ret/; +} +export const arrow = () => /arrow/; +export class C { + field = /field/; + optionalField = /optional/; + static staticField = /static/; +} +export function withDefault(pattern = /default/, flags) { } +export const arrowWithDefault = (pattern = /default/, count) => { }; +//// [shadowed.js] +class RegExp { + exec() { } +} +export const local = new RegExp(); +export const global = /not the local class/; +//// [typeShadowed.js] +export const alias = ""; +export const globalType = /not the alias/; + + +//// [regexp.d.ts] +export declare const re: RegExp; +export declare let reLet: RegExp; +export declare var reVar: RegExp; +export declare const obj: { + /** A pattern */ + pattern: RegExp; + nested: { + inner: RegExp; + }; +}; +export declare const constObj: { + readonly pattern: RegExp; + readonly list: readonly [RegExp, RegExp]; +}; +export declare function returnsRegExp(): RegExp; +export declare const arrow: () => RegExp; +export declare class C { + field: RegExp; + optionalField?: RegExp | undefined; + static staticField: RegExp; +} +export declare function withDefault(pattern: RegExp | undefined, flags: string): void; +export declare const arrowWithDefault: (pattern: RegExp | undefined, count: number) => void; +//// [shadowed.d.ts] +declare class RegExp { + exec(): void; +} +export declare const local: RegExp; +export declare const global: globalThis.RegExp; +export {}; +//// [typeShadowed.d.ts] +type RegExp = string; +export declare const alias: RegExp; +export declare const globalType: globalThis.RegExp; +export {}; diff --git a/tsc/testdata/baselines/reference/compiler/isolatedDeclarationsRegExp.symbols b/tsc/testdata/baselines/reference/compiler/isolatedDeclarationsRegExp.symbols new file mode 100644 index 0000000000000..5b31bf9c82d4b --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/isolatedDeclarationsRegExp.symbols @@ -0,0 +1,95 @@ +//// [tests/cases/compiler/isolatedDeclarationsRegExp.ts] //// + +=== regexp.ts === +export const re = /ab+c/gi; +>re : Symbol(re, Decl(regexp.ts, 0, 12)) + +export let reLet = /x/; +>reLet : Symbol(reLet, Decl(regexp.ts, 1, 10)) + +export var reVar = /y/d; +>reVar : Symbol(reVar, Decl(regexp.ts, 2, 10)) + +export const obj = { +>obj : Symbol(obj, Decl(regexp.ts, 4, 12)) + + /** A pattern */ + pattern: /\d+/, +>pattern : Symbol(pattern, Decl(regexp.ts, 4, 20)) + + nested: { inner: /[a-z]/u }, +>nested : Symbol(nested, Decl(regexp.ts, 6, 19)) +>inner : Symbol(inner, Decl(regexp.ts, 7, 13)) + +}; + +export const constObj = { +>constObj : Symbol(constObj, Decl(regexp.ts, 10, 12)) + + pattern: /\d+/, +>pattern : Symbol(pattern, Decl(regexp.ts, 10, 25)) + + list: [/a/, /b/], +>list : Symbol(list, Decl(regexp.ts, 11, 19)) + +} as const; +>const : Symbol(const) + +export function returnsRegExp() { +>returnsRegExp : Symbol(returnsRegExp, Decl(regexp.ts, 13, 11)) + + return /ret/; +} + +export const arrow = () => /arrow/; +>arrow : Symbol(arrow, Decl(regexp.ts, 19, 12)) + +export class C { +>C : Symbol(C, Decl(regexp.ts, 19, 35)) + + field = /field/; +>field : Symbol(C.field, Decl(regexp.ts, 21, 16)) + + optionalField? = /optional/; +>optionalField : Symbol(C.optionalField, Decl(regexp.ts, 22, 20)) + + static staticField = /static/; +>staticField : Symbol(C.staticField, Decl(regexp.ts, 23, 32)) +} + +export function withDefault(pattern = /default/, flags: string): void {} +>withDefault : Symbol(withDefault, Decl(regexp.ts, 25, 1)) +>pattern : Symbol(pattern, Decl(regexp.ts, 27, 28)) +>flags : Symbol(flags, Decl(regexp.ts, 27, 48)) + +export const arrowWithDefault = (pattern = /default/, count: number): void => {}; +>arrowWithDefault : Symbol(arrowWithDefault, Decl(regexp.ts, 28, 12)) +>pattern : Symbol(pattern, Decl(regexp.ts, 28, 33)) +>count : Symbol(count, Decl(regexp.ts, 28, 53)) + +=== shadowed.ts === +class RegExp { +>RegExp : Symbol(RegExp, Decl(shadowed.ts, 0, 0)) + + exec(): void {} +>exec : Symbol(RegExp.exec, Decl(shadowed.ts, 0, 14)) +} +export const local: RegExp = new RegExp(); +>local : Symbol(local, Decl(shadowed.ts, 3, 12)) +>RegExp : Symbol(RegExp, Decl(shadowed.ts, 0, 0)) +>RegExp : Symbol(RegExp, Decl(shadowed.ts, 0, 0)) + +export const global = /not the local class/; +>global : Symbol(global, Decl(shadowed.ts, 4, 12)) + +=== typeShadowed.ts === +type RegExp = string; +>RegExp : Symbol(RegExp, Decl(typeShadowed.ts, 0, 0)) + +export const alias: RegExp = ""; +>alias : Symbol(alias, Decl(typeShadowed.ts, 1, 12)) +>RegExp : Symbol(RegExp, Decl(typeShadowed.ts, 0, 0)) + +export const globalType = /not the alias/; +>globalType : Symbol(globalType, Decl(typeShadowed.ts, 2, 12)) + diff --git a/tsc/testdata/baselines/reference/compiler/isolatedDeclarationsRegExp.types b/tsc/testdata/baselines/reference/compiler/isolatedDeclarationsRegExp.types new file mode 100644 index 0000000000000..a4e255f11c39d --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/isolatedDeclarationsRegExp.types @@ -0,0 +1,118 @@ +//// [tests/cases/compiler/isolatedDeclarationsRegExp.ts] //// + +=== regexp.ts === +export const re = /ab+c/gi; +>re : RegExp +>/ab+c/gi : RegExp + +export let reLet = /x/; +>reLet : RegExp +>/x/ : RegExp + +export var reVar = /y/d; +>reVar : RegExp +>/y/d : RegExp + +export const obj = { +>obj : { pattern: RegExp; nested: { inner: RegExp; }; } +>{ /** A pattern */ pattern: /\d+/, nested: { inner: /[a-z]/u },} : { pattern: RegExp; nested: { inner: RegExp; }; } + + /** A pattern */ + pattern: /\d+/, +>pattern : RegExp +>/\d+/ : RegExp + + nested: { inner: /[a-z]/u }, +>nested : { inner: RegExp; } +>{ inner: /[a-z]/u } : { inner: RegExp; } +>inner : RegExp +>/[a-z]/u : RegExp + +}; + +export const constObj = { +>constObj : { readonly pattern: RegExp; readonly list: readonly [RegExp, RegExp]; } +>{ pattern: /\d+/, list: [/a/, /b/],} as const : { readonly pattern: RegExp; readonly list: readonly [RegExp, RegExp]; } +>{ pattern: /\d+/, list: [/a/, /b/],} : { readonly pattern: RegExp; readonly list: readonly [RegExp, RegExp]; } + + pattern: /\d+/, +>pattern : RegExp +>/\d+/ : RegExp + + list: [/a/, /b/], +>list : readonly [RegExp, RegExp] +>[/a/, /b/] : readonly [RegExp, RegExp] +>/a/ : RegExp +>/b/ : RegExp + +} as const; + +export function returnsRegExp() { +>returnsRegExp : () => RegExp + + return /ret/; +>/ret/ : RegExp +} + +export const arrow = () => /arrow/; +>arrow : () => RegExp +>() => /arrow/ : () => RegExp +>/arrow/ : RegExp + +export class C { +>C : C + + field = /field/; +>field : RegExp +>/field/ : RegExp + + optionalField? = /optional/; +>optionalField : RegExp | undefined +>/optional/ : RegExp + + static staticField = /static/; +>staticField : RegExp +>/static/ : RegExp +} + +export function withDefault(pattern = /default/, flags: string): void {} +>withDefault : (pattern: RegExp | undefined, flags: string) => void +>pattern : RegExp +>/default/ : RegExp +>flags : string + +export const arrowWithDefault = (pattern = /default/, count: number): void => {}; +>arrowWithDefault : (pattern: RegExp | undefined, count: number) => void +>(pattern = /default/, count: number): void => {} : (pattern: RegExp | undefined, count: number) => void +>pattern : RegExp +>/default/ : RegExp +>count : number + +=== shadowed.ts === +class RegExp { +>RegExp : RegExp + + exec(): void {} +>exec : () => void +} +export const local: RegExp = new RegExp(); +>local : RegExp +>new RegExp() : RegExp +>RegExp : typeof RegExp + +export const global = /not the local class/; +>global : globalThis.RegExp +>/not the local class/ : globalThis.RegExp + +=== typeShadowed.ts === +type RegExp = string; +>RegExp : string + +export const alias: RegExp = ""; +>alias : string +>"" : "" + +export const globalType = /not the alias/; +>globalType : globalThis.RegExp +>/not the alias/ : globalThis.RegExp + diff --git a/tsc/testdata/tests/cases/compiler/isolatedDeclarationsRegExp.ts b/tsc/testdata/tests/cases/compiler/isolatedDeclarationsRegExp.ts new file mode 100644 index 0000000000000..7e943298a8edb --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/isolatedDeclarationsRegExp.ts @@ -0,0 +1,47 @@ +// @isolatedDeclarations: true +// @declaration: true +// @strict: true +// @target: esnext + +// @filename: regexp.ts +export const re = /ab+c/gi; +export let reLet = /x/; +export var reVar = /y/d; + +export const obj = { + /** A pattern */ + pattern: /\d+/, + nested: { inner: /[a-z]/u }, +}; + +export const constObj = { + pattern: /\d+/, + list: [/a/, /b/], +} as const; + +export function returnsRegExp() { + return /ret/; +} + +export const arrow = () => /arrow/; + +export class C { + field = /field/; + optionalField? = /optional/; + static staticField = /static/; +} + +export function withDefault(pattern = /default/, flags: string): void {} +export const arrowWithDefault = (pattern = /default/, count: number): void => {}; + +// @filename: shadowed.ts +class RegExp { + exec(): void {} +} +export const local: RegExp = new RegExp(); +export const global = /not the local class/; + +// @filename: typeShadowed.ts +type RegExp = string; +export const alias: RegExp = ""; +export const globalType = /not the alias/;