sugar-high 2.4.0 → 2.4.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/core.js CHANGED
@@ -83,7 +83,9 @@ function tokenize(code, options) {
83
83
  const quote = curr
84
84
  const start = i++
85
85
  while (i < code.length) {
86
- if (code[i] === quote && code[i - 1] !== '\\') {
86
+ if (code[i] === '\\') {
87
+ i++
88
+ } else if (code[i] === quote) {
87
89
  i++
88
90
  break
89
91
  }
@@ -168,18 +168,11 @@ function isSign(ch) {
168
168
  }
169
169
 
170
170
  function isWord(chr) {
171
- return /^[\w_]+$/.test(chr) || hasUnicode(chr)
171
+ return /^[\w$\u0080-\uffff]+$/.test(chr || '')
172
172
  }
173
173
 
174
174
  function isCls(str) {
175
- const chr0 = str[0]
176
- return isWord(chr0) &&
177
- chr0 === chr0.toUpperCase() ||
178
- str === 'null'
179
- }
180
-
181
- function hasUnicode(s) {
182
- return /[^\u0000-\u007f]/.test(s);
175
+ return /^[0-9A-Z\p{Lu}]/u.test(str) || str === 'null'
183
176
  }
184
177
 
185
178
  function isAlpha(chr) {
@@ -187,11 +180,11 @@ function isAlpha(chr) {
187
180
  }
188
181
 
189
182
  function isIdentifierChar(chr) {
190
- return isAlpha(chr) || hasUnicode(chr)
183
+ return /^[$_A-Za-z\u0080-\uffff]$/.test(chr || '')
191
184
  }
192
185
 
193
186
  function isIdentifier(str) {
194
- return isIdentifierChar(str[0]) && (str.length === 1 || isWord(str.slice(1)))
187
+ return isIdentifierChar(str[0]) && isWord(str)
195
188
  }
196
189
 
197
190
  function isStrTemplateChr(chr) {
@@ -282,17 +275,17 @@ function tokenize(code, options) {
282
275
  let __jsxEnter = false
283
276
  /** @type {0 | 1 | 2} 0 = none; 1 = inside `<open`; 2 = inside `</close` */
284
277
  let __jsxTag = 0
285
- let __jsxExpr = false
278
+ let __jsxExprDepth = 0
286
279
  let __jsxTagExpr = 0
287
280
 
288
281
  /** Nested `<open>…</open>` depth (content between tags, including nested elements). */
289
282
  let __jsxStack = 0
290
283
 
291
- const __jsxChild = () => __jsxEnter && !__jsxExpr && !__jsxTag
284
+ const __jsxChild = () => __jsxEnter && !__jsxExprDepth && !__jsxTag
292
285
  // < __content__ >
293
- const inJsxTag = () => __jsxTag && !__jsxChild()
286
+ const inJsxTag = () => __jsxTag
294
287
  // {'__content__'}
295
- const inJsxLiterals = () => !__jsxTag && __jsxChild() && !__jsxExpr && __jsxStack > 0
288
+ const inJsxLiterals = () => __jsxChild() && __jsxStack
296
289
 
297
290
  /** @type {string | null} */
298
291
  let __strQuote = null
@@ -419,14 +412,12 @@ function tokenize(code, options) {
419
412
  if (isSingleQuotes(curr) && !inJsxLiterals() && !inStrTemplateLiterals()) {
420
413
  append()
421
414
  let isStringClose = false
422
- if (prev !== `\\`) {
423
- if (__strQuote && curr === __strQuote) {
424
- __strQuote = null
425
- isStringClose = true
426
- } else if (!__strQuote) {
427
- __strQuote = curr
428
- __strTokenStart = tokens.length
429
- }
415
+ if (__strQuote && curr === __strQuote) {
416
+ __strQuote = null
417
+ isStringClose = true
418
+ } else if (!__strQuote) {
419
+ __strQuote = curr
420
+ __strTokenStart = tokens.length
430
421
  }
431
422
 
432
423
  append(T_STRING, curr)
@@ -438,25 +429,14 @@ function tokenize(code, options) {
438
429
  continue
439
430
  }
440
431
 
441
- if (!inStrTemplateLiterals()) {
442
- if (prev !== '\\n' && isTemplateQuote(curr)) {
443
- append()
444
- append(T_STRING, curr)
445
- __strTemplateQuoteStack++
446
- continue
447
- }
432
+ if (isTemplateQuote(curr)) {
433
+ append()
434
+ __strTemplateQuoteStack += inStrTemplateLiterals() ? -1 : 1
435
+ append(T_STRING, curr)
436
+ continue
448
437
  }
449
438
 
450
439
  if (inStrTemplateLiterals()) {
451
- if (prev !== '\\n' && isTemplateQuote(curr)) {
452
- if (__strTemplateQuoteStack > 0) {
453
- append()
454
- __strTemplateQuoteStack--
455
- append(T_STRING, curr)
456
- continue
457
- }
458
- }
459
-
460
440
  if (c_n === '${') {
461
441
  __strTemplateExprStack++
462
442
  append(T_STRING)
@@ -477,7 +457,7 @@ function tokenize(code, options) {
477
457
  if (curr === '{') {
478
458
  append()
479
459
  append(T_SIGN, curr)
480
- __jsxExpr = true
460
+ __jsxExprDepth = 1
481
461
  continue
482
462
  }
483
463
  }
@@ -634,6 +614,8 @@ function tokenize(code, options) {
634
614
  // string quotation
635
615
  if (isQuotationChar || isStringTemplateLiterals || isSingleQuotes(__strQuote)) {
636
616
  current += curr
617
+ // Consume the escaped character before the next delimiter check.
618
+ if (curr === '\\') current += code[++i] || ''
637
619
  } else if (isRegexChar) {
638
620
  append()
639
621
  const [lastType, lastToken] = last
@@ -656,25 +638,25 @@ function tokenize(code, options) {
656
638
  __regexQuoteStart = true
657
639
  const start = i++
658
640
 
659
- // end of line of end of file
660
- const isEof = () => i >= code.length
661
- const isEol = () => isEof() || code[i] === '\n'
662
-
663
641
  let foundClose = false
664
642
 
665
643
  // `/` is literal inside regex character classes, e.g. `[/]`.
666
644
  let inCharClass = false
667
645
 
668
646
  // traverse to find closing regex slash
669
- for (; !isEol(); i++) {
647
+ for (; i < code.length && code[i] !== '\n'; i++) {
670
648
  const ch = code[i]
671
- const escaped = code[i - 1] === '\\'
672
- if (!escaped && ch === '[') inCharClass = true
673
- if (!escaped && ch === ']') inCharClass = false
674
- if (ch === '/' && !inCharClass && !escaped) {
649
+ if (ch === '\\') {
650
+ if (code[i + 1] === '\n') break
651
+ i++
652
+ continue
653
+ }
654
+ if (ch === '[') inCharClass = true
655
+ if (ch === ']') inCharClass = false
656
+ if (ch === '/' && !inCharClass) {
675
657
  foundClose = true
676
658
  // end of regex, append regex flags
677
- while (start !== i && /^[a-z]$/.test(code[i + 1]) && !isEol()) {
659
+ while (/^[a-z]$/.test(code[i + 1])) {
678
660
  i++
679
661
  }
680
662
  break
@@ -730,11 +712,11 @@ function tokenize(code, options) {
730
712
  append()
731
713
  }
732
714
  } else {
733
- if (__jsxExpr && curr === '}') {
715
+ if (__jsxExprDepth && curr === '}') {
734
716
  append()
735
717
  current = curr
736
718
  append()
737
- __jsxExpr = false
719
+ __jsxExprDepth--
738
720
  } else if (
739
721
  // it's jsx literals and is not a jsx bracket
740
722
  (isJsxLiterals && !JSXBrackets.has(curr)) ||
@@ -761,6 +743,7 @@ function tokenize(code, options) {
761
743
  }
762
744
  else if (JSXBrackets.has(curr)) append()
763
745
  }
746
+ if (__jsxExprDepth && curr === '{') __jsxExprDepth++
764
747
  }
765
748
  }
766
749
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sugar-high",
3
- "version": "2.4.0",
3
+ "version": "2.4.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/huozhi/sugar-high.git",