Skip to content

Commit e5c1531

Browse files
authored
Merge pull request #12 from chick-p/fix-jsx-attributes
Trim quotes in JSX attributes
2 parents 98642d1 + be5f279 commit e5c1531

File tree

6 files changed

+341
-63
lines changed

6 files changed

+341
-63
lines changed

src/JSXProcessor.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ const extractCommentNodes = (node: ts.Node): TxtNode[] => {
6262
// A list of ts.SyntaxKind that the parsing of comments at the parent node is skipped. Comments should be parsed at the child node.
6363
const ignoredCommentKinds = [ts.SyntaxKind.SourceFile];
6464

65+
const trimQuotes = (str: string) => {
66+
if (str.length < 2) {
67+
return str;
68+
}
69+
if (str[0] !== `"` && str[0] !== `'`) {
70+
return str;
71+
}
72+
if (str[0] !== str[str.length - 1]) {
73+
return str;
74+
}
75+
return str.slice(1, -1);
76+
};
77+
6578
const jsxToAST = (node: ts.Node) => {
6679
const startLineAndCharacter = node
6780
.getSourceFile()
@@ -124,7 +137,7 @@ const jsxToAST = (node: ts.Node) => {
124137
return {
125138
...txtPartialNode,
126139
type: ASTNodeTypes.Str,
127-
value: node.getText(),
140+
value: trimQuotes(node.getText()),
128141
} satisfies TxtTextNode;
129142
}
130143

@@ -158,7 +171,6 @@ class JSXProcessor implements TextlintPluginProcessor {
158171
ts.ScriptTarget.Latest,
159172
true,
160173
);
161-
162174
return jsxToAST(sourceFile) as TxtParentNode;
163175
},
164176
postProcess(messages: any[], filePath?: string) {

test/snapshots/nested_line_comments/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@
239239
}
240240
},
241241
"type": "Str",
242-
"value": "\"str\""
242+
"value": "str"
243243
}
244244
]
245245
}

test/snapshots/string/input.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/snapshots/string/output.json

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const Dialog = () => {
2+
const text = "Hello World";
3+
4+
// @ts-expect-error
5+
return <Dialog
6+
text={text}
7+
alt="Howdy, World"
8+
/>;
9+
};

0 commit comments

Comments
 (0)