Skip to content

Commit

Permalink
Recognize more tags as component names
Browse files Browse the repository at this point in the history
Fixes #424
  • Loading branch information
yang committed Feb 28, 2019
1 parent 4593c51 commit c7f33cc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/transformers/JSXTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Options} from "../index";
import NameManager from "../NameManager";
import XHTMLEntities from "../parser/plugins/jsx/xhtml";
import {TokenType as tt} from "../parser/tokenizer/types";
import {charCodes} from "../parser/util/charcodes";
import TokenProcessor from "../TokenProcessor";
import getJSXPragmaInfo, {JSXPragmaInfo} from "../util/getJSXPragmaInfo";
import RootTransformer from "./RootTransformer";
Expand Down Expand Up @@ -251,8 +252,15 @@ export default class JSXTransformer extends Transformer {
}
}

/**
* Spec for identifiers: https://tc39.github.io/ecma262/#prod-IdentifierStart.
*
* Really only treat anything starting with a-z as tag names. `_`, `$`, `é`
* should be treated as copmonent names
*/
export function startsWithLowerCase(s: string): boolean {
return s[0] === s[0].toLowerCase();
const firstChar = s.charCodeAt(0);
return firstChar >= charCodes.lowercaseA && firstChar <= charCodes.lowercaseZ;
}

/**
Expand Down
27 changes: 27 additions & 0 deletions test/jsx-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ describe("transform JSX", () => {
);
});

it("handles more esoteric component names", () => {
assertResult(
`
<_Foo />
`,
`${JSX_PREFIX}
React.createElement(_Foo, {${devProps(2)}} )
`,
);
assertResult(
`
<$ />
`,
`${JSX_PREFIX}
React.createElement($, {${devProps(2)}} )
`,
);
assertResult(
`
<é />
`,
`${JSX_PREFIX}
React.createElement(é, {${devProps(2)}} )
`,
);
});

it("transforms nested JSX elements", () => {
assertResult(
`
Expand Down

0 comments on commit c7f33cc

Please sign in to comment.