Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quote shorthand prop keys that contain a hyphen #292

Merged
merged 1 commit into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/transformers/JSXTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,7 @@ export default class JSXTransformer extends Transformer {
this.tokens.appendCode(`, {`);
while (true) {
if (this.tokens.matches2(tt.jsxName, tt.eq)) {
const keyName = this.tokens.identifierName();
if (keyName.includes("-")) {
this.tokens.replaceToken(`'${keyName}'`);
} else {
this.tokens.copyToken();
}
this.processPropKeyName();
this.tokens.replaceToken(": ");
if (this.tokens.matches1(tt.braceL)) {
this.tokens.replaceToken("");
Expand All @@ -114,7 +109,7 @@ export default class JSXTransformer extends Transformer {
this.processStringPropValue();
}
} else if (this.tokens.matches1(tt.jsxName)) {
this.tokens.copyToken();
this.processPropKeyName();
this.tokens.appendCode(": true");
} else if (this.tokens.matches1(tt.braceL)) {
this.tokens.replaceToken("");
Expand All @@ -132,6 +127,15 @@ export default class JSXTransformer extends Transformer {
}
}

processPropKeyName(): void {
const keyName = this.tokens.identifierName();
if (keyName.includes("-")) {
this.tokens.replaceToken(`'${keyName}'`);
} else {
this.tokens.copyToken();
}
}

processStringPropValue(): void {
const token = this.tokens.currentToken();
const valueCode = this.tokens.code.slice(token.start + 1, token.end - 1);
Expand Down
22 changes: 22 additions & 0 deletions test/jsx-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ describe("transform JSX", () => {
);
});

it("handles property keys that require quoting", () => {
assertResult(
`
<A foo-bar={true} />
`,
`${JSX_PREFIX}
React.createElement(A, { 'foo-bar': true, ${devProps(2)}} )
`,
);
});

it("handles shorthand property keys that require quoting", () => {
assertResult(
`
<A foo-bar />
`,
`${JSX_PREFIX}
React.createElement(A, { 'foo-bar': true, ${devProps(2)}} )
`,
);
});

it("handles inline comments", () => {
assertResult(
`
Expand Down