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

Commit ab0cd70

Browse files
committed
feat: fix parsing comments in jsx
1 parent ed89d79 commit ab0cd70

File tree

2 files changed

+22
-80
lines changed

2 files changed

+22
-80
lines changed

src/convert-comments.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ export function convertComments(
107107
* Create a TypeScript Scanner, with skipTrivia set to false so that
108108
* we can parse the comments
109109
*/
110-
const triviaScanner = ts.createScanner(ast.languageVersion, false, 0, code);
110+
const triviaScanner = ts.createScanner(
111+
ast.languageVersion,
112+
false,
113+
ast.languageVariant,
114+
code
115+
);
111116

112117
let kind = triviaScanner.scan();
113118
while (kind !== ts.SyntaxKind.EndOfFileToken) {
@@ -123,8 +128,21 @@ export function convertComments(
123128
comments.push(comment);
124129
break;
125130
}
131+
case ts.SyntaxKind.GreaterThanToken:
132+
container = nodeUtils.getNodeContainer(ast, start, end);
133+
if (
134+
container &&
135+
container.parent &&
136+
container.parent.kind === ts.SyntaxKind.JsxOpeningElement &&
137+
container.parent.parent &&
138+
container.parent.parent.kind === ts.SyntaxKind.JsxElement
139+
) {
140+
kind = triviaScanner.reScanJsxToken();
141+
continue;
142+
}
143+
break;
126144
case ts.SyntaxKind.CloseBraceToken:
127-
container = nodeUtils.getNodeContainer(ast, start, end) as ts.Node;
145+
container = nodeUtils.getNodeContainer(ast, start, end);
128146

129147
if (
130148
container.kind === ts.SyntaxKind.TemplateMiddle ||

tests/lib/__snapshots__/comments.ts.snap

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3888,28 +3888,7 @@ Object {
38883888
"type": "VariableDeclaration",
38893889
},
38903890
],
3891-
"comments": Array [
3892-
Object {
3893-
"loc": Object {
3894-
"end": Object {
3895-
"column": 9,
3896-
"line": 6,
3897-
},
3898-
"start": Object {
3899-
"column": 6,
3900-
"line": 4,
3901-
},
3902-
},
3903-
"range": Array [
3904-
48,
3905-
75,
3906-
],
3907-
"type": "Block",
3908-
"value": "*
3909-
* test
3910-
",
3911-
},
3912-
],
3891+
"comments": Array [],
39133892
"loc": Object {
39143893
"end": Object {
39153894
"column": 0,
@@ -4519,26 +4498,7 @@ Object {
45194498
"type": "VariableDeclaration",
45204499
},
45214500
],
4522-
"comments": Array [
4523-
Object {
4524-
"loc": Object {
4525-
"end": Object {
4526-
"column": 67,
4527-
"line": 1,
4528-
},
4529-
"start": Object {
4530-
"column": 48,
4531-
"line": 1,
4532-
},
4533-
},
4534-
"range": Array [
4535-
48,
4536-
67,
4537-
],
4538-
"type": "Line",
4539-
"value": "example.com</a>);",
4540-
},
4541-
],
4501+
"comments": Array [],
45424502
"loc": Object {
45434503
"end": Object {
45444504
"column": 0,
@@ -5257,24 +5217,6 @@ Object {
52575217
"type": "Block",
52585218
"value": " Test ",
52595219
},
5260-
Object {
5261-
"loc": Object {
5262-
"end": Object {
5263-
"column": 29,
5264-
"line": 3,
5265-
},
5266-
"start": Object {
5267-
"column": 20,
5268-
"line": 3,
5269-
},
5270-
},
5271-
"range": Array [
5272-
59,
5273-
68,
5274-
],
5275-
"type": "Line",
5276-
"value": "</test>",
5277-
},
52785220
],
52795221
"loc": Object {
52805222
"end": Object {
@@ -6156,24 +6098,6 @@ Object {
61566098
"type": "Block",
61576099
"value": " Test ",
61586100
},
6159-
Object {
6160-
"loc": Object {
6161-
"end": Object {
6162-
"column": 29,
6163-
"line": 3,
6164-
},
6165-
"start": Object {
6166-
"column": 20,
6167-
"line": 3,
6168-
},
6169-
},
6170-
"range": Array [
6171-
59,
6172-
68,
6173-
],
6174-
"type": "Line",
6175-
"value": "</test>",
6176-
},
61776101
],
61786102
"loc": Object {
61796103
"end": Object {

0 commit comments

Comments
 (0)