|
1 | 1 | import type { Token as JSToken, JSXToken } from "js-tokens"; |
2 | 2 | import jsTokens from "js-tokens"; |
| 3 | +// We inline this package |
| 4 | +// eslint-disable-next-line import/no-extraneous-dependencies |
| 5 | +import * as charCodes from "charcodes"; |
3 | 6 |
|
4 | 7 | import { |
5 | 8 | isStrictReservedWord, |
@@ -46,16 +49,29 @@ if (process.env.BABEL_8_BREAKING) { |
46 | 49 | token: JSToken | JSXToken, |
47 | 50 | ): InternalTokenType | "uncolored" { |
48 | 51 | if (token.type === "IdentifierName") { |
| 52 | + const tokenValue = token.value; |
49 | 53 | if ( |
50 | | - isKeyword(token.value) || |
51 | | - isStrictReservedWord(token.value, true) || |
52 | | - sometimesKeywords.has(token.value) |
| 54 | + isKeyword(tokenValue) || |
| 55 | + isStrictReservedWord(tokenValue, true) || |
| 56 | + sometimesKeywords.has(tokenValue) |
53 | 57 | ) { |
54 | 58 | return "keyword"; |
55 | 59 | } |
56 | 60 |
|
57 | | - if (token.value[0] !== token.value[0].toLowerCase()) { |
58 | | - return "capitalized"; |
| 61 | + const firstChar = tokenValue.charCodeAt(0); |
| 62 | + if (firstChar < 128) { |
| 63 | + // ASCII characters |
| 64 | + if ( |
| 65 | + firstChar >= charCodes.uppercaseA && |
| 66 | + firstChar <= charCodes.uppercaseZ |
| 67 | + ) { |
| 68 | + return "capitalized"; |
| 69 | + } |
| 70 | + } else { |
| 71 | + const firstChar = String.fromCodePoint(tokenValue.codePointAt(0)); |
| 72 | + if (firstChar !== firstChar.toLowerCase()) { |
| 73 | + return "capitalized"; |
| 74 | + } |
59 | 75 | } |
60 | 76 | } |
61 | 77 |
|
@@ -139,22 +155,24 @@ if (process.env.BABEL_8_BREAKING) { |
139 | 155 | // typing it since the whole block will be removed in Babel 8 |
140 | 156 | const getTokenType = function (token: any, offset: number, text: string) { |
141 | 157 | if (token.type === "name") { |
| 158 | + const tokenValue = token.value; |
142 | 159 | if ( |
143 | | - isKeyword(token.value) || |
144 | | - isStrictReservedWord(token.value, true) || |
145 | | - sometimesKeywords.has(token.value) |
| 160 | + isKeyword(tokenValue) || |
| 161 | + isStrictReservedWord(tokenValue, true) || |
| 162 | + sometimesKeywords.has(tokenValue) |
146 | 163 | ) { |
147 | 164 | return "keyword"; |
148 | 165 | } |
149 | 166 |
|
150 | 167 | if ( |
151 | | - JSX_TAG.test(token.value) && |
| 168 | + JSX_TAG.test(tokenValue) && |
152 | 169 | (text[offset - 1] === "<" || text.slice(offset - 2, offset) === "</") |
153 | 170 | ) { |
154 | 171 | return "jsxIdentifier"; |
155 | 172 | } |
156 | 173 |
|
157 | | - if (token.value[0] !== token.value[0].toLowerCase()) { |
| 174 | + const firstChar = String.fromCodePoint(tokenValue.codePointAt(0)); |
| 175 | + if (firstChar !== firstChar.toLowerCase()) { |
158 | 176 | return "capitalized"; |
159 | 177 | } |
160 | 178 | } |
|
0 commit comments