Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 275897b

Browse files
authored
Fix: Location data for methods and constructors (#357)
1 parent 5fce5e7 commit 275897b

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

lib/convert.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -744,8 +744,9 @@ module.exports = function convert(config) {
744744
case SyntaxKind.SetAccessor:
745745
case SyntaxKind.MethodDeclaration: {
746746

747-
// TODO: double-check that these positions are correct
748-
const methodLoc = ast.getLineAndCharacterOfPosition(node.name.end + 1),
747+
const openingParen = nodeUtils.findNextToken(node.name, ast);
748+
749+
const methodLoc = ast.getLineAndCharacterOfPosition(openingParen.getStart()),
749750
nodeIsMethod = (node.kind === SyntaxKind.MethodDeclaration),
750751
method = {
751752
type: AST_NODE_TYPES.FunctionExpression,
@@ -758,7 +759,7 @@ module.exports = function convert(config) {
758759
loc: {
759760
start: {
760761
line: methodLoc.line + 1,
761-
column: methodLoc.character - 1
762+
column: methodLoc.character
762763
},
763764
end: result.loc.end
764765
}
@@ -865,7 +866,8 @@ module.exports = function convert(config) {
865866
}
866867
};
867868

868-
const constructorIdentifierLoc = ast.getLineAndCharacterOfPosition(firstConstructorToken.getStart()),
869+
const constructorIdentifierLocStart = ast.getLineAndCharacterOfPosition(firstConstructorToken.getStart()),
870+
constructorIdentifierLocEnd = ast.getLineAndCharacterOfPosition(firstConstructorToken.getEnd()),
869871
constructorIsComputed = !!node.name && nodeUtils.isComputedProperty(node.name);
870872

871873
let constructorKey;
@@ -878,12 +880,12 @@ module.exports = function convert(config) {
878880
range: [firstConstructorToken.getStart(), firstConstructorToken.end],
879881
loc: {
880882
start: {
881-
line: constructorIdentifierLoc.line + 1,
882-
column: constructorIdentifierLoc.character
883+
line: constructorIdentifierLocStart.line + 1,
884+
column: constructorIdentifierLocStart.character
883885
},
884886
end: {
885-
line: constructor.loc.start.line,
886-
column: constructor.loc.start.column
887+
line: constructorIdentifierLocEnd.line + 1,
888+
column: constructorIdentifierLocEnd.character
887889
}
888890
}
889891
};
@@ -894,12 +896,12 @@ module.exports = function convert(config) {
894896
range: [firstConstructorToken.getStart(), firstConstructorToken.end],
895897
loc: {
896898
start: {
897-
line: constructorIdentifierLoc.line + 1,
898-
column: constructorIdentifierLoc.character
899+
line: constructorIdentifierLocStart.line + 1,
900+
column: constructorIdentifierLocStart.character
899901
},
900902
end: {
901-
line: constructor.loc.start.line,
902-
column: constructor.loc.start.column
903+
line: constructorIdentifierLocEnd.line + 1,
904+
column: constructorIdentifierLocEnd.character
903905
}
904906
}
905907
};

tests/lib/__snapshots__/ecma-features.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10441,7 +10441,7 @@ Object {
1044110441
"line": 1,
1044210442
},
1044310443
"start": Object {
10444-
"column": 18,
10444+
"column": 19,
1044510445
"line": 1,
1044610446
},
1044710447
},
@@ -18360,7 +18360,7 @@ Object {
1836018360
"key": Object {
1836118361
"loc": Object {
1836218362
"end": Object {
18363-
"column": 21,
18363+
"column": 20,
1836418364
"line": 1,
1836518365
},
1836618366
"start": Object {

tests/lib/__snapshots__/typescript.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ Object {
400400
"key": Object {
401401
"loc": Object {
402402
"end": Object {
403-
"column": 24,
403+
"column": 12,
404404
"line": 2,
405405
},
406406
"start": Object {
@@ -4744,7 +4744,7 @@ Object {
47444744
"line": 6,
47454745
},
47464746
"start": Object {
4747-
"column": 15,
4747+
"column": 16,
47484748
"line": 4,
47494749
},
47504750
},
@@ -4931,7 +4931,7 @@ Object {
49314931
"line": 9,
49324932
},
49334933
"start": Object {
4934-
"column": 18,
4934+
"column": 19,
49354935
"line": 7,
49364936
},
49374937
},

0 commit comments

Comments
 (0)