fix(js-parser): correctly retain keywords inside constructor
MODIFY
internal/formatter/builders/js/patterns/JSBindingIdentifier.ts
internal/formatter/builders/js/patterns/JSBindingIdentifier.ts
* LICENSE file in the root directory of this source tree.
*/
-import {Builder, Token, concat} from "@internal/formatter";
+import {Builder, Token, concat, space} from "@internal/formatter";
import {JSBindingIdentifier} from "@internal/ast";
import JSIdentifier from "../auxiliary/JSIdentifier";
@@ -21,6 +21,7 @@ export default function JSBindingIdentifier(
}
return concat([
+ node.meta?.accessibility ? concat([node.meta.accessibility, space]) : "",
JSIdentifier(builder, node),
printPatternMeta(builder, node, node.meta),
]);

ADD
internal/formatter/test-fixtures/typescript/constructorQualifiers/input.ts
+
+class Foo {
+ constructor(public a: string, private b: string, protected c: string) {
+ console.log(a);
+ console.log(b);
+ console.log(c);
+ }
+}

MODIFY
internal/js-parser/parser/lval.ts
internal/js-parser/parser/lval.ts
@@ -948,6 +948,7 @@ export function parseBindingListItem(
type: "JSPatternMeta",
accessibility,
readonly,
+ typeAnnotation: left.meta?.typeAnnotation,
},
),
},

MODIFY
internal/js-parser/test-fixtures/typescript/class/parameter-properties-binding-patterns/input.test.md
internal/js-parser/test-fixtures/typescript/class/parameter-properties-binding-patterns/input.test.md
@@ -212,6 +212,7 @@ JSRoot {
meta: JSPatternMeta {
accessibility: "public"
readonly: false
+ typeAnnotation: undefined
loc: Object {
filename: "typescript/class/parameter-properties-binding-patterns/input.ts"
end: Object {

MODIFY
internal/js-parser/test-fixtures/typescript/class/parameter-properties/input.test.md
internal/js-parser/test-fixtures/typescript/class/parameter-properties/input.test.md
@@ -205,6 +205,7 @@ JSRoot {
meta: JSPatternMeta {
accessibility: undefined
readonly: true
+ typeAnnotation: undefined
loc: Object {
filename: "typescript/class/parameter-properties/input.ts"
end: Object {
@@ -245,6 +246,19 @@ JSRoot {
line: 4
}
}
+ typeAnnotation: TSNumberKeywordTypeAnnotation {
+ loc: Object {
+ filename: "typescript/class/parameter-properties/input.ts"
+ end: Object {
+ column: 25
+ line: 4
+ }
+ start: Object {
+ column: 19
+ line: 4
+ }
+ }
+ }
}
}
JSBindingIdentifier {
@@ -263,6 +277,7 @@ JSRoot {
meta: JSPatternMeta {
accessibility: "protected"
readonly: false
+ typeAnnotation: undefined
loc: Object {
filename: "typescript/class/parameter-properties/input.ts"
end: Object {
@@ -303,6 +318,19 @@ JSRoot {
line: 6
}
}
+ typeAnnotation: TSNumberKeywordTypeAnnotation {
+ loc: Object {
+ filename: "typescript/class/parameter-properties/input.ts"
+ end: Object {
+ column: 27
+ line: 6
+ }
+ start: Object {
+ column: 21
+ line: 6
+ }
+ }
+ }
}
}
JSBindingIdentifier {
@@ -321,6 +349,7 @@ JSRoot {
meta: JSPatternMeta {
accessibility: "public"
readonly: true
+ typeAnnotation: undefined
loc: Object {
filename: "typescript/class/parameter-properties/input.ts"
end: Object {
@@ -366,6 +395,7 @@ JSRoot {
accessibility: undefined
leadingComments: undefined
readonly: true
+ typeAnnotation: undefined
loc: Object {
filename: "typescript/class/parameter-properties/input.ts"
end: Object {
@@ -423,6 +453,21 @@ JSRoot {
line: 10
}
}
+ right: JSNumericLiteral {
+ value: 0
+ format: undefined
+ loc: Object {
+ filename: "typescript/class/parameter-properties/input.ts"
+ end: Object {
+ column: 29
+ line: 10
+ }
+ start: Object {
+ column: 28
+ line: 10
+ }
+ }
+ }
meta: JSPatternMeta {
accessibility: "public"
readonly: false
@@ -437,22 +482,20 @@ JSRoot {
line: 10
}
}
- }
- right: JSNumericLiteral {
- value: 0
- format: undefined
+ typeAnnotation: TSNumberKeywordTypeAnnotation {
loc: Object {
filename: "typescript/class/parameter-properties/input.ts"
end: Object {
- column: 29
+ column: 25
line: 10
}
start: Object {
- column: 28
+ column: 19
line: 10
}
}
}
+ }
left: JSBindingIdentifier {
name: "y"
loc: Object {

fix(js-parser): correctly retain keywords inside constructor (#1166)