From c7f33cc9c4fed9d3feccf45f9a2a09b287cfed26 Mon Sep 17 00:00:00 2001 From: Yang Zhang Date: Wed, 27 Feb 2019 18:30:46 -0800 Subject: [PATCH] Recognize more tags as component names Fixes #424 --- src/transformers/JSXTransformer.ts | 10 +++++++++- test/jsx-test.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/transformers/JSXTransformer.ts b/src/transformers/JSXTransformer.ts index 09bac056..7cb91561 100644 --- a/src/transformers/JSXTransformer.ts +++ b/src/transformers/JSXTransformer.ts @@ -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"; @@ -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; } /** diff --git a/test/jsx-test.ts b/test/jsx-test.ts index d8f237ba..8094069b 100644 --- a/test/jsx-test.ts +++ b/test/jsx-test.ts @@ -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( `