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
55 changes: 33 additions & 22 deletions src/LuaAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,25 @@ export interface Node extends TextRange {
}

export function createNode(kind: SyntaxKind, tsOriginal?: ts.Node, parent?: Node): Node {
let line: number | undefined;
let column: number | undefined;
// TODO figure out why tsOriginal.getSourceFile()
// can return udnefined in the first place instead of catching it here
if (tsOriginal && tsOriginal.getSourceFile()) {
const lineAndCharacter = ts.getLineAndCharacterOfPosition(tsOriginal.getSourceFile(), tsOriginal.pos);
line = lineAndCharacter.line;
column = lineAndCharacter.character;
const sourcePosition = getSourcePosition(tsOriginal);
if (sourcePosition) {
return {kind, parent, line: sourcePosition.line, column: sourcePosition.column};
} else {
return {kind, parent};
}
return {kind, parent, line, column};
}

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

export function setNodeOriginal<T extends Node>(node: T, tsOriginal: ts.Node): T {
const lineAndCharacter = ts.getLineAndCharacterOfPosition(tsOriginal.getSourceFile(), tsOriginal.pos);
node.line = lineAndCharacter.line;
node.column = lineAndCharacter.character;
const sourcePosition = getSourcePosition(tsOriginal);
if (sourcePosition) {
node.line = sourcePosition.line;
node.line = sourcePosition.line;
}

return node;
}

Expand All @@ -140,20 +139,32 @@ export function setParent(node: Node | Node[] | undefined, parent: Node): void
if (Array.isArray(node)) {
node.forEach(n => {
n.parent = parent;
if (!n.line || !n.column) {
n.line = parent.line;
n.column = parent.column;
}
});
} else {
node.parent = parent;
if (!node.line || !node.column) {
node.line = parent.line;
node.column = parent.column;
}
}
}

function getSourcePosition(sourceNode: ts.Node): TextRange | undefined {
if (sourceNode !== undefined && sourceNode.getSourceFile() !== undefined && sourceNode.pos >= 0) {

const { line, character } = ts.getLineAndCharacterOfPosition(
sourceNode.getSourceFile(),
sourceNode.pos + sourceNode.getLeadingTriviaWidth()
);

return { line, column: character };
}
}

export function getOriginalPos(node: Node): TextRange {
while (node.line === undefined && node.parent !== undefined) {
node = node.parent;
}

return { line: node.line, column: node.column };
}

export interface Block extends Node {
kind: SyntaxKind.Block;
statements?: Statement[];
Expand Down Expand Up @@ -816,8 +827,8 @@ export function createIdentifier(
return expression;
}

export function cloneIdentifier(identifier: Identifier): Identifier {
return createIdentifier(identifier.text, undefined, identifier.symbolId);
export function cloneIdentifier(identifier: Identifier, tsOriginal?: ts.Node): Identifier {
return createIdentifier(identifier.text, tsOriginal, identifier.symbolId);
}

export function createAnnonymousIdentifier(tsOriginal?: ts.Node, parent?: Node): Identifier {
Expand Down
Loading