Skip to content
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
1 change: 1 addition & 0 deletions src/transformation/context/visitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type NodesBySyntaxKind = {
[ts.SyntaxKind.NullKeyword]: ts.NullLiteral;
[ts.SyntaxKind.SuperKeyword]: ts.SuperExpression;
[ts.SyntaxKind.ThisKeyword]: ts.ThisExpression;
[ts.SyntaxKind.NotEmittedStatement]: ts.NotEmittedStatement;
};

export type ExpressionLikeNode = ts.Expression | ts.QualifiedName | ts.ExternalModuleReference;
Expand Down
16 changes: 2 additions & 14 deletions src/transformation/utils/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,12 @@ export function findFirstNodeAbove<T extends ts.Node>(node: ts.Node, callback: (
}

export function getFirstDeclarationInFile(symbol: ts.Symbol, sourceFile: ts.SourceFile): ts.Declaration | undefined {
const declarations = (symbol.getDeclarations() ?? []).filter(
// TODO: getSourceFile?
declaration => findFirstNodeAbove(declaration, ts.isSourceFile) === sourceFile
);
const originalSourceFile = ts.getParseTreeNode(sourceFile) ?? sourceFile;
const declarations = (symbol.getDeclarations() ?? []).filter(d => d.getSourceFile() === originalSourceFile);

return declarations.length > 0 ? declarations.reduce((p, c) => (p.pos < c.pos ? p : c)) : undefined;
}

export function isFirstDeclaration(context: TransformationContext, node: ts.VariableDeclaration): boolean {
const symbol = context.checker.getSymbolAtLocation(node.name);
if (!symbol) {
return false;
}

const firstDeclaration = getFirstDeclarationInFile(symbol, context.sourceFile);
return firstDeclaration === node;
}

function isStandardLibraryDeclaration(context: TransformationContext, declaration: ts.Declaration): boolean {
const sourceFile = declaration.getSourceFile();
if (!sourceFile) {
Expand Down
1 change: 1 addition & 0 deletions src/transformation/visitors/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export const typescriptVisitors: Visitors = {
[ts.SyntaxKind.NonNullExpression]: (node, context) => context.transformExpression(node.expression),
[ts.SyntaxKind.AsExpression]: transformAssertionExpression,
[ts.SyntaxKind.TypeAssertionExpression]: transformAssertionExpression,
[ts.SyntaxKind.NotEmittedStatement]: () => undefined,
};
22 changes: 22 additions & 0 deletions test/unit/hoisting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,28 @@ test("Hoisting due to reference from hoisted function", () => {
expect(result).toBe("foo");
});

test("Hoisting with synthetic source file node", () => {
util.testModule`
export const foo = bar();
function bar() { return "bar"; }
`
.setCustomTransformers({
before: [
() => sourceFile =>
ts.updateSourceFileNode(
sourceFile,
[ts.createNotEmittedStatement(undefined!), ...sourceFile.statements],
sourceFile.isDeclarationFile,
sourceFile.referencedFiles,
sourceFile.typeReferenceDirectives,
sourceFile.hasNoDefaultLib,
sourceFile.libReferenceDirectives
),
],
})
.expectToMatchJsResult();
});

test("Namespace Hoisting", () => {
const code = `
function bar() {
Expand Down
9 changes: 8 additions & 1 deletion test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ export abstract class TestBuilder {
return this;
}

private customTransformers?: ts.CustomTransformers;
public setCustomTransformers(customTransformers?: ts.CustomTransformers): this {
expect(this.hasProgram).toBe(false);
this.customTransformers = customTransformers;
return this;
}

// Transpilation and execution

public getTsCode(): string {
Expand All @@ -228,7 +235,7 @@ export abstract class TestBuilder {
@memoize
public getLuaResult(): tstl.TranspileResult {
const program = this.getProgram();
const result = tstl.transpile({ program });
const result = tstl.transpile({ program, customTransformers: this.customTransformers });
const diagnostics = ts.sortAndDeduplicateDiagnostics([
...ts.getPreEmitDiagnostics(program),
...result.diagnostics,
Expand Down