-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Bind toplevel this assignments as global declarations #22891
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
Changes from 4 commits
a0dc554
e58da91
5831e80
59e2617
2050027
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13960,6 +13960,11 @@ namespace ts { | |
if (type && type !== unknownType) { | ||
return getFlowTypeOfReference(node, type); | ||
} | ||
if (isSourceFile(container)) { | ||
// look up in the source file's locals or exports | ||
const parent = getSymbolOfNode(container); | ||
return createAnonymousType(parent, container.commonJsModuleIndicator ? parent.exports : globals, emptyArray, emptyArray, createIndexInfo(anyType, /*isReadonly*/ false), undefined); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am also not sure i understand the need for the indexer.. why not just return a "global" type? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mhegazy Do you want an error on any unknown toplevel this access in Javascript? I don't think we should be stricter than Typescript here. @Andy-MS I'll take a look and see what I can do. A global type seems like a good idea. I don't think the current code is a real performance problem, though, for a couple of reasons:
|
||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
tests/cases/conformance/salsa/a.js(4,1): error TS2304: Cannot find name 'unknown'. | ||
tests/cases/conformance/salsa/b.js(3,1): error TS2304: Cannot find name 'unknown'. | ||
|
||
|
||
==== tests/cases/conformance/salsa/a.js (1 errors) ==== | ||
this.a = 10; | ||
this.a; | ||
a; | ||
unknown; | ||
~~~~~~~ | ||
!!! error TS2304: Cannot find name 'unknown'. | ||
this.unknown; | ||
|
||
// also, improved types for this-prefixed globals like eval: | ||
this.eval('hi'); | ||
|
||
==== tests/cases/conformance/salsa/b.js (1 errors) ==== | ||
this.a; | ||
a; | ||
unknown; | ||
~~~~~~~ | ||
!!! error TS2304: Cannot find name 'unknown'. | ||
this.unknown; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,31 @@ | ||
=== tests/cases/conformance/salsa/a.js === | ||
this.a = 10; | ||
No type information for this code.this.a; | ||
No type information for this code.a; | ||
No type information for this code. | ||
No type information for this code.=== tests/cases/conformance/salsa/b.js === | ||
>this.a : Symbol(a, Decl(a.js, 0, 0)) | ||
>a : Symbol(a, Decl(a.js, 0, 0)) | ||
|
||
this.a; | ||
No type information for this code.a; | ||
No type information for this code. | ||
No type information for this code. | ||
>this.a : Symbol(a, Decl(a.js, 0, 0)) | ||
>a : Symbol(a, Decl(a.js, 0, 0)) | ||
|
||
a; | ||
>a : Symbol(a, Decl(a.js, 0, 0)) | ||
|
||
unknown; | ||
this.unknown; | ||
|
||
// also, improved types for this-prefixed globals like eval: | ||
this.eval('hi'); | ||
>this.eval : Symbol(eval, Decl(lib.d.ts, --, --)) | ||
>eval : Symbol(eval, Decl(lib.d.ts, --, --)) | ||
|
||
=== tests/cases/conformance/salsa/b.js === | ||
this.a; | ||
>this.a : Symbol(a, Decl(a.js, 0, 0)) | ||
>a : Symbol(a, Decl(a.js, 0, 0)) | ||
|
||
a; | ||
>a : Symbol(a, Decl(a.js, 0, 0)) | ||
|
||
unknown; | ||
this.unknown; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,49 @@ | ||
=== tests/cases/conformance/salsa/a.js === | ||
this.a = 10; | ||
>this.a = 10 : 10 | ||
>this.a : any | ||
>this : any | ||
>a : any | ||
>this.a : number | ||
>this : { [x: string]: any; eval(x: string): any; parseInt(s: string, radix?: number): number; parseFloat(string: string): number; isNaN(number: number): boolean; isFinite(number: number): boolean; decodeURI(encodedURI: string): string; decodeURIComponent(encodedURIComponent: string): string; encodeURI(uri: string): string; encodeURIComponent(uriComponent: string): string; escape(string: string): string; unescape(string: string): string; readonly NaN: number; readonly Infinity: number; readonly Object: ObjectConstructor; readonly Function: FunctionConstructor; readonly String: StringConstructor; readonly Boolean: BooleanConstructor; readonly Number: NumberConstructor; readonly Math: Math; readonly Date: DateConstructor; readonly RegExp: RegExpConstructor; readonly Error: ErrorConstructor; readonly EvalError: EvalErrorConstructor; readonly RangeError: RangeErrorConstructor; readonly ReferenceError: ReferenceErrorConstructor; readonly SyntaxError: SyntaxErrorConstructor; readonly TypeError: TypeErrorConstructor; readonly URIError: URIErrorConstructor; readonly JSON: JSON; readonly Array: ArrayConstructor; readonly ArrayBuffer: ArrayBufferConstructor; readonly DataView: DataViewConstructor; readonly Int8Array: Int8ArrayConstructor; readonly Uint8Array: Uint8ArrayConstructor; readonly Uint8ClampedArray: Uint8ClampedArrayConstructor; readonly Int16Array: Int16ArrayConstructor; readonly Uint16Array: Uint16ArrayConstructor; readonly Int32Array: Int32ArrayConstructor; readonly Uint32Array: Uint32ArrayConstructor; readonly Float32Array: Float32ArrayConstructor; readonly Float64Array: Float64ArrayConstructor; Intl: typeof Intl; a: number; undefined: undefined; } | ||
>a : number | ||
>10 : 10 | ||
|
||
this.a; | ||
>this.a : any | ||
>this : any | ||
>a : any | ||
>this.a : number | ||
>this : { [x: string]: any; eval(x: string): any; parseInt(s: string, radix?: number): number; parseFloat(string: string): number; isNaN(number: number): boolean; isFinite(number: number): boolean; decodeURI(encodedURI: string): string; decodeURIComponent(encodedURIComponent: string): string; encodeURI(uri: string): string; encodeURIComponent(uriComponent: string): string; escape(string: string): string; unescape(string: string): string; readonly NaN: number; readonly Infinity: number; readonly Object: ObjectConstructor; readonly Function: FunctionConstructor; readonly String: StringConstructor; readonly Boolean: BooleanConstructor; readonly Number: NumberConstructor; readonly Math: Math; readonly Date: DateConstructor; readonly RegExp: RegExpConstructor; readonly Error: ErrorConstructor; readonly EvalError: EvalErrorConstructor; readonly RangeError: RangeErrorConstructor; readonly ReferenceError: ReferenceErrorConstructor; readonly SyntaxError: SyntaxErrorConstructor; readonly TypeError: TypeErrorConstructor; readonly URIError: URIErrorConstructor; readonly JSON: JSON; readonly Array: ArrayConstructor; readonly ArrayBuffer: ArrayBufferConstructor; readonly DataView: DataViewConstructor; readonly Int8Array: Int8ArrayConstructor; readonly Uint8Array: Uint8ArrayConstructor; readonly Uint8ClampedArray: Uint8ClampedArrayConstructor; readonly Int16Array: Int16ArrayConstructor; readonly Uint16Array: Uint16ArrayConstructor; readonly Int32Array: Int32ArrayConstructor; readonly Uint32Array: Uint32ArrayConstructor; readonly Float32Array: Float32ArrayConstructor; readonly Float64Array: Float64ArrayConstructor; Intl: typeof Intl; a: number; undefined: undefined; } | ||
>a : number | ||
|
||
a; | ||
>a : any | ||
>a : number | ||
|
||
unknown; | ||
>unknown : any | ||
|
||
this.unknown; | ||
>this.unknown : any | ||
>this : { [x: string]: any; eval(x: string): any; parseInt(s: string, radix?: number): number; parseFloat(string: string): number; isNaN(number: number): boolean; isFinite(number: number): boolean; decodeURI(encodedURI: string): string; decodeURIComponent(encodedURIComponent: string): string; encodeURI(uri: string): string; encodeURIComponent(uriComponent: string): string; escape(string: string): string; unescape(string: string): string; readonly NaN: number; readonly Infinity: number; readonly Object: ObjectConstructor; readonly Function: FunctionConstructor; readonly String: StringConstructor; readonly Boolean: BooleanConstructor; readonly Number: NumberConstructor; readonly Math: Math; readonly Date: DateConstructor; readonly RegExp: RegExpConstructor; readonly Error: ErrorConstructor; readonly EvalError: EvalErrorConstructor; readonly RangeError: RangeErrorConstructor; readonly ReferenceError: ReferenceErrorConstructor; readonly SyntaxError: SyntaxErrorConstructor; readonly TypeError: TypeErrorConstructor; readonly URIError: URIErrorConstructor; readonly JSON: JSON; readonly Array: ArrayConstructor; readonly ArrayBuffer: ArrayBufferConstructor; readonly DataView: DataViewConstructor; readonly Int8Array: Int8ArrayConstructor; readonly Uint8Array: Uint8ArrayConstructor; readonly Uint8ClampedArray: Uint8ClampedArrayConstructor; readonly Int16Array: Int16ArrayConstructor; readonly Uint16Array: Uint16ArrayConstructor; readonly Int32Array: Int32ArrayConstructor; readonly Uint32Array: Uint32ArrayConstructor; readonly Float32Array: Float32ArrayConstructor; readonly Float64Array: Float64ArrayConstructor; Intl: typeof Intl; a: number; undefined: undefined; } | ||
>unknown : any | ||
|
||
// also, improved types for this-prefixed globals like eval: | ||
this.eval('hi'); | ||
>this.eval('hi') : any | ||
>this.eval : (x: string) => any | ||
>this : { [x: string]: any; eval(x: string): any; parseInt(s: string, radix?: number): number; parseFloat(string: string): number; isNaN(number: number): boolean; isFinite(number: number): boolean; decodeURI(encodedURI: string): string; decodeURIComponent(encodedURIComponent: string): string; encodeURI(uri: string): string; encodeURIComponent(uriComponent: string): string; escape(string: string): string; unescape(string: string): string; readonly NaN: number; readonly Infinity: number; readonly Object: ObjectConstructor; readonly Function: FunctionConstructor; readonly String: StringConstructor; readonly Boolean: BooleanConstructor; readonly Number: NumberConstructor; readonly Math: Math; readonly Date: DateConstructor; readonly RegExp: RegExpConstructor; readonly Error: ErrorConstructor; readonly EvalError: EvalErrorConstructor; readonly RangeError: RangeErrorConstructor; readonly ReferenceError: ReferenceErrorConstructor; readonly SyntaxError: SyntaxErrorConstructor; readonly TypeError: TypeErrorConstructor; readonly URIError: URIErrorConstructor; readonly JSON: JSON; readonly Array: ArrayConstructor; readonly ArrayBuffer: ArrayBufferConstructor; readonly DataView: DataViewConstructor; readonly Int8Array: Int8ArrayConstructor; readonly Uint8Array: Uint8ArrayConstructor; readonly Uint8ClampedArray: Uint8ClampedArrayConstructor; readonly Int16Array: Int16ArrayConstructor; readonly Uint16Array: Uint16ArrayConstructor; readonly Int32Array: Int32ArrayConstructor; readonly Uint32Array: Uint32ArrayConstructor; readonly Float32Array: Float32ArrayConstructor; readonly Float64Array: Float64ArrayConstructor; Intl: typeof Intl; a: number; undefined: undefined; } | ||
>eval : (x: string) => any | ||
>'hi' : "hi" | ||
|
||
=== tests/cases/conformance/salsa/b.js === | ||
this.a; | ||
>this.a : any | ||
>this : any | ||
>a : any | ||
>this.a : number | ||
>this : { [x: string]: any; eval(x: string): any; parseInt(s: string, radix?: number): number; parseFloat(string: string): number; isNaN(number: number): boolean; isFinite(number: number): boolean; decodeURI(encodedURI: string): string; decodeURIComponent(encodedURIComponent: string): string; encodeURI(uri: string): string; encodeURIComponent(uriComponent: string): string; escape(string: string): string; unescape(string: string): string; readonly NaN: number; readonly Infinity: number; readonly Object: ObjectConstructor; readonly Function: FunctionConstructor; readonly String: StringConstructor; readonly Boolean: BooleanConstructor; readonly Number: NumberConstructor; readonly Math: Math; readonly Date: DateConstructor; readonly RegExp: RegExpConstructor; readonly Error: ErrorConstructor; readonly EvalError: EvalErrorConstructor; readonly RangeError: RangeErrorConstructor; readonly ReferenceError: ReferenceErrorConstructor; readonly SyntaxError: SyntaxErrorConstructor; readonly TypeError: TypeErrorConstructor; readonly URIError: URIErrorConstructor; readonly JSON: JSON; readonly Array: ArrayConstructor; readonly ArrayBuffer: ArrayBufferConstructor; readonly DataView: DataViewConstructor; readonly Int8Array: Int8ArrayConstructor; readonly Uint8Array: Uint8ArrayConstructor; readonly Uint8ClampedArray: Uint8ClampedArrayConstructor; readonly Int16Array: Int16ArrayConstructor; readonly Uint16Array: Uint16ArrayConstructor; readonly Int32Array: Int32ArrayConstructor; readonly Uint32Array: Uint32ArrayConstructor; readonly Float32Array: Float32ArrayConstructor; readonly Float64Array: Float64ArrayConstructor; Intl: typeof Intl; a: number; undefined: undefined; } | ||
>a : number | ||
|
||
a; | ||
>a : any | ||
>a : number | ||
|
||
unknown; | ||
>unknown : any | ||
|
||
this.unknown; | ||
>this.unknown : any | ||
>this : { [x: string]: any; eval(x: string): any; parseInt(s: string, radix?: number): number; parseFloat(string: string): number; isNaN(number: number): boolean; isFinite(number: number): boolean; decodeURI(encodedURI: string): string; decodeURIComponent(encodedURIComponent: string): string; encodeURI(uri: string): string; encodeURIComponent(uriComponent: string): string; escape(string: string): string; unescape(string: string): string; readonly NaN: number; readonly Infinity: number; readonly Object: ObjectConstructor; readonly Function: FunctionConstructor; readonly String: StringConstructor; readonly Boolean: BooleanConstructor; readonly Number: NumberConstructor; readonly Math: Math; readonly Date: DateConstructor; readonly RegExp: RegExpConstructor; readonly Error: ErrorConstructor; readonly EvalError: EvalErrorConstructor; readonly RangeError: RangeErrorConstructor; readonly ReferenceError: ReferenceErrorConstructor; readonly SyntaxError: SyntaxErrorConstructor; readonly TypeError: TypeErrorConstructor; readonly URIError: URIErrorConstructor; readonly JSON: JSON; readonly Array: ArrayConstructor; readonly ArrayBuffer: ArrayBufferConstructor; readonly DataView: DataViewConstructor; readonly Int8Array: Int8ArrayConstructor; readonly Uint8Array: Uint8ArrayConstructor; readonly Uint8ClampedArray: Uint8ClampedArrayConstructor; readonly Int16Array: Int16ArrayConstructor; readonly Uint16Array: Uint16ArrayConstructor; readonly Int32Array: Int32ArrayConstructor; readonly Uint32Array: Uint32ArrayConstructor; readonly Float32Array: Float32ArrayConstructor; readonly Float64Array: Float64ArrayConstructor; Intl: typeof Intl; a: number; undefined: undefined; } | ||
>unknown : any | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
tests/cases/conformance/salsa/mod.js(6,1): error TS2304: Cannot find name 'a'. | ||
|
||
|
||
==== tests/cases/conformance/salsa/use.js (0 errors) ==== | ||
var mod = require('./mod') | ||
mod.a; | ||
|
||
==== tests/cases/conformance/salsa/decl.d.ts (0 errors) ==== | ||
declare var module: { exports: any }; | ||
declare function require(name: string): any; | ||
==== tests/cases/conformance/salsa/mod.js (1 errors) ==== | ||
/// <reference path='./decl.d.ts' /> | ||
module.exports = {}; | ||
this.a = 10; | ||
this.a; // ok | ||
module.exports.a; // should be ok but doesn't have the right type | ||
a; // error: not actually at top-level in a module | ||
~ | ||
!!! error TS2304: Cannot find name 'a'. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
=== tests/cases/conformance/salsa/use.js === | ||
var mod = require('./mod') | ||
>mod : Symbol(mod, Decl(use.js, 0, 3)) | ||
>require : Symbol(require, Decl(decl.d.ts, 0, 37)) | ||
>'./mod' : Symbol("tests/cases/conformance/salsa/mod", Decl(mod.js, 0, 0)) | ||
|
||
mod.a; | ||
>mod.a : Symbol(a, Decl(mod.js, 1, 20)) | ||
>mod : Symbol(mod, Decl(use.js, 0, 3)) | ||
>a : Symbol(a, Decl(mod.js, 1, 20)) | ||
|
||
=== tests/cases/conformance/salsa/decl.d.ts === | ||
declare var module: { exports: any }; | ||
>module : Symbol(module, Decl(decl.d.ts, 0, 11)) | ||
>exports : Symbol(exports, Decl(decl.d.ts, 0, 21)) | ||
|
||
declare function require(name: string): any; | ||
>require : Symbol(require, Decl(decl.d.ts, 0, 37)) | ||
>name : Symbol(name, Decl(decl.d.ts, 1, 25)) | ||
|
||
=== tests/cases/conformance/salsa/mod.js === | ||
/// <reference path='./decl.d.ts' /> | ||
module.exports = {}; | ||
>module.exports : Symbol(exports, Decl(decl.d.ts, 0, 21)) | ||
>module : Symbol(module, Decl(decl.d.ts, 0, 11)) | ||
>exports : Symbol(exports, Decl(decl.d.ts, 0, 21)) | ||
|
||
this.a = 10; | ||
>this.a : Symbol(a, Decl(mod.js, 1, 20)) | ||
>this : Symbol("tests/cases/conformance/salsa/mod", Decl(mod.js, 0, 0)) | ||
>a : Symbol(a, Decl(mod.js, 1, 20)) | ||
|
||
this.a; // ok | ||
>this.a : Symbol(a, Decl(mod.js, 1, 20)) | ||
>this : Symbol("tests/cases/conformance/salsa/mod", Decl(mod.js, 0, 0)) | ||
>a : Symbol(a, Decl(mod.js, 1, 20)) | ||
|
||
module.exports.a; // should be ok but doesn't have the right type | ||
>module.exports : Symbol(exports, Decl(decl.d.ts, 0, 21)) | ||
>module : Symbol(module, Decl(decl.d.ts, 0, 11)) | ||
>exports : Symbol(exports, Decl(decl.d.ts, 0, 21)) | ||
|
||
a; // error: not actually at top-level in a module | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
=== tests/cases/conformance/salsa/use.js === | ||
var mod = require('./mod') | ||
>mod : typeof "tests/cases/conformance/salsa/mod" | ||
>require('./mod') : typeof "tests/cases/conformance/salsa/mod" | ||
>require : (name: string) => any | ||
>'./mod' : "./mod" | ||
|
||
mod.a; | ||
>mod.a : number | ||
>mod : typeof "tests/cases/conformance/salsa/mod" | ||
>a : number | ||
|
||
=== tests/cases/conformance/salsa/decl.d.ts === | ||
declare var module: { exports: any }; | ||
>module : { exports: any; } | ||
>exports : any | ||
|
||
declare function require(name: string): any; | ||
>require : (name: string) => any | ||
>name : string | ||
|
||
=== tests/cases/conformance/salsa/mod.js === | ||
/// <reference path='./decl.d.ts' /> | ||
module.exports = {}; | ||
>module.exports = {} : { [x: string]: any; } | ||
>module.exports : any | ||
>module : { exports: any; } | ||
>exports : any | ||
>{} : { [x: string]: any; } | ||
|
||
this.a = 10; | ||
>this.a = 10 : 10 | ||
>this.a : number | ||
>this : typeof "tests/cases/conformance/salsa/mod" | ||
>a : number | ||
>10 : 10 | ||
|
||
this.a; // ok | ||
>this.a : number | ||
>this : typeof "tests/cases/conformance/salsa/mod" | ||
>a : number | ||
|
||
module.exports.a; // should be ok but doesn't have the right type | ||
>module.exports.a : any | ||
>module.exports : any | ||
>module : { exports: any; } | ||
>exports : any | ||
>a : any | ||
|
||
a; // error: not actually at top-level in a module | ||
>a : any | ||
|
Uh oh!
There was an error while loading. Please reload this page.