diff --git a/ts2panda/src/base/util.ts b/ts2panda/src/base/util.ts index 3662772e19f0748b2c4bfe4e39561ea0957afe0f..c35f76af6b9b7fd8bddc8a421b67a5fb584a81dc 100644 --- a/ts2panda/src/base/util.ts +++ b/ts2panda/src/base/util.ts @@ -28,6 +28,7 @@ import { LOGD } from "../log"; import { isFunctionLikeDeclaration } from "../syntaxCheckHelper"; import { CmdOptions } from "../cmdOptions"; import { CompilerDriver } from "../compilerDriver"; +import { Recorder } from "src/recorder"; export function containSpreadElement(args?: ts.NodeArray): boolean { if (!args) { @@ -338,4 +339,15 @@ export function transformCommonjsModule(sourceFile: ts.SourceFile) { ))]; return ts.factory.updateSourceFile(sourceFile, newStatements); +} + +export function getScopeOfNodeOrMappingNode(node: ts.Node, recorder: Recorder) { + let nodeScope = null; + if (node == ts.getOriginalNode(node)) { + let mapNearestFunc = recorder.getMappingNode(node); + nodeScope = recorder.getScopeOfNode(mapNearestFunc!); + } else { + nodeScope = recorder.getScopeOfNode(node!); + } + return nodeScope; } \ No newline at end of file diff --git a/ts2panda/src/ignoreSyntaxError.ts b/ts2panda/src/ignoreSyntaxError.ts new file mode 100644 index 0000000000000000000000000000000000000000..a0b647a8387f678b312923ef24f5d17209c9dc51 --- /dev/null +++ b/ts2panda/src/ignoreSyntaxError.ts @@ -0,0 +1,4 @@ +const BigInt_literals_are_not_available_when_targeting_lower_than_ES2020: number = 2737; +export const IGNORE_ERROR_CODE: number[] = [ + BigInt_literals_are_not_available_when_targeting_lower_than_ES2020 +]; \ No newline at end of file diff --git a/ts2panda/src/index.ts b/ts2panda/src/index.ts index 866827b25dd93dc2907a5b0c25728ea2c2624410..b9461f8c3de9553864dc7dd0472c5791acb1cd82 100644 --- a/ts2panda/src/index.ts +++ b/ts2panda/src/index.ts @@ -24,6 +24,7 @@ import { LOGE } from "./log"; import { setGlobalDeclare, setGlobalStrict } from "./strictMode"; import { TypeChecker } from "./typeChecker"; import { setPos, isBase64Str, transformCommonjsModule } from "./base/util"; +import { IGNORE_ERROR_CODE } from './ignoreSyntaxError' function checkIsGlobalDeclaration(sourceFile: ts.SourceFile) { for (let statement of sourceFile.statements) { @@ -65,6 +66,10 @@ function main(fileNames: string[], options: ts.CompilerOptions) { } } + if (checkDiagnosticsError(program)) { + return; + } + let emitResult = program.emit( undefined, undefined, @@ -120,10 +125,31 @@ function main(fileNames: string[], options: ts.CompilerOptions) { .concat(emitResult.diagnostics); allDiagnostics.forEach(diagnostic => { + let ignoerErrorSet = new Set(IGNORE_ERROR_CODE); + if (ignoerErrorSet.has(diagnostic.code)) { + return; + } diag.printDiagnostic(diagnostic); }); } +function checkDiagnosticsError(program: ts.Program) { + let diagnosticsFlag = false; + let allDiagnostics = ts + .getPreEmitDiagnostics(program); + allDiagnostics.forEach(diagnostic => { + let ignoerErrorSet = new Set(IGNORE_ERROR_CODE); + if (ignoerErrorSet.has(diagnostic.code)) { + diagnosticsFlag = false; + return; + } + diagnosticsFlag = true; + diag.printDiagnostic(diagnostic); + }); + + return diagnosticsFlag; +} + function getOutputBinName(node: ts.SourceFile) { let outputBinName = CmdOptions.getOutputBinName(); let fileName = node.fileName.substring(0, node.fileName.lastIndexOf('.')); @@ -250,7 +276,7 @@ namespace Compiler { export let Default: ts.CompilerOptions = { outDir: "../tmp/build", allowJs: true, - noEmitOnError: true, + noEmitOnError: false, noImplicitAny: true, target: ts.ScriptTarget.ES2017, module: ts.ModuleKind.ES2015, diff --git a/ts2panda/src/recorder.ts b/ts2panda/src/recorder.ts index e3ba10ca90838821142f396a2929c62704838865..79167967ee0a64760498d574f81517cd2b0e2889 100644 --- a/ts2panda/src/recorder.ts +++ b/ts2panda/src/recorder.ts @@ -18,7 +18,8 @@ import * as astutils from "./astutils"; import { hasDefaultKeywordModifier, hasExportKeywordModifier, - isAnonymousFunctionDefinition + isAnonymousFunctionDefinition, + getScopeOfNodeOrMappingNode } from "./base/util"; import { CmdOptions } from "./cmdOptions"; import { CompilerDriver } from "./compilerDriver"; @@ -73,6 +74,7 @@ export class Recorder { private importStmts: Array = []; private exportStmts: Array = []; private syntaxCheckStatus: boolean; + private mappingOriginNode: Map = new Map(); constructor(node: ts.Node, scope: Scope, compilerDriver: CompilerDriver, recordType: boolean, isTsFile: boolean, syntaxCheckStatus: boolean) { this.node = node; @@ -88,6 +90,10 @@ export class Recorder { record() { this.setParent(this.node); this.setScopeMap(this.node, this.scope); + if (ts.getOriginalNode(this.node)) { + this.setMappingNode(this.node, ts.getOriginalNode(this.node)); + this.setMappingNode(ts.getOriginalNode(this.node), this.node); + } this.recordInfo(this.node, this.scope); return this.node; } @@ -132,6 +138,10 @@ export class Recorder { } case ts.SyntaxKind.FunctionDeclaration: { let functionScope = this.buildVariableScope(scope, childNode); + if (ts.getOriginalNode(node)) { + this.setMappingNode(childNode, ts.getOriginalNode(childNode)); + this.setMappingNode(ts.getOriginalNode(childNode), childNode); + } let isExport: boolean = false; if (hasExportKeywordModifier(childNode)) { if (!CmdOptions.isModules()) { @@ -158,6 +168,10 @@ export class Recorder { case ts.SyntaxKind.CatchClause: { let localScope = new LocalScope(scope); this.setScopeMap(childNode, localScope); + if (ts.getOriginalNode(childNode)) { + this.setMappingNode(childNode, ts.getOriginalNode(childNode)); + this.setMappingNode(ts.getOriginalNode(childNode), childNode); + } this.recordInfo(childNode, localScope); break; } @@ -166,8 +180,13 @@ export class Recorder { case ts.SyntaxKind.ForStatement: case ts.SyntaxKind.ForInStatement: case ts.SyntaxKind.ForOfStatement: { - let loopScope: LoopScope = new LoopScope(scope);; + let loopScope: LoopScope = new LoopScope(scope); + this.setMappingNode(childNode, ts.getOriginalNode(childNode)); this.setScopeMap(childNode, loopScope); + if (ts.getOriginalNode(childNode)) { + this.setMappingNode(childNode, ts.getOriginalNode(childNode)); + this.setMappingNode(ts.getOriginalNode(childNode), childNode); + } this.recordInfo(childNode, loopScope); break; } @@ -261,6 +280,10 @@ export class Recorder { private recordClassInfo(childNode: ts.ClassLikeDeclaration, scope: Scope, isExport: boolean) { let localScope = new LocalScope(scope); this.setScopeMap(childNode, localScope); + if (ts.getOriginalNode(childNode)) { + this.setMappingNode(childNode, ts.getOriginalNode(childNode)); + this.setMappingNode(ts.getOriginalNode(childNode), childNode); + } let ctor = extractCtorOfClass(childNode); if (!ctor) { AddCtor2Class(this, childNode, localScope); @@ -282,6 +305,10 @@ export class Recorder { functionScope.setParentVariableScope(parentVariableScope); parentVariableScope.addChildVariableScope(functionScope); this.setScopeMap(node, functionScope); + if (ts.getOriginalNode(node)) { + this.setMappingNode(node, ts.getOriginalNode(node)); + this.setMappingNode(ts.getOriginalNode(node), node); + } return functionScope; } @@ -832,4 +859,12 @@ export class Recorder { getFuncNameMap() { return this.funcNameMap; } + + setMappingNode(currentNode: ts.Node, originNode: ts.Node) { + this.mappingOriginNode.set(originNode, currentNode); + } + + getMappingNode(node: ts.Node) { + return this.mappingOriginNode.get(node) + } } diff --git a/ts2panda/src/statement/classStatement.ts b/ts2panda/src/statement/classStatement.ts index ed9498afbf8359bcb57673c151c9b11f60caad3e..bdd0069f1d7b34a626543fd0d63e1bb503cdebdd 100644 --- a/ts2panda/src/statement/classStatement.ts +++ b/ts2panda/src/statement/classStatement.ts @@ -28,7 +28,8 @@ import { getParamLengthOfFunc, hasDefaultKeywordModifier, hasExportKeywordModifier, - isUndefinedIdentifier + isUndefinedIdentifier, + getScopeOfNodeOrMappingNode } from "../base/util"; import { CacheList, getVregisterCache } from "../base/vregisterCache"; import { Compiler } from "../compiler"; @@ -118,7 +119,7 @@ export function compileClassDeclaration(compiler: Compiler, stmt: ts.ClassLikeDe compileUnCompiledProperty(compiler, properties, classReg); pandaGen.loadAccumulator(stmt, classReg); - let classScope = compiler.getRecorder().getScopeOfNode(stmt); + let classScope = getScopeOfNodeOrMappingNode(stmt, compiler.getRecorder()); if (hasExportKeywordModifier(stmt)) { if (stmt.name) { let className = jshelpers.getTextOfIdentifierOrLiteral(stmt.name); @@ -169,7 +170,7 @@ export function AddCtor2Class(recorder: Recorder, classNode: ts.ClassLikeDeclara ctorNode = jshelpers.setParent(ctorNode, classNode)!; ctorNode = ts.setTextRange(ctorNode, classNode); - let parentScope = recorder.getScopeOfNode(classNode); + let parentScope = getScopeOfNodeOrMappingNode(classNode, recorder); let funcScope = recorder.buildVariableScope(scope, ctorNode); funcScope.setParent(parentScope); @@ -177,7 +178,15 @@ export function AddCtor2Class(recorder: Recorder, classNode: ts.ClassLikeDeclara ctorBodyScope.setParent(funcScope); recorder.setScopeMap(ctorNode, funcScope); + if (ts.getOriginalNode(ctorNode)) { + recorder.setMappingNode(ctorNode, ts.getOriginalNode(ctorNode)); + recorder.setMappingNode(ts.getOriginalNode(ctorNode), ctorNode); + } recorder.setScopeMap(ctorNode.body!, ctorBodyScope); + if (ts.getOriginalNode(ctorNode.body!)) { + recorder.setMappingNode(ctorNode.body!, ts.getOriginalNode(ctorNode.body!)); + recorder.setMappingNode(ts.getOriginalNode(ctorNode.body!), ctorNode.body!); + } recorder.recordFuncName(ctorNode); recorder.recordFunctionParameters(ctorNode); @@ -416,7 +425,7 @@ function loadCtorObj(node: ts.CallExpression, compiler: Compiler) { return; } - let nearestFuncScope = recorder.getScopeOfNode(nearestFunc); + let nearestFuncScope = getScopeOfNodeOrMappingNode(nearestFunc, recorder); if (!nearestFuncScope) { return; } @@ -425,7 +434,7 @@ function loadCtorObj(node: ts.CallExpression, compiler: Compiler) { pandaGen.loadAccumulator(node, getVregisterCache(pandaGen, CacheList.FUNC)); } else { let outerFunc = jshelpers.getContainingFunctionDeclaration(nearestFunc); - let outerFuncScope = recorder.getScopeOfNode(outerFunc!); + let outerFuncScope = getScopeOfNodeOrMappingNode(outerFunc, recorder); outerFuncScope.pendingCreateEnv(); let level = 1; while (!ts.isConstructorDeclaration(outerFunc!)) {