Skip to content

Typescript full mode fixes #1295

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 4 commits into from
May 1, 2019
Merged
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
2 changes: 1 addition & 1 deletion javascript/extractor/lib/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "typescript-parser-wrapper",
"private": true,
"dependencies": {
"typescript": "3.4"
"typescript": "3.4.5"
},
"scripts": {
"build": "tsc --project tsconfig.json && rollup -c",
Expand Down
20 changes: 17 additions & 3 deletions javascript/extractor/lib/typescript/src/ast_extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,21 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
}
}

forEachNode(ast, (node: AugmentedNode) => {
// Number of conditional type expressions the visitor is currently inside.
// We disable type extraction inside such type expressions, to avoid complications
// with `infer` types.
let insideConditionalTypes = 0;

visitAstNode(ast);
function visitAstNode(node: AugmentedNode) {
if (node.kind === ts.SyntaxKind.ConditionalType) {
++insideConditionalTypes;
}
ts.forEachChild(node, visitAstNode);
if (node.kind === ts.SyntaxKind.ConditionalType) {
--insideConditionalTypes;
}

// fill in line/column info
if ("pos" in node) {
node.$pos = augmentPos(node.pos, true);
Expand All @@ -176,7 +190,7 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
}
}

if (typeChecker != null) {
if (typeChecker != null && insideConditionalTypes === 0) {
if (isTypedNode(node)) {
let contextualType = isContextuallyTypedNode(node)
? typeChecker.getContextualType(node)
Expand Down Expand Up @@ -250,7 +264,7 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
}
}
}
});
}
}

type NamedNodeWithSymbol = AugmentedNode & (ts.ClassDeclaration | ts.InterfaceDeclaration
Expand Down
4 changes: 3 additions & 1 deletion javascript/extractor/lib/typescript/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,9 @@ function handleOpenProjectCommand(command: OpenProjectCommand) {
function getEffectiveExportTarget(symbol: ts.Symbol) {
if (symbol.exports != null && symbol.exports.has(ts.InternalSymbolName.ExportEquals)) {
let exportAlias = symbol.exports.get(ts.InternalSymbolName.ExportEquals);
return typeChecker.getAliasedSymbol(exportAlias);
if (exportAlias.flags & ts.SymbolFlags.Alias) {
return typeChecker.getAliasedSymbol(exportAlias);
}
}
return symbol;
}
Expand Down
18 changes: 17 additions & 1 deletion javascript/extractor/lib/typescript/src/type_table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,8 +819,24 @@ export class TypeTable {
this.isInShallowTypeContext = false;
}

/**
* Returns the properties of the given type, or `null` if the properties of this
* type could not be computed.
*/
private tryGetProperties(type: ts.Type) {
// Workaround for https://github.com/Microsoft/TypeScript/issues/30845
// Should be safe to remove once that has been fixed.
try {
return type.getProperties();
} catch (e) {
return null;
}
}

private extractProperties(type: ts.Type, id: number) {
for (let symbol of type.getProperties()) {
let props = this.tryGetProperties(type);
if (props == null) return;
for (let symbol of props) {
let propertyType = this.typeChecker.getTypeOfSymbolAtLocation(symbol, this.arbitraryAstNode);
if (propertyType == null) continue;
let propertyTypeId = this.getId(propertyType);
Expand Down
6 changes: 3 additions & 3 deletions javascript/extractor/lib/typescript/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,9 @@ tsutils@^2.12.1:
dependencies:
tslib "^1.8.1"

[email protected]:
version "3.4.3"
resolved "typescript-3.4.3.tgz#0eb320e4ace9b10eadf5bc6103286b0f8b7c224f"
[email protected].5:
version "3.4.5"
resolved "typescript-3.4.5.tgz#2d2618d10bb566572b8d7aad5180d84257d70a99"

wrappy@1:
version "1.0.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Foo {}

declare module 'foo' {
export = new Foo();
}

declare module 'bar' {
import * as baz from "baz";
export = baz;
}

declare module 'baz' {
export class C {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
| "bar" in global scope |
| C in module 'bar' |
| Foo in global scope |
| Foo in tst.ts |
| module 'bar' |
| module 'foo' |
| tst.ts |
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import javascript

from CanonicalName name
select name
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"include": ["."],
"compilerOptions": {
"esModuleInterop": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import self from "./tst";

class Foo {}

export = new Foo();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| Success |
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import javascript

select "Success"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"include": ["."]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

var _myGlobal = this;

module Test {
var global = _myGlobal || {};

export class C {}

export function f(x: C) {
global.field = x || {};
}
}