
committed by
Miško Hevery

parent
bfe077ad64
commit
fc50c77bd3
@ -22,15 +22,17 @@ export class SymbolExtractor {
|
||||
static parse(path: string, contents: string): Symbol[] {
|
||||
const symbols: Symbol[] = [];
|
||||
const source: ts.SourceFile = ts.createSourceFile(path, contents, ts.ScriptTarget.Latest, true);
|
||||
let fnDepth = 0;
|
||||
let fnRecurseDepth = 0;
|
||||
function visitor(child: ts.Node) {
|
||||
// Left for easier debugging.
|
||||
// console.log('>>>', ts.SyntaxKind[child.kind]);
|
||||
switch (child.kind) {
|
||||
case ts.SyntaxKind.FunctionExpression:
|
||||
fnDepth++;
|
||||
if (fnDepth <= 1) {
|
||||
// Only go into function expression once for the outer closure.
|
||||
fnRecurseDepth++;
|
||||
if (fnRecurseDepth <= 1) {
|
||||
ts.forEachChild(child, visitor);
|
||||
}
|
||||
fnRecurseDepth--;
|
||||
break;
|
||||
case ts.SyntaxKind.SourceFile:
|
||||
case ts.SyntaxKind.VariableStatement:
|
||||
@ -44,9 +46,13 @@ export class SymbolExtractor {
|
||||
break;
|
||||
case ts.SyntaxKind.VariableDeclaration:
|
||||
const varDecl = child as ts.VariableDeclaration;
|
||||
if (varDecl.initializer) {
|
||||
if (varDecl.initializer && fnRecurseDepth !== 0) {
|
||||
symbols.push({name: varDecl.name.getText()});
|
||||
}
|
||||
if (fnRecurseDepth == 0 &&
|
||||
isRollupExportSymbol(child.parent as ts.VariableDeclarationList)) {
|
||||
ts.forEachChild(child, visitor);
|
||||
}
|
||||
break;
|
||||
case ts.SyntaxKind.FunctionDeclaration:
|
||||
const funcDecl = child as ts.FunctionDeclaration;
|
||||
@ -114,3 +120,15 @@ function toSymbol(v: string | Symbol): Symbol {
|
||||
function toName(symbol: Symbol): string {
|
||||
return symbol.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detects if VariableDeclarationList is format `var x = function(){}()`;
|
||||
*
|
||||
* Rollup produces this format when it wants to export symbols from a bundle.
|
||||
* @param child
|
||||
*/
|
||||
function isRollupExportSymbol(child: ts.VariableDeclarationList): boolean {
|
||||
if (child.declarations.length !== 1) return false;
|
||||
const decl: ts.VariableDeclaration = child.declarations[0];
|
||||
return !!(decl.initializer && decl.initializer.kind == ts.SyntaxKind.CallExpression);
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
/**
|
||||
* Rollup exports symbols in this particular way. This test demonstrates that we can correctly read
|
||||
* symbols.
|
||||
*/
|
||||
var fooBar = function(exports) {
|
||||
'use strict';
|
||||
// tslint:disable-next-line:no-console
|
||||
console.log('Hello, Alice in Wonderland');
|
||||
var A = function() {
|
||||
function A() {}
|
||||
A.prototype.a = function() { return document.a; };
|
||||
return A;
|
||||
}();
|
||||
// tslint:disable-next-line:no-console
|
||||
console.error(new A().a());
|
||||
exports.A = A;
|
||||
return exports;
|
||||
}({});
|
@ -0,0 +1,3 @@
|
||||
[
|
||||
"A"
|
||||
]
|
Reference in New Issue
Block a user