Skip to content

Commit 552e156

Browse files
authored
Merge pull request #19640 from asgerf/js/no-type-extraction
JS: Disable type extraction
2 parents 1289f14 + 98319ce commit 552e156

File tree

259 files changed

+735
-13381
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

259 files changed

+735
-13381
lines changed

javascript/extractor/lib/typescript/src/ast_extractor.ts

Lines changed: 1 addition & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,13 @@ export interface AugmentedSourceFile extends ts.SourceFile {
1010
/** Internal property that we expose as a workaround. */
1111
redirectInfo?: object | null;
1212
$tokens?: Token[];
13-
$symbol?: number;
1413
$lineStarts?: ReadonlyArray<number>;
1514
}
1615

1716
export interface AugmentedNode extends ts.Node {
1817
$pos?: any;
1918
$end?: any;
2019
$declarationKind?: string;
21-
$type?: number;
22-
$symbol?: number;
23-
$resolvedSignature?: number;
24-
$overloadIndex?: number;
25-
$declaredSignature?: number;
2620
}
2721

2822
export type AugmentedPos = number;
@@ -73,7 +67,7 @@ function tryGetTypeOfNode(typeChecker: ts.TypeChecker, node: AugmentedNode): ts.
7367
} catch (e) {
7468
let sourceFile = node.getSourceFile();
7569
let { line, character } = sourceFile.getLineAndCharacterOfPosition(node.pos);
76-
console.warn(`Could not compute type of ${ts.SyntaxKind[node.kind]} at ${sourceFile.fileName}:${line+1}:${character+1}`);
70+
console.warn(`Could not compute type of ${ts.SyntaxKind[node.kind]} at ${sourceFile.fileName}:${line + 1}:${character + 1}`);
7771
return null;
7872
}
7973
}
@@ -157,17 +151,6 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
157151
});
158152
}
159153

160-
let typeChecker = project && project.program.getTypeChecker();
161-
let typeTable = project && project.typeTable;
162-
163-
// Associate a symbol with the AST node root, in case it is a module.
164-
if (typeTable != null) {
165-
let symbol = typeChecker.getSymbolAtLocation(ast);
166-
if (symbol != null) {
167-
ast.$symbol = typeTable.getSymbolId(symbol);
168-
}
169-
}
170-
171154
visitAstNode(ast);
172155
function visitAstNode(node: AugmentedNode) {
173156
ts.forEachChild(node, visitAstNode);
@@ -190,192 +173,5 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
190173
node.$declarationKind = "var";
191174
}
192175
}
193-
194-
if (typeChecker != null) {
195-
if (isTypedNode(node) && !typeTable.skipExtractingTypes) {
196-
let contextualType = isContextuallyTypedNode(node)
197-
? typeChecker.getContextualType(node)
198-
: null;
199-
let type = contextualType || tryGetTypeOfNode(typeChecker, node);
200-
if (type != null) {
201-
let parent = node.parent;
202-
let unfoldAlias = ts.isTypeAliasDeclaration(parent) && node === parent.type;
203-
let id = typeTable.buildType(type, unfoldAlias);
204-
if (id != null) {
205-
node.$type = id;
206-
}
207-
}
208-
// Extract the target call signature of a function call.
209-
// In case the callee is overloaded or generic, this is not something we can
210-
// derive from the callee type in QL.
211-
if (ts.isCallOrNewExpression(node)) {
212-
let kind = ts.isCallExpression(node) ? ts.SignatureKind.Call : ts.SignatureKind.Construct;
213-
let resolvedSignature = typeChecker.getResolvedSignature(node);
214-
if (resolvedSignature != null) {
215-
let resolvedId = typeTable.getSignatureId(kind, resolvedSignature);
216-
if (resolvedId != null) {
217-
(node as AugmentedNode).$resolvedSignature = resolvedId;
218-
}
219-
let declaration = resolvedSignature.declaration;
220-
if (declaration != null) {
221-
// Find the generic signature, i.e. without call-site type arguments substituted,
222-
// but with overloading resolved.
223-
let calleeType = typeChecker.getTypeAtLocation(node.expression);
224-
if (calleeType != null && declaration != null) {
225-
let calleeSignatures = typeChecker.getSignaturesOfType(calleeType, kind);
226-
for (let i = 0; i < calleeSignatures.length; ++i) {
227-
if (calleeSignatures[i].declaration === declaration) {
228-
(node as AugmentedNode).$overloadIndex = i;
229-
break;
230-
}
231-
}
232-
}
233-
// Extract the symbol so the declaration can be found from QL.
234-
let name = (declaration as any).name;
235-
let symbol = name && typeChecker.getSymbolAtLocation(name);
236-
if (symbol != null) {
237-
(node as AugmentedNode).$symbol = typeTable.getSymbolId(symbol);
238-
}
239-
}
240-
}
241-
}
242-
}
243-
let symbolNode =
244-
isNamedNodeWithSymbol(node) ? node.name :
245-
ts.isImportDeclaration(node) ? node.moduleSpecifier :
246-
ts.isExternalModuleReference(node) ? node.expression :
247-
null;
248-
if (symbolNode != null) {
249-
let symbol = typeChecker.getSymbolAtLocation(symbolNode);
250-
if (symbol != null) {
251-
node.$symbol = typeTable.getSymbolId(symbol);
252-
}
253-
}
254-
if (ts.isTypeReferenceNode(node)) {
255-
// For type references we inject a symbol on each part of the name.
256-
// We traverse each node in the name here since we know these are part of
257-
// a type annotation. This means we don't have to do it for all identifiers
258-
// and qualified names, which would extract more information than we need.
259-
let namePart: (ts.EntityName & AugmentedNode) = node.typeName;
260-
while (ts.isQualifiedName(namePart)) {
261-
let symbol = typeChecker.getSymbolAtLocation(namePart.right);
262-
if (symbol != null) {
263-
namePart.$symbol = typeTable.getSymbolId(symbol);
264-
}
265-
266-
// Traverse into the prefix.
267-
namePart = namePart.left;
268-
}
269-
let symbol = typeChecker.getSymbolAtLocation(namePart);
270-
if (symbol != null) {
271-
namePart.$symbol = typeTable.getSymbolId(symbol);
272-
}
273-
}
274-
if (ts.isFunctionLike(node)) {
275-
let signature = typeChecker.getSignatureFromDeclaration(node);
276-
if (signature != null) {
277-
let kind = ts.isConstructSignatureDeclaration(node) || ts.isConstructorDeclaration(node)
278-
? ts.SignatureKind.Construct : ts.SignatureKind.Call;
279-
let id = typeTable.getSignatureId(kind, signature);
280-
if (id != null) {
281-
(node as AugmentedNode).$declaredSignature = id;
282-
}
283-
}
284-
}
285-
}
286-
}
287-
}
288-
289-
type NamedNodeWithSymbol = AugmentedNode & (ts.ClassDeclaration | ts.InterfaceDeclaration
290-
| ts.TypeAliasDeclaration | ts.EnumDeclaration | ts.EnumMember | ts.ModuleDeclaration | ts.FunctionDeclaration
291-
| ts.MethodDeclaration | ts.MethodSignature);
292-
293-
/**
294-
* True if the given AST node has a name, and should be associated with a symbol.
295-
*/
296-
function isNamedNodeWithSymbol(node: ts.Node): node is NamedNodeWithSymbol {
297-
switch (node.kind) {
298-
case ts.SyntaxKind.ClassDeclaration:
299-
case ts.SyntaxKind.InterfaceDeclaration:
300-
case ts.SyntaxKind.TypeAliasDeclaration:
301-
case ts.SyntaxKind.EnumDeclaration:
302-
case ts.SyntaxKind.EnumMember:
303-
case ts.SyntaxKind.ModuleDeclaration:
304-
case ts.SyntaxKind.FunctionDeclaration:
305-
case ts.SyntaxKind.MethodDeclaration:
306-
case ts.SyntaxKind.MethodSignature:
307-
return true;
308176
}
309-
return false;
310-
}
311-
312-
/**
313-
* True if the given AST node has a type.
314-
*/
315-
function isTypedNode(node: ts.Node): boolean {
316-
switch (node.kind) {
317-
case ts.SyntaxKind.ArrayLiteralExpression:
318-
case ts.SyntaxKind.ArrowFunction:
319-
case ts.SyntaxKind.AsExpression:
320-
case ts.SyntaxKind.SatisfiesExpression:
321-
case ts.SyntaxKind.AwaitExpression:
322-
case ts.SyntaxKind.BinaryExpression:
323-
case ts.SyntaxKind.CallExpression:
324-
case ts.SyntaxKind.ClassExpression:
325-
case ts.SyntaxKind.ClassDeclaration:
326-
case ts.SyntaxKind.CommaListExpression:
327-
case ts.SyntaxKind.ConditionalExpression:
328-
case ts.SyntaxKind.Constructor:
329-
case ts.SyntaxKind.DeleteExpression:
330-
case ts.SyntaxKind.ElementAccessExpression:
331-
case ts.SyntaxKind.ExpressionStatement:
332-
case ts.SyntaxKind.ExpressionWithTypeArguments:
333-
case ts.SyntaxKind.FalseKeyword:
334-
case ts.SyntaxKind.FunctionDeclaration:
335-
case ts.SyntaxKind.FunctionExpression:
336-
case ts.SyntaxKind.GetAccessor:
337-
case ts.SyntaxKind.Identifier:
338-
case ts.SyntaxKind.IndexSignature:
339-
case ts.SyntaxKind.JsxExpression:
340-
case ts.SyntaxKind.LiteralType:
341-
case ts.SyntaxKind.MethodDeclaration:
342-
case ts.SyntaxKind.MethodSignature:
343-
case ts.SyntaxKind.NewExpression:
344-
case ts.SyntaxKind.NonNullExpression:
345-
case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
346-
case ts.SyntaxKind.NumericLiteral:
347-
case ts.SyntaxKind.ObjectKeyword:
348-
case ts.SyntaxKind.ObjectLiteralExpression:
349-
case ts.SyntaxKind.OmittedExpression:
350-
case ts.SyntaxKind.ParenthesizedExpression:
351-
case ts.SyntaxKind.PartiallyEmittedExpression:
352-
case ts.SyntaxKind.PostfixUnaryExpression:
353-
case ts.SyntaxKind.PrefixUnaryExpression:
354-
case ts.SyntaxKind.PropertyAccessExpression:
355-
case ts.SyntaxKind.RegularExpressionLiteral:
356-
case ts.SyntaxKind.SetAccessor:
357-
case ts.SyntaxKind.StringLiteral:
358-
case ts.SyntaxKind.TaggedTemplateExpression:
359-
case ts.SyntaxKind.TemplateExpression:
360-
case ts.SyntaxKind.TemplateHead:
361-
case ts.SyntaxKind.TemplateMiddle:
362-
case ts.SyntaxKind.TemplateSpan:
363-
case ts.SyntaxKind.TemplateTail:
364-
case ts.SyntaxKind.TrueKeyword:
365-
case ts.SyntaxKind.TypeAssertionExpression:
366-
case ts.SyntaxKind.TypeLiteral:
367-
case ts.SyntaxKind.TypeOfExpression:
368-
case ts.SyntaxKind.VoidExpression:
369-
case ts.SyntaxKind.YieldExpression:
370-
return true;
371-
default:
372-
return ts.isTypeNode(node);
373-
}
374-
}
375-
376-
type ContextuallyTypedNode = (ts.ArrayLiteralExpression | ts.ObjectLiteralExpression) & AugmentedNode;
377-
378-
function isContextuallyTypedNode(node: ts.Node): node is ContextuallyTypedNode {
379-
let kind = node.kind;
380-
return kind === ts.SyntaxKind.ArrayLiteralExpression || kind === ts.SyntaxKind.ObjectLiteralExpression;
381177
}
Lines changed: 3 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,26 @@
11
import * as ts from "./typescript";
2-
import { TypeTable } from "./type_table";
3-
import * as pathlib from "path";
4-
import { VirtualSourceRoot } from "./virtual_source_root";
5-
6-
/**
7-
* Extracts the package name from the prefix of an import string.
8-
*/
9-
const packageNameRex = /^(?:@[\w.-]+[/\\]+)?\w[\w.-]*(?=[/\\]|$)/;
10-
const extensions = ['.ts', '.tsx', '.d.ts', '.js', '.jsx'];
11-
12-
function getPackageName(importString: string) {
13-
let packageNameMatch = packageNameRex.exec(importString);
14-
if (packageNameMatch == null) return null;
15-
let packageName = packageNameMatch[0];
16-
if (packageName.charAt(0) === '@') {
17-
packageName = packageName.replace(/[/\\]+/g, '/'); // Normalize slash after the scope.
18-
}
19-
return packageName;
20-
}
212

223
export class Project {
234
public program: ts.Program = null;
245
private host: ts.CompilerHost;
25-
private resolutionCache: ts.ModuleResolutionCache;
266

277
constructor(
28-
public tsConfig: string,
29-
public config: ts.ParsedCommandLine,
30-
public typeTable: TypeTable,
31-
public packageEntryPoints: Map<string, string>,
32-
public virtualSourceRoot: VirtualSourceRoot) {
33-
34-
this.resolveModuleNames = this.resolveModuleNames.bind(this);
8+
public tsConfig: string,
9+
public config: ts.ParsedCommandLine,
10+
public packageEntryPoints: Map<string, string>) {
3511

36-
this.resolutionCache = ts.createModuleResolutionCache(pathlib.dirname(tsConfig), ts.sys.realpath, config.options);
3712
let host = ts.createCompilerHost(config.options, true);
38-
host.resolveModuleNames = this.resolveModuleNames;
3913
host.trace = undefined; // Disable tracing which would otherwise go to standard out
4014
this.host = host;
4115
}
4216

4317
public unload(): void {
44-
this.typeTable.releaseProgram();
4518
this.program = null;
4619
}
4720

4821
public load(): void {
4922
const { config, host } = this;
5023
this.program = ts.createProgram(config.fileNames, config.options, host);
51-
this.typeTable.setProgram(this.program, this.virtualSourceRoot);
5224
}
5325

5426
/**
@@ -60,74 +32,4 @@ export class Project {
6032
this.unload();
6133
this.load();
6234
}
63-
64-
/**
65-
* Override for module resolution in the TypeScript compiler host.
66-
*/
67-
private resolveModuleNames(
68-
moduleNames: string[],
69-
containingFile: string,
70-
reusedNames: string[],
71-
redirectedReference: ts.ResolvedProjectReference,
72-
options: ts.CompilerOptions) {
73-
74-
let oppositePath =
75-
this.virtualSourceRoot.toVirtualPath(containingFile) ||
76-
this.virtualSourceRoot.fromVirtualPath(containingFile);
77-
78-
const { host, resolutionCache } = this;
79-
return moduleNames.map((moduleName) => {
80-
let redirected = this.redirectModuleName(moduleName, containingFile, options);
81-
if (redirected != null) return redirected;
82-
if (oppositePath != null) {
83-
// If the containing file is in the virtual source root, try resolving from the real source root, and vice versa.
84-
redirected = ts.resolveModuleName(moduleName, oppositePath, options, host, resolutionCache).resolvedModule;
85-
if (redirected != null) return redirected;
86-
}
87-
return ts.resolveModuleName(moduleName, containingFile, options, host, resolutionCache).resolvedModule;
88-
});
89-
}
90-
91-
/**
92-
* Returns the path that the given import string should be redirected to, or null if it should
93-
* fall back to standard module resolution.
94-
*/
95-
private redirectModuleName(moduleName: string, containingFile: string, options: ts.CompilerOptions): ts.ResolvedModule {
96-
// Get a package name from the leading part of the module name, e.g. '@scope/foo' from '@scope/foo/bar'.
97-
let packageName = getPackageName(moduleName);
98-
if (packageName == null) return null;
99-
100-
// Get the overridden location of this package, if one exists.
101-
let packageEntryPoint = this.packageEntryPoints.get(packageName);
102-
if (packageEntryPoint == null) return null;
103-
104-
// If the requested module name is exactly the overridden package name,
105-
// return the entry point file (it is not necessarily called `index.ts`).
106-
if (moduleName === packageName) {
107-
return { resolvedFileName: packageEntryPoint, isExternalLibraryImport: true };
108-
}
109-
110-
// Get the suffix after the package name, e.g. the '/bar' in '@scope/foo/bar'.
111-
let suffix = moduleName.substring(packageName.length);
112-
113-
// Resolve the suffix relative to the package directory.
114-
let packageDir = pathlib.dirname(packageEntryPoint);
115-
let joinedPath = pathlib.join(packageDir, suffix);
116-
117-
// Add implicit '/index'
118-
if (ts.sys.directoryExists(joinedPath)) {
119-
joinedPath = pathlib.join(joinedPath, 'index');
120-
}
121-
122-
// Try each recognized extension. We must not return a file whose extension is not
123-
// recognized by TypeScript.
124-
for (let ext of extensions) {
125-
let candidate = joinedPath.endsWith(ext) ? joinedPath : (joinedPath + ext);
126-
if (ts.sys.fileExists(candidate)) {
127-
return { resolvedFileName: candidate, isExternalLibraryImport: true };
128-
}
129-
}
130-
131-
return null;
132-
}
13335
}

0 commit comments

Comments
 (0)