Skip to content

Commit ed5bcd7

Browse files
lollekoPerryvw
authored andcommitted
Added initial support for sourcemaps (#452)
* Added initial support for sourcemaps * Made some adjustments to source maps (#466) * Made some adjustments to source maps * Fixed tests * Removed tsOriginal field from AST nodes * Fixed package issues * Inline source maps * Added sourcemap lualib function * Added override for traceback (#490) * Added override for traceback * Improved sourcemap override * Removed obsolete argument * put traceback override at start of the file after headers * changed 2 underscore identifiers * Added test for sourceMapTraceback * don't enforce prettier linting * use debug.getinfo for file names * Trying to diagnose test issue * No longer check filename in sourcemap test * Another stab at fixing tests * Added sourcemap test, simplified API * Added back dependency that got lost in the merge * Fix prettier complaining * Made appveyor lint * Fixed bug in setNodeOriginal and added test to detect it
1 parent 8fb498a commit ed5bcd7

20 files changed

+758
-229
lines changed

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ test_script:
1919
- node --version
2020
- npm --version
2121
# run tests
22+
- npm run lint
2223
- npm run build
2324
- npm test
2425

build_lualib.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ compile([
2020
"./src/lualib",
2121
"--noHeader",
2222
"true",
23-
...glob.sync("./src/lualib/*.ts"),
23+
...glob.sync("./src/lualib/**/*.ts"),
2424
]);
2525

2626
if (fs.existsSync(bundlePath)) {

package-lock.json

Lines changed: 70 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"node": ">=8.5.0"
4040
},
4141
"dependencies": {
42+
"source-map": "^0.7.3",
4243
"typescript": "^3.3.1"
4344
},
4445
"devDependencies": {

src/CommandLineParser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ const optionDeclarations: {[key: string]: CLIOption<any>} = {
5252
describe: "Disables hoisting.",
5353
type: "boolean",
5454
} as CLIOption<boolean>,
55+
sourceMapTraceback: {
56+
default: false,
57+
describe: "Applies the source map to show source TS files and lines in error tracebacks.",
58+
type: "boolean",
59+
} as CLIOption<boolean>,
5560
};
5661

5762
export const { version } = require("../package.json");

src/Compiler.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from "path";
33
import * as ts from "typescript";
44
import * as CommandLineParser from "./CommandLineParser";
55
import { CompilerOptions, LuaLibImportKind, LuaTarget } from "./CompilerOptions";
6-
import { LuaTranspiler } from "./LuaTranspiler";
6+
import { LuaTranspiler, TranspileResult } from "./LuaTranspiler";
77

88
export function compile(argv: string[]): void {
99
const parseResult = CommandLineParser.parseCommandLine(argv);
@@ -167,7 +167,7 @@ export function transpileString(
167167
options: CompilerOptions = defaultCompilerOptions,
168168
ignoreDiagnostics = false,
169169
filePath = "file.ts"
170-
): string {
170+
): TranspileResult {
171171
const program = createStringCompilerProgram(input, options, filePath);
172172

173173
if (!ignoreDiagnostics) {
@@ -182,7 +182,5 @@ export function transpileString(
182182

183183
const transpiler = new LuaTranspiler(program);
184184

185-
const result = transpiler.transpileSourceFile(program.getSourceFile(filePath));
186-
187-
return result.trim();
185+
return transpiler.transpileSourceFile(program.getSourceFile(filePath));
188186
}

src/CompilerOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface CompilerOptions extends ts.CompilerOptions {
55
luaTarget?: LuaTarget;
66
luaLibImport?: LuaLibImportKind;
77
noHoisting?: boolean;
8+
sourceMapTraceback?: boolean;
89
}
910

1011
export enum LuaLibImportKind {

src/LuaAST.ts

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,9 @@ export type Operator = UnaryOperator | BinaryOperator;
9898

9999
export type SymbolId = number;
100100

101-
// TODO For future sourcemap support?
102101
export interface TextRange {
103-
pos: number;
104-
end: number;
102+
line?: number;
103+
column?: number;
105104
}
106105

107106
export interface Node extends TextRange {
@@ -110,22 +109,25 @@ export interface Node extends TextRange {
110109
}
111110

112111
export function createNode(kind: SyntaxKind, tsOriginal?: ts.Node, parent?: Node): Node {
113-
let pos = -1;
114-
let end = -1;
115-
if (tsOriginal) {
116-
pos = tsOriginal.pos;
117-
end = tsOriginal.end;
112+
const sourcePosition = getSourcePosition(tsOriginal);
113+
if (sourcePosition) {
114+
return {kind, parent, line: sourcePosition.line, column: sourcePosition.column};
115+
} else {
116+
return {kind, parent};
118117
}
119-
return {kind, parent, pos, end};
120118
}
121119

122120
export function cloneNode<T extends Node>(node: T): T {
123121
return Object.assign({}, node);
124122
}
125123

126124
export function setNodeOriginal<T extends Node>(node: T, tsOriginal: ts.Node): T {
127-
node.pos = tsOriginal.pos;
128-
node.end = tsOriginal.end;
125+
const sourcePosition = getSourcePosition(tsOriginal);
126+
if (sourcePosition) {
127+
node.line = sourcePosition.line;
128+
node.column = sourcePosition.column;
129+
}
130+
129131
return node;
130132
}
131133

@@ -136,20 +138,32 @@ export function setParent(node: Node | Node[] | undefined, parent: Node): void
136138
if (Array.isArray(node)) {
137139
node.forEach(n => {
138140
n.parent = parent;
139-
if (n.pos === -1 || n.end === -1) {
140-
n.pos = parent.pos;
141-
n.end = parent.end;
142-
}
143141
});
144142
} else {
145143
node.parent = parent;
146-
if (node.pos === -1 || node.end === -1) {
147-
node.pos = parent.pos;
148-
node.end = parent.end;
149-
}
150144
}
151145
}
152146

147+
function getSourcePosition(sourceNode: ts.Node): TextRange | undefined {
148+
if (sourceNode !== undefined && sourceNode.getSourceFile() !== undefined && sourceNode.pos >= 0) {
149+
150+
const { line, character } = ts.getLineAndCharacterOfPosition(
151+
sourceNode.getSourceFile(),
152+
sourceNode.pos + sourceNode.getLeadingTriviaWidth()
153+
);
154+
155+
return { line, column: character };
156+
}
157+
}
158+
159+
export function getOriginalPos(node: Node): TextRange {
160+
while (node.line === undefined && node.parent !== undefined) {
161+
node = node.parent;
162+
}
163+
164+
return { line: node.line, column: node.column };
165+
}
166+
153167
export interface Block extends Node {
154168
kind: SyntaxKind.Block;
155169
statements?: Statement[];
@@ -812,8 +826,8 @@ export function createIdentifier(
812826
return expression;
813827
}
814828

815-
export function cloneIdentifier(identifier: Identifier): Identifier {
816-
return createIdentifier(identifier.text, undefined, identifier.symbolId);
829+
export function cloneIdentifier(identifier: Identifier, tsOriginal?: ts.Node): Identifier {
830+
return createIdentifier(identifier.text, tsOriginal, identifier.symbolId);
817831
}
818832

819833
export function createAnnonymousIdentifier(tsOriginal?: ts.Node, parent?: Node): Identifier {

src/LuaLib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export enum LuaLibFeature {
3737
Set = "Set",
3838
WeakMap = "WeakMap",
3939
WeakSet = "WeakSet",
40+
SourceMapTraceBack = "SourceMapTraceBack",
4041
StringReplace = "StringReplace",
4142
StringSplit = "StringSplit",
4243
StringConcat = "StringConcat",

0 commit comments

Comments
 (0)