From 86913c7c3eb953c60286c936c6cd60d443f88bea Mon Sep 17 00:00:00 2001 From: Reyad Attiyat Date: Thu, 12 Jan 2017 21:55:18 -0600 Subject: [PATCH] Fix: Calculate constructor range using node.parameters.pos (fixes #139) The class constructor method node range can be calculated using the node.parameters.pos variable instead of using result.range and offset. This fixes issues with the space-before-function-paren rule in eslint. --- lib/ast-converter.js | 6 +- ...lass-with-constructor-with-space.result.js | 355 ++++++++++++++++++ .../class-with-constructor-with-space.src.js | 1 + 3 files changed, 358 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/ecma-features/classes/class-with-constructor-with-space.result.js create mode 100644 tests/fixtures/ecma-features/classes/class-with-constructor-with-space.src.js diff --git a/lib/ast-converter.js b/lib/ast-converter.js index 7064f26..865023e 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -1166,9 +1166,7 @@ module.exports = function(ast, extra) { var constructorIsStatic = Boolean(node.flags & ts.NodeFlags.Static), firstConstructorToken = constructorIsStatic ? ts.findNextToken(node.getFirstToken(), ast) : node.getFirstToken(), - constructorOffset = 11, - constructorStartOffset = constructorOffset + firstConstructorToken.getStart() - node.getFirstToken().getStart(), - constructorLoc = ast.getLineAndCharacterOfPosition(result.range[0] + constructorStartOffset), + constructorLoc = ast.getLineAndCharacterOfPosition(node.parameters.pos - 1), constructor = { type: "FunctionExpression", id: null, @@ -1183,7 +1181,7 @@ module.exports = function(ast, extra) { expression: false, async: false, body: convertChild(node.body), - range: [ result.range[0] + constructorStartOffset, result.range[1]], + range: [ node.parameters.pos - 1, result.range[1]], loc: { start: { line: constructorLoc.line + 1, diff --git a/tests/fixtures/ecma-features/classes/class-with-constructor-with-space.result.js b/tests/fixtures/ecma-features/classes/class-with-constructor-with-space.result.js new file mode 100644 index 0000000..1a39251 --- /dev/null +++ b/tests/fixtures/ecma-features/classes/class-with-constructor-with-space.result.js @@ -0,0 +1,355 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 27 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "body": [ + { + "type": "ClassDeclaration", + "range": [ + 0, + 26 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "id": { + "type": "Identifier", + "range": [ + 6, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "name": "A" + }, + "body": { + "type": "ClassBody", + "body": [ + { + "type": "MethodDefinition", + "range": [ + 9, + 25 + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "key": { + "type": "Identifier", + "name": "constructor", + "range": [ + 9, + 20 + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 21 + } + } + }, + "value": { + "type": "FunctionExpression", + "id": null, + "params": [], + "generator": false, + "expression": false, + "async": false, + "body": { + "type": "BlockStatement", + "range": [ + 23, + 25 + ], + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "body": [] + }, + "range": [ + 21, + 25 + ], + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 25 + } + } + }, + "computed": false, + "accessibility": null, + "static": false, + "kind": "constructor" + } + ], + "range": [ + 8, + 26 + ], + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 26 + } + } + }, + "superClass": null, + "implements": [], + "decorators": [] + }, + { + "type": "EmptyStatement", + "range": [ + 26, + 27 + ], + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 27 + } + } + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "class", + "range": [ + 0, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "A", + "range": [ + 6, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 8, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + } + } + }, + { + "type": "Identifier", + "value": "constructor", + "range": [ + 9, + 20 + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 20 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 21, + 22 + ], + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 22 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 22, + 23 + ], + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 23, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 24, + 25 + ], + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 25, + 26 + ], + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 26 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 26, + 27 + ], + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 27 + } + } + } + ] +}; diff --git a/tests/fixtures/ecma-features/classes/class-with-constructor-with-space.src.js b/tests/fixtures/ecma-features/classes/class-with-constructor-with-space.src.js new file mode 100644 index 0000000..af3f316 --- /dev/null +++ b/tests/fixtures/ecma-features/classes/class-with-constructor-with-space.src.js @@ -0,0 +1 @@ +class A {constructor (){}};