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
5 changes: 5 additions & 0 deletions tsc/internal/checker/pseudotypenodebuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tsc/internal/pseudochecker/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions tsc/internal/pseudochecker/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
PseudoTypeKindBoolean
PseudoTypeKindFalse
PseudoTypeKindTrue
PseudoTypeKindRegExp
PseudoTypeKindSingleCallSignature
PseudoTypeKindTuple
PseudoTypeKindObjectLiteral
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {};
Original file line number Diff line number Diff line change
@@ -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))

Original file line number Diff line number Diff line change
@@ -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

Loading
Loading