Skip to content

Adds support for looking up past Blocks in expando objects #38031

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

Merged
merged 3 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2974,7 +2974,7 @@ namespace ts {

function bindSpecialPropertyAssignment(node: BindablePropertyAssignmentExpression) {
// Class declarations in Typescript do not allow property declarations
const parentSymbol = lookupSymbolForPropertyAccess(node.left.expression);
const parentSymbol = lookupSymbolForPropertyAccess(node.left.expression, container) || lookupSymbolForPropertyAccess(node.left.expression, blockScopeContainer) ;
if (!isInJSFile(node) && !isFunctionSymbol(parentSymbol)) {
return;
}
Expand Down Expand Up @@ -3083,7 +3083,7 @@ namespace ts {
}

function bindPropertyAssignment(name: BindableStaticNameExpression, propertyAccess: BindableStaticAccessExpression, isPrototypeProperty: boolean, containerIsClass: boolean) {
let namespaceSymbol = lookupSymbolForPropertyAccess(name);
let namespaceSymbol = lookupSymbolForPropertyAccess(name, container) || lookupSymbolForPropertyAccess(name, blockScopeContainer);
const isToplevel = isTopLevelNamespaceAssignment(propertyAccess);
namespaceSymbol = bindPotentiallyMissingNamespaces(namespaceSymbol, propertyAccess.expression, isToplevel, isPrototypeProperty, containerIsClass);
bindPotentiallyNewExpandoMemberToNamespace(propertyAccess, namespaceSymbol, isPrototypeProperty);
Expand Down
59 changes: 59 additions & 0 deletions tests/baselines/reference/topLevelBlockExpando.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
tests/cases/compiler/check.js(22,8): error TS2339: Property 'first' does not exist on type '() => number'.
tests/cases/compiler/check.js(23,8): error TS2339: Property 'last' does not exist on type '() => number'.
tests/cases/compiler/check.js(25,17): error TS2345: Argument of type '() => number' is not assignable to parameter of type 'Human'.
Type '() => number' is missing the following properties from type 'Human': first, last


==== tests/cases/compiler/check.ts (0 errors) ====
//



// https://github.com/microsoft/TypeScript/issues/31972
interface Person {
first: string;
last: string;
}

{
const dice = () => Math.floor(Math.random() * 6);
dice.first = 'Rando';
dice.last = 'Calrissian';
const diceP: Person = dice;
}

==== tests/cases/compiler/check.js (3 errors) ====
// Creates a type { first:string, last: string }
/**
* @typedef {Object} Human - creates a new type named 'SpecialType'
* @property {string} first - a string property of SpecialType
* @property {string} last - a number property of SpecialType
*/

/**
* @param {Human} param used as a validation tool
*/
function doHumanThings(param) {}

const dice = () => Math.floor(Math.random() * 6);
dice.first = 'Rando';
dice.last = 'Calrissian';

doHumanThings(dice)

// but inside a block... you can't call iut a human
{
const dice = () => Math.floor(Math.random() * 6);
dice.first = 'Rando';
~~~~~
!!! error TS2339: Property 'first' does not exist on type '() => number'.
dice.last = 'Calrissian';
~~~~
!!! error TS2339: Property 'last' does not exist on type '() => number'.

doHumanThings(dice)
~~~~
!!! error TS2345: Argument of type '() => number' is not assignable to parameter of type 'Human'.
!!! error TS2345: Type '() => number' is missing the following properties from type 'Human': first, last
}

102 changes: 102 additions & 0 deletions tests/baselines/reference/topLevelBlockExpando.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
=== tests/cases/compiler/check.ts ===
//



// https://github.com/microsoft/TypeScript/issues/31972
interface Person {
>Person : Symbol(Person, Decl(check.ts, 0, 0))

first: string;
>first : Symbol(Person.first, Decl(check.ts, 5, 18))

last: string;
>last : Symbol(Person.last, Decl(check.ts, 6, 16))
}

{
const dice = () => Math.floor(Math.random() * 6);
>dice : Symbol(dice, Decl(check.ts, 11, 7))
>Math.floor : Symbol(Math.floor, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>floor : Symbol(Math.floor, Decl(lib.es5.d.ts, --, --))
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))

dice.first = 'Rando';
>dice.first : Symbol(dice.first, Decl(check.ts, 11, 51))
>dice : Symbol(dice, Decl(check.ts, 11, 7))
>first : Symbol(dice.first, Decl(check.ts, 11, 51))

dice.last = 'Calrissian';
>dice.last : Symbol(dice.last, Decl(check.ts, 12, 23))
>dice : Symbol(dice, Decl(check.ts, 11, 7))
>last : Symbol(dice.last, Decl(check.ts, 12, 23))

const diceP: Person = dice;
>diceP : Symbol(diceP, Decl(check.ts, 14, 7))
>Person : Symbol(Person, Decl(check.ts, 0, 0))
>dice : Symbol(dice, Decl(check.ts, 11, 7))
}

=== tests/cases/compiler/check.js ===
// Creates a type { first:string, last: string }
/**
* @typedef {Object} Human - creates a new type named 'SpecialType'
* @property {string} first - a string property of SpecialType
* @property {string} last - a number property of SpecialType
*/

/**
* @param {Human} param used as a validation tool
*/
function doHumanThings(param) {}
>doHumanThings : Symbol(doHumanThings, Decl(check.js, 0, 0))
>param : Symbol(param, Decl(check.js, 10, 23))

const dice = () => Math.floor(Math.random() * 6);
>dice : Symbol(dice, Decl(check.js, 12, 5), Decl(check.js, 12, 49), Decl(check.js, 13, 21))
>Math.floor : Symbol(Math.floor, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>floor : Symbol(Math.floor, Decl(lib.es5.d.ts, --, --))
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))

dice.first = 'Rando';
>dice.first : Symbol(dice.first, Decl(check.js, 12, 49), Decl(check.js, 20, 51))
>dice : Symbol(dice, Decl(check.js, 12, 5), Decl(check.js, 12, 49), Decl(check.js, 13, 21))
>first : Symbol(dice.first, Decl(check.js, 12, 49), Decl(check.js, 20, 51))

dice.last = 'Calrissian';
>dice.last : Symbol(dice.last, Decl(check.js, 13, 21), Decl(check.js, 21, 23))
>dice : Symbol(dice, Decl(check.js, 12, 5), Decl(check.js, 12, 49), Decl(check.js, 13, 21))
>last : Symbol(dice.last, Decl(check.js, 13, 21), Decl(check.js, 21, 23))

doHumanThings(dice)
>doHumanThings : Symbol(doHumanThings, Decl(check.js, 0, 0))
>dice : Symbol(dice, Decl(check.js, 12, 5), Decl(check.js, 12, 49), Decl(check.js, 13, 21))

// but inside a block... you can't call iut a human
{
const dice = () => Math.floor(Math.random() * 6);
>dice : Symbol(dice, Decl(check.js, 20, 7))
>Math.floor : Symbol(Math.floor, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>floor : Symbol(Math.floor, Decl(lib.es5.d.ts, --, --))
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))

dice.first = 'Rando';
>dice : Symbol(dice, Decl(check.js, 20, 7))

dice.last = 'Calrissian';
>dice : Symbol(dice, Decl(check.js, 20, 7))

doHumanThings(dice)
>doHumanThings : Symbol(doHumanThings, Decl(check.js, 0, 0))
>dice : Symbol(dice, Decl(check.js, 20, 7))
}

132 changes: 132 additions & 0 deletions tests/baselines/reference/topLevelBlockExpando.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
=== tests/cases/compiler/check.ts ===
//



// https://github.com/microsoft/TypeScript/issues/31972
interface Person {
first: string;
>first : string

last: string;
>last : string
}

{
const dice = () => Math.floor(Math.random() * 6);
>dice : { (): number; first: string; last: string; }
>() => Math.floor(Math.random() * 6) : { (): number; first: string; last: string; }
>Math.floor(Math.random() * 6) : number
>Math.floor : (x: number) => number
>Math : Math
>floor : (x: number) => number
>Math.random() * 6 : number
>Math.random() : number
>Math.random : () => number
>Math : Math
>random : () => number
>6 : 6

dice.first = 'Rando';
>dice.first = 'Rando' : "Rando"
>dice.first : string
>dice : { (): number; first: string; last: string; }
>first : string
>'Rando' : "Rando"

dice.last = 'Calrissian';
>dice.last = 'Calrissian' : "Calrissian"
>dice.last : string
>dice : { (): number; first: string; last: string; }
>last : string
>'Calrissian' : "Calrissian"

const diceP: Person = dice;
>diceP : Person
>dice : { (): number; first: string; last: string; }
}

=== tests/cases/compiler/check.js ===
// Creates a type { first:string, last: string }
/**
* @typedef {Object} Human - creates a new type named 'SpecialType'
* @property {string} first - a string property of SpecialType
* @property {string} last - a number property of SpecialType
*/

/**
* @param {Human} param used as a validation tool
*/
function doHumanThings(param) {}
>doHumanThings : (param: Human) => void
>param : Human

const dice = () => Math.floor(Math.random() * 6);
>dice : { (): number; first: string; last: string; }
>() => Math.floor(Math.random() * 6) : { (): number; first: string; last: string; }
>Math.floor(Math.random() * 6) : number
>Math.floor : (x: number) => number
>Math : Math
>floor : (x: number) => number
>Math.random() * 6 : number
>Math.random() : number
>Math.random : () => number
>Math : Math
>random : () => number
>6 : 6

dice.first = 'Rando';
>dice.first = 'Rando' : "Rando"
>dice.first : string
>dice : { (): number; first: string; last: string; }
>first : string
>'Rando' : "Rando"

dice.last = 'Calrissian';
>dice.last = 'Calrissian' : "Calrissian"
>dice.last : string
>dice : { (): number; first: string; last: string; }
>last : string
>'Calrissian' : "Calrissian"

doHumanThings(dice)
>doHumanThings(dice) : void
>doHumanThings : (param: Human) => void
>dice : { (): number; first: string; last: string; }

// but inside a block... you can't call iut a human
{
const dice = () => Math.floor(Math.random() * 6);
>dice : () => number
>() => Math.floor(Math.random() * 6) : () => number
>Math.floor(Math.random() * 6) : number
>Math.floor : (x: number) => number
>Math : Math
>floor : (x: number) => number
>Math.random() * 6 : number
>Math.random() : number
>Math.random : () => number
>Math : Math
>random : () => number
>6 : 6

dice.first = 'Rando';
>dice.first = 'Rando' : "Rando"
>dice.first : any
>dice : () => number
>first : any
>'Rando' : "Rando"

dice.last = 'Calrissian';
>dice.last = 'Calrissian' : "Calrissian"
>dice.last : any
>dice : () => number
>last : any
>'Calrissian' : "Calrissian"

doHumanThings(dice)
>doHumanThings(dice) : void
>doHumanThings : (param: Human) => void
>dice : () => number
}

49 changes: 49 additions & 0 deletions tests/cases/compiler/topLevelBlockExpando.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// https://github.com/microsoft/TypeScript/issues/31972

// @allowJs: true
// @noEmit: true
// @checkJs: true

// @filename: check.ts

// https://github.com/microsoft/TypeScript/issues/31972
interface Person {
first: string;
last: string;
}

{
const dice = () => Math.floor(Math.random() * 6);
dice.first = 'Rando';
dice.last = 'Calrissian';
const diceP: Person = dice;
}

// @filename: check.js

// Creates a type { first:string, last: string }
/**
* @typedef {Object} Human - creates a new type named 'SpecialType'
* @property {string} first - a string property of SpecialType
* @property {string} last - a number property of SpecialType
*/

/**
* @param {Human} param used as a validation tool
*/
function doHumanThings(param) {}

const dice = () => Math.floor(Math.random() * 6);
dice.first = 'Rando';
dice.last = 'Calrissian';

doHumanThings(dice)

// but inside a block... you can't call iut a human
{
const dice = () => Math.floor(Math.random() * 6);
dice.first = 'Rando';
dice.last = 'Calrissian';

doHumanThings(dice)
}