sugar-high 0.4.0 → 0.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.
Files changed (2) hide show
  1. package/lib/index.mjs +24 -12
  2. package/package.json +4 -2
package/lib/index.mjs CHANGED
@@ -172,8 +172,9 @@ export function tokenize(code) {
172
172
  // jsx.close for is the closing sign of open or close tags
173
173
  // jsx.child for entering children
174
174
  // jsx.expr for entering {expression}
175
- /** @type {{ tag: boolean; close: boolean; child: boolean; expr: boolean }} */
176
- const jsx = { tag: false, close: false, child: false, expr: false }
175
+ // jsx.end for entering close tag
176
+ /** @type {{ tag: boolean; close: boolean; child: boolean; expr: boolean; end: boolean }} */
177
+ const jsx = { tag: false, close: false, child: false, expr: false, end: false }
177
178
 
178
179
  const inJsxTag = () => jsx.tag || jsx.close
179
180
  const inJsxLiterals = () => !inJsxTag() && jsx.child && !jsx.expr
@@ -185,19 +186,19 @@ export function tokenize(code) {
185
186
  */
186
187
  function classify(token) {
187
188
  if (keywords.has(token)) {
188
- return T_KEYWORD
189
+ return last[1] === '.' ? T_IDENTIFIER : T_KEYWORD
189
190
  } else if (token === '\n') {
190
191
  return T_BREAK
191
192
  } else if (isSpaces(token)) {
192
193
  return T_SPACE
193
194
  } else if (token.split('').every(ch => signs.has(ch))) {
194
195
  return T_SIGN
196
+ } else if (inJsxLiterals()) {
197
+ return T_JSX_LITERALS
195
198
  } else if (isCls(token)) {
196
- return inJsxLiterals() ? T_JSX_LITERALS : inJsxTag() ? T_IDENTIFIER : T_CLS_NUMBER
199
+ return inJsxTag() ? T_IDENTIFIER : T_CLS_NUMBER
197
200
  } else {
198
- return inJsxLiterals()
199
- ? T_JSX_LITERALS
200
- : isIdentifier(token) ? T_IDENTIFIER : T_STRING
201
+ return isIdentifier(token) ? T_IDENTIFIER : T_STRING
201
202
  }
202
203
  }
203
204
 
@@ -224,12 +225,21 @@ export function tokenize(code) {
224
225
  if (jsx.close && (last[1] === '>' || last[1] === '/>')) {
225
226
  jsx.close = false
226
227
  }
228
+
229
+ if (!jsx.tag && (c_n === '</' || c_n === '/>')) {
230
+ jsx.end = true
231
+ }
232
+
227
233
  if (jsx.tag) {
228
234
  const isOpenElementEnd = curr === '>'
229
235
  const isCloseElementEnd = p_c === '/>'
230
- jsx.child = !isCloseElementEnd
236
+ jsx.child = !jsx.end && (isOpenElementEnd && !isCloseElementEnd)
237
+
231
238
  jsx.tag = !(isOpenElementEnd || isCloseElementEnd)
232
239
  jsx.close = isOpenElementEnd || isCloseElementEnd
240
+ if (jsx.end && (isCloseElementEnd || isOpenElementEnd)) {
241
+ jsx.end = false
242
+ }
233
243
  }
234
244
  // if it's not in a jsx tag declaration or a string, close child if next is jsx close tag
235
245
  if (!jsx.tag && (curr === '<' && isIdentifierChar(next) || c_n === '</')) {
@@ -243,13 +253,13 @@ export function tokenize(code) {
243
253
  append()
244
254
  const [lastType, lastToken] = last
245
255
  // Special cases that are not considered as regex:
246
- // * (expr1) / expr2: `()` before `/` operator is still in expression
256
+ // * (expr1) / expr2: `)` before `/` operator is still in expression
247
257
  // * <non comment start>/ expr: non comment start before `/` is not regex
248
258
  if (
249
259
  isRegexChar &&
250
260
  lastType &&
251
261
  !(
252
- (lastType === T_SIGN && !'()'.includes(lastToken)) ||
262
+ (lastType === T_SIGN && ')' !== lastToken) ||
253
263
  lastType === T_COMMENT
254
264
  )
255
265
  ) {
@@ -324,16 +334,18 @@ export function tokenize(code) {
324
334
  }
325
335
  } else {
326
336
  if (
337
+ // it's jsx literals and is not a jsx bracket
327
338
  (isJsxLiterals && !jsxBrackets.has(curr)) ||
339
+ // same type char as previous one in current token
328
340
  isWord(curr) === isWord(current[current.length - 1]) &&
329
341
  !signs.has(curr)
330
342
  ) {
331
343
  current += curr
332
344
  if (next === '<') {
333
- append();
345
+ append(T_JSX_LITERALS);
334
346
  }
335
347
  } else {
336
- append()
348
+ append(c_n === '</' ? T_JSX_LITERALS : undefined)
337
349
  current = curr
338
350
  if (c_n === '</' || c_n === '/>') {
339
351
  current = c_n
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sugar-high",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "type": "module",
5
5
  "exports": "./lib/index.mjs",
6
6
  "description": "Super lightweight JSX syntax highlighter",
@@ -16,9 +16,11 @@
16
16
  "build": "next build docs"
17
17
  },
18
18
  "devDependencies": {
19
- "next": "12.1.4",
19
+ "codice": "^0.0.3",
20
+ "next": "12.1.6-canary.6",
20
21
  "react": "18.0.0",
21
22
  "react-dom": "18.0.0",
23
+ "sugar-high": "link:./",
22
24
  "vitest": "~0.6.0"
23
25
  }
24
26
  }