From a58893d1e316221c278ff3106e02c47fbf4d8838 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Fri, 4 Jan 2019 10:02:09 -0600 Subject: [PATCH] Ensure modifiers are included in TSParameterProperty ranges (#9276) --- .../babel-parser/src/plugins/typescript.js | 17 +++++++---- .../options.json | 2 +- .../output.json | 4 +-- .../class/parameter-properties/output.json | 28 +++++++++---------- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/packages/babel-parser/src/plugins/typescript.js b/packages/babel-parser/src/plugins/typescript.js index c3c72d8139..0d5d380c30 100644 --- a/packages/babel-parser/src/plugins/typescript.js +++ b/packages/babel-parser/src/plugins/typescript.js @@ -1364,6 +1364,10 @@ export default (superClass: Class): Class => allowModifiers: ?boolean, decorators: N.Decorator[], ): N.Pattern | N.TSParameterProperty { + // Store original location/position to include modifiers in range + const startPos = this.state.start; + const startLoc = this.state.startLoc; + let accessibility: ?N.Accessibility; let readonly = false; if (allowModifiers) { @@ -1375,7 +1379,7 @@ export default (superClass: Class): Class => this.parseAssignableListItemTypes(left); const elt = this.parseMaybeDefault(left.start, left.loc.start, left); if (accessibility || readonly) { - const pp: N.TSParameterProperty = this.startNodeAtNode(elt); + const pp: N.TSParameterProperty = this.startNodeAt(startPos, startLoc); if (decorators.length) { pp.decorators = decorators; } @@ -1389,12 +1393,13 @@ export default (superClass: Class): Class => } pp.parameter = elt; return this.finishNode(pp, "TSParameterProperty"); - } else { - if (decorators.length) { - left.decorators = decorators; - } - return elt; } + + if (decorators.length) { + left.decorators = decorators; + } + + return elt; } parseFunctionBodyAndFinish( diff --git a/packages/babel-parser/test/fixtures/typescript/class/parameter-properties-binding-patterns/options.json b/packages/babel-parser/test/fixtures/typescript/class/parameter-properties-binding-patterns/options.json index 67137f2107..b84b042172 100644 --- a/packages/babel-parser/test/fixtures/typescript/class/parameter-properties-binding-patterns/options.json +++ b/packages/babel-parser/test/fixtures/typescript/class/parameter-properties-binding-patterns/options.json @@ -1,3 +1,3 @@ { - "throws": "A parameter property may not be declared using a binding pattern. (2:23)" + "throws": "A parameter property may not be declared using a binding pattern. (2:16)" } diff --git a/packages/babel-parser/test/fixtures/typescript/class/parameter-properties-with-decorators/output.json b/packages/babel-parser/test/fixtures/typescript/class/parameter-properties-with-decorators/output.json index 22072d89e4..a131ac8e69 100644 --- a/packages/babel-parser/test/fixtures/typescript/class/parameter-properties-with-decorators/output.json +++ b/packages/babel-parser/test/fixtures/typescript/class/parameter-properties-with-decorators/output.json @@ -116,12 +116,12 @@ "params": [ { "type": "TSParameterProperty", - "start": 40, + "start": 31, "end": 49, "loc": { "start": { "line": 2, - "column": 30 + "column": 21 }, "end": { "line": 2, diff --git a/packages/babel-parser/test/fixtures/typescript/class/parameter-properties/output.json b/packages/babel-parser/test/fixtures/typescript/class/parameter-properties/output.json index 8b94730316..219e2a2a60 100644 --- a/packages/babel-parser/test/fixtures/typescript/class/parameter-properties/output.json +++ b/packages/babel-parser/test/fixtures/typescript/class/parameter-properties/output.json @@ -116,12 +116,12 @@ "params": [ { "type": "TSParameterProperty", - "start": 44, + "start": 35, "end": 45, "loc": { "start": { "line": 3, - "column": 17 + "column": 8 }, "end": { "line": 3, @@ -149,12 +149,12 @@ }, { "type": "TSParameterProperty", - "start": 62, + "start": 55, "end": 72, "loc": { "start": { "line": 4, - "column": 15 + "column": 8 }, "end": { "line": 4, @@ -212,12 +212,12 @@ }, { "type": "TSParameterProperty", - "start": 92, + "start": 82, "end": 95, "loc": { "start": { "line": 5, - "column": 18 + "column": 8 }, "end": { "line": 5, @@ -246,12 +246,12 @@ }, { "type": "TSParameterProperty", - "start": 113, + "start": 105, "end": 124, "loc": { "start": { "line": 6, - "column": 16 + "column": 8 }, "end": { "line": 6, @@ -310,12 +310,12 @@ }, { "type": "TSParameterProperty", - "start": 150, + "start": 134, "end": 153, "loc": { "start": { "line": 7, - "column": 24 + "column": 8 }, "end": { "line": 7, @@ -344,12 +344,12 @@ }, { "type": "TSParameterProperty", - "start": 215, + "start": 206, "end": 220, "loc": { "start": { "line": 9, - "column": 17 + "column": 8 }, "end": { "line": 9, @@ -430,12 +430,12 @@ }, { "type": "TSParameterProperty", - "start": 237, + "start": 230, "end": 251, "loc": { "start": { "line": 10, - "column": 15 + "column": 8 }, "end": { "line": 10,