From 8465ab7150780de78069a69a65a877b00e31be2a Mon Sep 17 00:00:00 2001 From: zgy-ian Date: Thu, 7 Oct 2021 16:18:31 +0800 Subject: [PATCH] Modify the transformation of tsc from the before phase to beafter Signed-off-by: zgy-ian --- ts2panda/src/compilerDriver.ts | 4 ++++ ts2panda/src/index.ts | 29 +++++++++++++++++++++++++---- ts2panda/src/recorder.ts | 12 +++++++----- ts2panda/src/syntaxCheckHelper.ts | 4 ++++ 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/ts2panda/src/compilerDriver.ts b/ts2panda/src/compilerDriver.ts index e8fa59c596..087cbcd304 100644 --- a/ts2panda/src/compilerDriver.ts +++ b/ts2panda/src/compilerDriver.ts @@ -143,6 +143,10 @@ export class CompilerDriver { return spArray.reverse(); } + compileForSyntaxCheck(node: ts.SourceFile): void { + this.compilePrologue(node); + } + compile(node: ts.SourceFile): void { if (CmdOptions.showASTStatistics()) { let statics: number[] = new Array(ts.SyntaxKind.Count).fill(0); diff --git a/ts2panda/src/index.ts b/ts2panda/src/index.ts index f778f0a75e..47eb54db2b 100644 --- a/ts2panda/src/index.ts +++ b/ts2panda/src/index.ts @@ -36,11 +36,23 @@ function main(fileNames: string[], options: ts.CompilerOptions) { before: [ (ctx: ts.TransformationContext) => { return (node: ts.SourceFile) => { - let outputBinName = CmdOptions.getOutputBinName(); - let fileName = node.fileName.substring(0, node.fileName.lastIndexOf('.')); - if (fileName != CmdOptions.getInputFileName()) { - outputBinName = fileName + ".abc"; + let outputBinName = getOutputBinName(node); + let compilerDriver = new CompilerDriver(outputBinName); + compilerDriver.compileForSyntaxCheck(node); + return node; + } + } + ], + after: [ + (ctx: ts.TransformationContext) => { + return (node: ts.SourceFile) => { + if (ts.getEmitHelpers(node)) { + const printer: ts.Printer = ts.createPrinter({newLine:ts.NewLineKind.LineFeed}); + const text: string = printer.printNode(ts.EmitHint.Unspecified, node, node); + let newNode = ts.createSourceFile(node.fileName, text, options.target!); + node = newNode; } + let outputBinName = getOutputBinName(node); let compilerDriver = new CompilerDriver(outputBinName); setGlobalStrict(jshelpers.isEffectiveStrictModeSourceFile(node, options)); if (CmdOptions.isVariantBytecode()) { @@ -70,6 +82,15 @@ function main(fileNames: string[], options: ts.CompilerOptions) { }); } +function getOutputBinName(node: ts.SourceFile) { + let outputBinName = CmdOptions.getOutputBinName(); + let fileName = node.fileName.substring(0, node.fileName.lastIndexOf('.')); + if (fileName != CmdOptions.getInputFileName()) { + outputBinName = fileName + ".abc"; + } + return outputBinName; +} + namespace Compiler { export namespace Options { export let Default: ts.CompilerOptions = { diff --git a/ts2panda/src/recorder.ts b/ts2panda/src/recorder.ts index d74afb9d1c..391b4b8c4c 100644 --- a/ts2panda/src/recorder.ts +++ b/ts2panda/src/recorder.ts @@ -42,9 +42,7 @@ import { VariableScope } from "./scope"; import { - AddCtor2Class, - isContainConstruct, - getClassNameForConstructor + AddCtor2Class, getClassNameForConstructor, isContainConstruct } from "./statement/classStatement"; import { checkSyntaxError } from "./syntaxChecker"; import { isGlobalIdentifier } from "./syntaxCheckHelper"; @@ -83,6 +81,10 @@ export class Recorder { private recordInfo(node: ts.Node, scope: Scope) { node.forEachChild(childNode => { + if (childNode!.parent == undefined || childNode.parent.kind != node.kind) { + childNode = jshelpers.setParent(childNode, node)!; + childNode = ts.setTextRange(childNode, node)!; + } checkSyntaxError(childNode); switch (childNode.kind) { case ts.SyntaxKind.FunctionExpression: @@ -217,7 +219,7 @@ export class Recorder { let tmp: Scope | undefined = nearestRefVariableScope.getNearestLexicalScope(); let needCreateLoopEnv: boolean = false; if (nearestDefLexicalScope instanceof LoopScope) { - while(tmp) { + while (tmp) { if (tmp == nearestDefLexicalScope) { needCreateLoopEnv = true; break; @@ -236,7 +238,7 @@ export class Recorder { if (name == "arguments") { let varialbeScope = scope.getNearestVariableScope(); - varialbeScope ?.setUseArgs(true); + varialbeScope?.setUseArgs(true); } } diff --git a/ts2panda/src/syntaxCheckHelper.ts b/ts2panda/src/syntaxCheckHelper.ts index 2c719c38f4..8439b27263 100644 --- a/ts2panda/src/syntaxCheckHelper.ts +++ b/ts2panda/src/syntaxCheckHelper.ts @@ -256,6 +256,10 @@ export function isOptionalParameter(node: ts.ParameterDeclaration): boolean { return true; } + if (!node.parent || !node.parent.parent) { + return false; + } + let iife = jshelpers.getImmediatelyInvokedFunctionExpression(node.parent); if (iife) { return !node.type && !node.dotDotDotToken && node.parent.parameters.indexOf(node) >= iife.arguments.length; -- Gitee