sugar-high 0.5.0 → 0.5.2
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 +32 -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,15 +217,23 @@ function tokenize(code) {
|
|
|
212
217
|
* @returns {number}
|
|
213
218
|
*/
|
|
214
219
|
function classify(token) {
|
|
220
|
+
const isLineBreak = token === '\n'
|
|
221
|
+
// First checking if they're attributes values
|
|
215
222
|
if (inJsxTag() && inStringQuotes()) {
|
|
216
223
|
return T_STRING
|
|
217
224
|
}
|
|
225
|
+
// Then determine if they're jsx literals
|
|
218
226
|
const isJsxLiterals = inJsxLiterals()
|
|
219
227
|
if (isJsxLiterals) {
|
|
220
228
|
return T_JSX_LITERALS
|
|
229
|
+
|
|
230
|
+
}
|
|
231
|
+
// Determine strings first before other types
|
|
232
|
+
if (inStringQuotes()) {
|
|
233
|
+
return T_STRING
|
|
221
234
|
} else if (keywords.has(token)) {
|
|
222
235
|
return last[1] === '.' ? T_IDENTIFIER : T_KEYWORD
|
|
223
|
-
} else if (
|
|
236
|
+
} else if (isLineBreak) {
|
|
224
237
|
return T_BREAK
|
|
225
238
|
} else if (isSpaces(token)) {
|
|
226
239
|
return T_SPACE
|
|
@@ -320,14 +333,18 @@ function tokenize(code) {
|
|
|
320
333
|
|
|
321
334
|
if (__jsxEnter) {
|
|
322
335
|
// <: open tag sign
|
|
336
|
+
// new '<' not inside jsx
|
|
323
337
|
if (!__jsxTag && curr === '<') {
|
|
324
338
|
append()
|
|
325
|
-
__jsxTag = 1
|
|
326
|
-
current = curr
|
|
327
339
|
if (next === '/') {
|
|
340
|
+
// close tag
|
|
328
341
|
__jsxTag = 2
|
|
329
342
|
current = c_n
|
|
330
343
|
i++
|
|
344
|
+
} else {
|
|
345
|
+
// open tag
|
|
346
|
+
__jsxTag = 1
|
|
347
|
+
current = curr
|
|
331
348
|
}
|
|
332
349
|
append(T_SIGN)
|
|
333
350
|
continue
|
|
@@ -350,11 +367,14 @@ function tokenize(code) {
|
|
|
350
367
|
|
|
351
368
|
// >: tag self close sign or close tag sign
|
|
352
369
|
if (c_n === '/>' || c_n === '</') {
|
|
353
|
-
|
|
370
|
+
// if current token is not part of close tag sign, push it first
|
|
371
|
+
if (current !== '<' || current !== '<') {
|
|
372
|
+
append()
|
|
373
|
+
}
|
|
354
374
|
if (c_n === '/>') {
|
|
355
375
|
__jsxTag = 0
|
|
356
|
-
}
|
|
357
|
-
|
|
376
|
+
} else {
|
|
377
|
+
// is '</'
|
|
358
378
|
__jsxStack--
|
|
359
379
|
}
|
|
360
380
|
current = c_n
|
|
@@ -377,7 +397,8 @@ function tokenize(code) {
|
|
|
377
397
|
if (!__jsxTag && (curr === '<' && isIdentifierChar(next) || c_n === '</')) {
|
|
378
398
|
__jsxTag = next === '/' ? 2 : 1
|
|
379
399
|
|
|
380
|
-
|
|
400
|
+
// current and next char can form a jsx open or close tag
|
|
401
|
+
if (curr === '<' && (next === '/' || isAlpha(next))) {
|
|
381
402
|
__jsxEnter = true
|
|
382
403
|
}
|
|
383
404
|
}
|