sugar-high 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.mjs +25 -11
- package/package.json +1 -1
package/lib/index.mjs
CHANGED
|
@@ -141,8 +141,12 @@ function hasUnicode(s) {
|
|
|
141
141
|
return /[^\u0000-\u007f]/.test(s);
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
+
function isAlpha(chr) {
|
|
145
|
+
return /^[a-zA-Z]$/.test(chr)
|
|
146
|
+
}
|
|
147
|
+
|
|
144
148
|
function isIdentifierChar(chr) {
|
|
145
|
-
return
|
|
149
|
+
return isAlpha(chr) || hasUnicode(chr)
|
|
146
150
|
}
|
|
147
151
|
|
|
148
152
|
function isIdentifier(str) {
|
|
@@ -182,12 +186,13 @@ function tokenize(code) {
|
|
|
182
186
|
/** @type {Array<[number, string]>} */
|
|
183
187
|
const tokens = []
|
|
184
188
|
|
|
189
|
+
/** @type boolean if entered jsx tag, inside <open tag> or </close tag> */
|
|
185
190
|
let __jsxEnter = false
|
|
186
191
|
/**
|
|
187
192
|
* @type {0 | 1 | 2}
|
|
188
|
-
* 0 for not in jsx
|
|
189
|
-
* 1 for open jsx tag
|
|
190
|
-
* 2 for closing jsx tag
|
|
193
|
+
* 0 for not in jsx;
|
|
194
|
+
* 1 for open jsx tag;
|
|
195
|
+
* 2 for closing jsx tag;
|
|
191
196
|
**/
|
|
192
197
|
let __jsxTag = 0
|
|
193
198
|
let __jsxExpr = false
|
|
@@ -212,6 +217,7 @@ function tokenize(code) {
|
|
|
212
217
|
* @returns {number}
|
|
213
218
|
*/
|
|
214
219
|
function classify(token) {
|
|
220
|
+
const isLineBreak = token === '\n'
|
|
215
221
|
if (inJsxTag() && inStringQuotes()) {
|
|
216
222
|
return T_STRING
|
|
217
223
|
}
|
|
@@ -220,7 +226,7 @@ function tokenize(code) {
|
|
|
220
226
|
return T_JSX_LITERALS
|
|
221
227
|
} else if (keywords.has(token)) {
|
|
222
228
|
return last[1] === '.' ? T_IDENTIFIER : T_KEYWORD
|
|
223
|
-
} else if (
|
|
229
|
+
} else if (isLineBreak) {
|
|
224
230
|
return T_BREAK
|
|
225
231
|
} else if (isSpaces(token)) {
|
|
226
232
|
return T_SPACE
|
|
@@ -320,14 +326,18 @@ function tokenize(code) {
|
|
|
320
326
|
|
|
321
327
|
if (__jsxEnter) {
|
|
322
328
|
// <: open tag sign
|
|
329
|
+
// new '<' not inside jsx
|
|
323
330
|
if (!__jsxTag && curr === '<') {
|
|
324
331
|
append()
|
|
325
|
-
__jsxTag = 1
|
|
326
|
-
current = curr
|
|
327
332
|
if (next === '/') {
|
|
333
|
+
// close tag
|
|
328
334
|
__jsxTag = 2
|
|
329
335
|
current = c_n
|
|
330
336
|
i++
|
|
337
|
+
} else {
|
|
338
|
+
// open tag
|
|
339
|
+
__jsxTag = 1
|
|
340
|
+
current = curr
|
|
331
341
|
}
|
|
332
342
|
append(T_SIGN)
|
|
333
343
|
continue
|
|
@@ -350,11 +360,14 @@ function tokenize(code) {
|
|
|
350
360
|
|
|
351
361
|
// >: tag self close sign or close tag sign
|
|
352
362
|
if (c_n === '/>' || c_n === '</') {
|
|
353
|
-
|
|
363
|
+
// if current token is not part of close tag sign, push it first
|
|
364
|
+
if (current !== '<' || current !== '<') {
|
|
365
|
+
append()
|
|
366
|
+
}
|
|
354
367
|
if (c_n === '/>') {
|
|
355
368
|
__jsxTag = 0
|
|
356
|
-
}
|
|
357
|
-
|
|
369
|
+
} else {
|
|
370
|
+
// is '</'
|
|
358
371
|
__jsxStack--
|
|
359
372
|
}
|
|
360
373
|
current = c_n
|
|
@@ -377,7 +390,8 @@ function tokenize(code) {
|
|
|
377
390
|
if (!__jsxTag && (curr === '<' && isIdentifierChar(next) || c_n === '</')) {
|
|
378
391
|
__jsxTag = next === '/' ? 2 : 1
|
|
379
392
|
|
|
380
|
-
|
|
393
|
+
// current and next char can form a jsx open or close tag
|
|
394
|
+
if (curr === '<' && (next === '/' || isAlpha(next))) {
|
|
381
395
|
__jsxEnter = true
|
|
382
396
|
}
|
|
383
397
|
}
|