Skip to content

Commit

Permalink
Port #32947 to release-3.6
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham committed Aug 19, 2019
1 parent 03055d2 commit d31bd35
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 13 deletions.
17 changes: 13 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9166,12 +9166,12 @@ namespace ts {
return undefined;
}

function resolveTypeReferenceName(typeReferenceName: EntityNameExpression | EntityName | undefined, meaning: SymbolFlags) {
function resolveTypeReferenceName(typeReferenceName: EntityNameExpression | EntityName | undefined, meaning: SymbolFlags, ignoreErrors?: boolean) {
if (!typeReferenceName) {
return unknownSymbol;
}

return resolveEntityName(typeReferenceName, meaning) || unknownSymbol;
return resolveEntityName(typeReferenceName, meaning, ignoreErrors) || unknownSymbol;
}

function getTypeReferenceType(node: NodeWithTypeArguments, symbol: Symbol): Type {
Expand Down Expand Up @@ -9363,10 +9363,19 @@ namespace ts {
if (!links.resolvedType) {
let symbol: Symbol | undefined;
let type: Type | undefined;
let meaning = SymbolFlags.Type;
const meaning = SymbolFlags.Type;
if (isJSDocTypeReference(node)) {
type = getIntendedTypeFromJSDocTypeReference(node);
meaning |= SymbolFlags.Value;
if (!type) {
symbol = resolveTypeReferenceName(getTypeReferenceName(node), meaning, /*ignoreErrors*/ true);
if (symbol === unknownSymbol) {
symbol = resolveTypeReferenceName(getTypeReferenceName(node), meaning | SymbolFlags.Value);
}
else {
resolveTypeReferenceName(getTypeReferenceName(node), meaning); // Resolve again to mark errors, if any
}
type = getTypeReferenceType(node, symbol);
}
}
if (!type) {
symbol = resolveTypeReferenceName(getTypeReferenceName(node), meaning);
Expand Down
86 changes: 86 additions & 0 deletions tests/baselines/reference/jsEnumTagOnObjectFrozen.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
=== tests/cases/compiler/usage.js ===
const { Thing, useThing, cbThing } = require("./index");
>Thing : Symbol(Thing, Decl(usage.js, 0, 7))
>useThing : Symbol(useThing, Decl(usage.js, 0, 14))
>cbThing : Symbol(cbThing, Decl(usage.js, 0, 24))
>require : Symbol(require)
>"./index" : Symbol("tests/cases/compiler/index", Decl(index.js, 0, 0))

useThing(Thing.a);
>useThing : Symbol(useThing, Decl(usage.js, 0, 14))
>Thing : Symbol(Thing, Decl(usage.js, 0, 7))

/**
* @typedef {Object} LogEntry
* @property {string} type
* @property {number} time
*/

cbThing(type => {
>cbThing : Symbol(cbThing, Decl(usage.js, 0, 24))
>type : Symbol(type, Decl(usage.js, 10, 8))

/** @type {LogEntry} */
const logEntry = {
>logEntry : Symbol(logEntry, Decl(usage.js, 12, 9))

time: Date.now(),
>time : Symbol(time, Decl(usage.js, 12, 22))
>Date.now : Symbol(DateConstructor.now, Decl(lib.es5.d.ts, --, --))
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --))
>now : Symbol(DateConstructor.now, Decl(lib.es5.d.ts, --, --))

type,
>type : Symbol(type, Decl(usage.js, 13, 25))

};
});

=== tests/cases/compiler/index.js ===
/** @enum {string} */
const Thing = Object.freeze({
>Thing : Symbol(Thing, Decl(index.js, 1, 5), Decl(index.js, 0, 4))
>Object.freeze : Symbol(ObjectConstructor.freeze, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>freeze : Symbol(ObjectConstructor.freeze, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

a: "thing",
>a : Symbol(a, Decl(index.js, 1, 29))

b: "chill"
>b : Symbol(b, Decl(index.js, 2, 15))

});

exports.Thing = Thing;
>exports.Thing : Symbol(Thing, Decl(index.js, 4, 3), Decl(index.js, 0, 4))
>exports : Symbol(Thing, Decl(index.js, 4, 3), Decl(index.js, 0, 4))
>Thing : Symbol(Thing, Decl(index.js, 4, 3), Decl(index.js, 0, 4))
>Thing : Symbol(Thing, Decl(index.js, 1, 5), Decl(index.js, 0, 4))

/**
* @param {Thing} x
*/
function useThing(x) {}
>useThing : Symbol(useThing, Decl(index.js, 6, 22))
>x : Symbol(x, Decl(index.js, 11, 18))

exports.useThing = useThing;
>exports.useThing : Symbol(useThing, Decl(index.js, 11, 23))
>exports : Symbol(useThing, Decl(index.js, 11, 23))
>useThing : Symbol(useThing, Decl(index.js, 11, 23))
>useThing : Symbol(useThing, Decl(index.js, 6, 22))

/**
* @param {(x: Thing) => void} x
*/
function cbThing(x) {}
>cbThing : Symbol(cbThing, Decl(index.js, 13, 28))
>x : Symbol(x, Decl(index.js, 18, 17))

exports.cbThing = cbThing;
>exports.cbThing : Symbol(cbThing, Decl(index.js, 18, 22))
>exports : Symbol(cbThing, Decl(index.js, 18, 22))
>cbThing : Symbol(cbThing, Decl(index.js, 18, 22))
>cbThing : Symbol(cbThing, Decl(index.js, 13, 28))

101 changes: 101 additions & 0 deletions tests/baselines/reference/jsEnumTagOnObjectFrozen.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
=== tests/cases/compiler/usage.js ===
const { Thing, useThing, cbThing } = require("./index");
>Thing : any
>useThing : (x: string) => void
>cbThing : (x: (x: string) => void) => void
>require("./index") : typeof import("tests/cases/compiler/index")
>require : any
>"./index" : "./index"

useThing(Thing.a);
>useThing(Thing.a) : void
>useThing : (x: string) => void
>Thing.a : error
>Thing : any
>a : any

/**
* @typedef {Object} LogEntry
* @property {string} type
* @property {number} time
*/

cbThing(type => {
>cbThing(type => { /** @type {LogEntry} */ const logEntry = { time: Date.now(), type, };}) : void
>cbThing : (x: (x: string) => void) => void
>type => { /** @type {LogEntry} */ const logEntry = { time: Date.now(), type, };} : (type: string) => void
>type : string

/** @type {LogEntry} */
const logEntry = {
>logEntry : LogEntry
>{ time: Date.now(), type, } : { time: number; type: string; }

time: Date.now(),
>time : number
>Date.now() : number
>Date.now : () => number
>Date : DateConstructor
>now : () => number

type,
>type : string

};
});

=== tests/cases/compiler/index.js ===
/** @enum {string} */
const Thing = Object.freeze({
>Thing : Readonly<{ a: string; b: string; }>
>Object.freeze({ a: "thing", b: "chill"}) : Readonly<{ a: string; b: string; }>
>Object.freeze : { <T>(a: T[]): readonly T[]; <T extends Function>(f: T): T; <T>(o: T): Readonly<T>; }
>Object : ObjectConstructor
>freeze : { <T>(a: T[]): readonly T[]; <T extends Function>(f: T): T; <T>(o: T): Readonly<T>; }
>{ a: "thing", b: "chill"} : { a: string; b: string; }

a: "thing",
>a : string
>"thing" : "thing"

b: "chill"
>b : string
>"chill" : "chill"

});

exports.Thing = Thing;
>exports.Thing = Thing : Readonly<{ a: string; b: string; }>
>exports.Thing : error
>exports : typeof import("tests/cases/compiler/index")
>Thing : any
>Thing : Readonly<{ a: string; b: string; }>

/**
* @param {Thing} x
*/
function useThing(x) {}
>useThing : (x: string) => void
>x : string

exports.useThing = useThing;
>exports.useThing = useThing : (x: string) => void
>exports.useThing : (x: string) => void
>exports : typeof import("tests/cases/compiler/index")
>useThing : (x: string) => void
>useThing : (x: string) => void

/**
* @param {(x: Thing) => void} x
*/
function cbThing(x) {}
>cbThing : (x: (x: string) => void) => void
>x : (x: string) => void

exports.cbThing = cbThing;
>exports.cbThing = cbThing : (x: (x: string) => void) => void
>exports.cbThing : (x: (x: string) => void) => void
>exports : typeof import("tests/cases/compiler/index")
>cbThing : (x: (x: string) => void) => void
>cbThing : (x: (x: string) => void) => void

45 changes: 45 additions & 0 deletions tests/cases/compiler/jsEnumTagOnObjectFrozen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @noEmit: true
// @checkJs: true
// @allowJs: true
// @filename: index.js
/** @enum {string} */
const Thing = Object.freeze({
a: "thing",
b: "chill"
});

exports.Thing = Thing;

/**
* @param {Thing} x
*/
function useThing(x) {}

exports.useThing = useThing;

/**
* @param {(x: Thing) => void} x
*/
function cbThing(x) {}

exports.cbThing = cbThing;

// @filename: usage.js

const { Thing, useThing, cbThing } = require("./index");

useThing(Thing.a);

/**
* @typedef {Object} LogEntry
* @property {string} type
* @property {number} time
*/

cbThing(type => {
/** @type {LogEntry} */
const logEntry = {
time: Date.now(),
type,
};
});
2 changes: 1 addition & 1 deletion tests/cases/user/axios-src/axios-src
2 changes: 1 addition & 1 deletion tests/cases/user/create-react-app/create-react-app
Submodule create-react-app updated 131 files
2 changes: 1 addition & 1 deletion tests/cases/user/prettier/prettier
Submodule prettier updated 282 files
2 changes: 1 addition & 1 deletion tests/cases/user/puppeteer/puppeteer
2 changes: 1 addition & 1 deletion tests/cases/user/webpack/webpack
Submodule webpack updated 315 files

0 comments on commit d31bd35

Please sign in to comment.