sugar-high 0.4.2 → 0.4.3

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.
Files changed (2) hide show
  1. package/lib/index.mjs +90 -30
  2. package/package.json +2 -2
package/lib/index.mjs CHANGED
@@ -144,8 +144,16 @@ function isIdentifier(str) {
144
144
  return isIdentifierChar(str[0]) && (str.length === 1 || isWord(str.slice(1)))
145
145
  }
146
146
 
147
+ function isStrTemplateChr(chr) {
148
+ return chr === '`'
149
+ }
150
+
151
+ function isSingleQuotes(chr) {
152
+ return chr === '"' || chr === "'"
153
+ }
154
+
147
155
  function isStringQuotation(chr) {
148
- return chr === '"' || chr === "'" || chr === '`'
156
+ return isSingleQuotes(chr) || isStrTemplateChr(chr)
149
157
  }
150
158
 
151
159
  function isCommentStart(str) {
@@ -184,6 +192,13 @@ export function tokenize(code) {
184
192
  const inJsxTag = () => __jsxTag && !__jsxChild()
185
193
  const inJsxLiterals = () => !__jsxTag && __jsxChild() && !__jsxExpr && __jsxStack > 0
186
194
 
195
+ /** @type {string | 0} */
196
+ let __strQuote = 0
197
+ let __strTemplateExprStack = 0
198
+ let __strTemplateQuoteStack = 0
199
+ const inStrTemplateLiterals = () => (__strTemplateQuoteStack > __strTemplateExprStack)
200
+ const inStrTemplateExpr = () => __strTemplateQuoteStack > 0 && (__strTemplateQuoteStack === __strTemplateExprStack)
201
+
187
202
  /**
188
203
  *
189
204
  * @param {string} token
@@ -204,11 +219,14 @@ export function tokenize(code) {
204
219
  } else if (isCls(token)) {
205
220
  return inJsxTag() ? T_IDENTIFIER : T_CLS_NUMBER
206
221
  } else {
207
- return isIdentifier(token) ? T_IDENTIFIER : T_STRING
222
+ return isIdentifier(token) && !inStrTemplateLiterals() && (__strQuote !== `"` && __strQuote !== `'`) ? T_IDENTIFIER : T_STRING
208
223
  }
209
224
  }
210
225
 
211
- const append = (_type) => {
226
+ const append = (_type, _token) => {
227
+ if (_token) {
228
+ current = _token
229
+ }
212
230
  if (current) {
213
231
  type = _type || classify(current)
214
232
  /** @type [number, string] */
@@ -227,12 +245,61 @@ export function tokenize(code) {
227
245
  const p_c = prev + curr // previous and current
228
246
  const c_n = curr + next // current and next
229
247
 
248
+ if (isSingleQuotes(curr)) {
249
+ append()
250
+ if (prev !== `\\`) {
251
+ if (__strQuote && curr === __strQuote) {
252
+ __strQuote = 0
253
+ } else if (!__strQuote) {
254
+ __strQuote = curr
255
+ }
256
+ }
257
+
258
+ append(T_STRING, curr)
259
+ continue
260
+ }
261
+
262
+
263
+ if (!inStrTemplateLiterals()) {
264
+ if (prev !== '\\n' && curr === '`') {
265
+ append()
266
+ append(T_SIGN, curr)
267
+ __strTemplateQuoteStack++
268
+ continue
269
+ }
270
+ }
271
+
272
+ if (inStrTemplateLiterals()) {
273
+ if (prev !== '\\n' && curr === '`') {
274
+ if (__strTemplateQuoteStack > 0) {
275
+ append()
276
+ __strTemplateQuoteStack--
277
+ append(T_SIGN, curr)
278
+ continue
279
+ }
280
+ }
281
+
282
+
283
+ if (c_n === '${') {
284
+ __strTemplateExprStack++
285
+ append(T_STRING)
286
+ append(T_SIGN, c_n)
287
+ i++
288
+ continue
289
+ }
290
+ }
291
+
292
+ if (inStrTemplateExpr() && curr === '}') {
293
+ append()
294
+ __strTemplateExprStack--
295
+ append(T_SIGN, curr)
296
+ continue
297
+ }
230
298
 
231
299
  if (__jsxChild()) {
232
300
  if (curr === '{') {
233
301
  append()
234
- current = curr
235
- append(T_SIGN)
302
+ append(T_SIGN, curr)
236
303
  __jsxExpr = true
237
304
  continue
238
305
  }
@@ -263,8 +330,7 @@ export function tokenize(code) {
263
330
  __jsxTag = 0
264
331
  __jsxEnter = false
265
332
  }
266
- current = curr
267
- append(T_SIGN)
333
+ append(T_SIGN, curr)
268
334
  continue
269
335
  }
270
336
 
@@ -303,9 +369,14 @@ export function tokenize(code) {
303
369
  }
304
370
 
305
371
  const isQuotationChar = isStringQuotation(curr)
372
+ const isStringTemplateLiterals = inStrTemplateLiterals()
306
373
  const isRegexChar = !__jsxEnter && isRegexStart(c_n)
307
374
  const isJsxLiterals = inJsxLiterals()
308
- if (isQuotationChar || isRegexChar) {
375
+
376
+ // string quotation
377
+ if (isQuotationChar || isStringTemplateLiterals || isSingleQuotes(__strQuote)) {
378
+ current += curr
379
+ } else if (isRegexChar) {
309
380
  append()
310
381
  const [lastType, lastToken] = last
311
382
  // Special cases that are not considered as regex:
@@ -323,34 +394,23 @@ export function tokenize(code) {
323
394
  append()
324
395
  continue
325
396
  }
397
+
326
398
  const start = i++
327
- const leftQuote = code[start]
328
- let foundClose = false
329
399
 
330
400
  // end of line of end of file
331
401
  const isEof = () => i >= code.length
332
402
  const isEol = () => isEof() || code[i] === '\n'
333
- // string quotation
334
- if (isQuotationChar) {
335
- for (; !isEof(); i++) {
336
- // handle single quotes
337
- if ((leftQuote === "'" || leftQuote === '"') && code[i] === '\n') break
338
- if (isStringQuotation(code[i]) && code[i] === leftQuote) {
339
- foundClose = true
340
- break
341
- }
342
- }
343
- } else {
344
- // regex
345
- for (; !isEol(); i++) {
346
- if (code[i] === '/' && code[i - 1] !== '\\') {
347
- foundClose = true
348
- // append regex flags
349
- while (start !== i && /^[a-z]$/.test(code[i + 1]) && !isEol()) {
350
- i++
351
- }
352
- break
403
+
404
+ let foundClose = false
405
+ // regex
406
+ for (; !isEol(); i++) {
407
+ if (code[i] === '/' && code[i - 1] !== '\\') {
408
+ foundClose = true
409
+ // append regex flags
410
+ while (start !== i && /^[a-z]$/.test(code[i + 1]) && !isEol()) {
411
+ i++
353
412
  }
413
+ break
354
414
  }
355
415
  }
356
416
  if (start !== i && foundClose) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sugar-high",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "type": "module",
5
5
  "exports": "./lib/index.mjs",
6
6
  "description": "Super lightweight JSX syntax highlighter",
@@ -16,7 +16,7 @@
16
16
  "build": "next build docs"
17
17
  },
18
18
  "devDependencies": {
19
- "codice": "^0.0.9",
19
+ "codice": "^0.0.10",
20
20
  "next": "12.1.6-canary.6",
21
21
  "react": "18.0.0",
22
22
  "react-dom": "18.0.0",