sugar-high 0.1.0 → 0.1.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 +50 -46
- package/package.json +1 -1
package/lib/index.mjs
CHANGED
|
@@ -147,15 +147,9 @@ function isRegexStart(str) {
|
|
|
147
147
|
export function tokenize(code) {
|
|
148
148
|
let current = ''
|
|
149
149
|
let type = -1
|
|
150
|
+
let nonSpaceType = -1
|
|
150
151
|
/** @type {Array<[number, string]>} */
|
|
151
152
|
const tokens = []
|
|
152
|
-
// string.type = 0 for string or string template
|
|
153
|
-
// string.type = 1 for regex
|
|
154
|
-
const string = { entered: false, type: 0 }
|
|
155
|
-
|
|
156
|
-
// comment.type = 0 for single line comments
|
|
157
|
-
// comment.type = 1 for multi-line comments
|
|
158
|
-
const comment = { entered: false, type: 0 }
|
|
159
153
|
|
|
160
154
|
// jsx.tag for entering open or closed tag
|
|
161
155
|
// jsx.child for entering children
|
|
@@ -173,20 +167,20 @@ export function tokenize(code) {
|
|
|
173
167
|
return T_KEYWORD
|
|
174
168
|
} else if (token === '\n') {
|
|
175
169
|
return T_BREAK
|
|
176
|
-
} else if (
|
|
177
|
-
(
|
|
178
|
-
// is quoted string
|
|
179
|
-
(isStringQuotation(chr0) && !isStringQuotation(chr1)) ||
|
|
180
|
-
// is regex
|
|
181
|
-
(!jsx.tag && isRegexStart(chr0 + chr1) && token[token.length - 1] === '/')
|
|
182
|
-
)
|
|
183
|
-
) {
|
|
184
|
-
return T_STRING
|
|
185
170
|
} else if (token === ' ') {
|
|
186
171
|
return T_SPACE
|
|
187
|
-
} else if (signs.has(
|
|
172
|
+
} else if (token.split('').every(ch => signs.has(ch))) {
|
|
188
173
|
return T_SIGN
|
|
189
174
|
} else if (
|
|
175
|
+
(
|
|
176
|
+
// is quoted string
|
|
177
|
+
(isStringQuotation(chr0) && !isStringQuotation(chr1)) ||
|
|
178
|
+
// is regex
|
|
179
|
+
(!jsx.tag && isRegexStart(chr0 + chr1) && token[token.length - 1] === '/')
|
|
180
|
+
)
|
|
181
|
+
) {
|
|
182
|
+
return T_STRING
|
|
183
|
+
} else if (
|
|
190
184
|
!inJsxLiterals() &&
|
|
191
185
|
(
|
|
192
186
|
isIdentifierChar(chr0) &&
|
|
@@ -203,6 +197,7 @@ export function tokenize(code) {
|
|
|
203
197
|
const append = () => {
|
|
204
198
|
if (current) {
|
|
205
199
|
type = classify(current)
|
|
200
|
+
if (type !== T_SPACE && type !== T_BREAK) nonSpaceType = type
|
|
206
201
|
tokens.push([type, current])
|
|
207
202
|
}
|
|
208
203
|
current = ''
|
|
@@ -222,48 +217,57 @@ export function tokenize(code) {
|
|
|
222
217
|
jsx.tag = !(isOpenElementEnd || isCloseElementEnd)
|
|
223
218
|
}
|
|
224
219
|
// if it's not in a jsx tag declaration or a string, close child if next is jsx close tag
|
|
225
|
-
if (!jsx.tag &&
|
|
220
|
+
if (!jsx.tag && (curr === '<' && isIdentifierChar(next) || c_n === '</')) {
|
|
226
221
|
jsx.tag = true
|
|
227
222
|
jsx.child = false
|
|
228
223
|
}
|
|
229
224
|
|
|
230
225
|
if (
|
|
231
|
-
!string.entered &&
|
|
232
226
|
(isStringQuotation(curr) || !jsx.tag && isRegexStart(c_n))
|
|
233
227
|
) {
|
|
234
|
-
string.entered = true
|
|
235
|
-
string.type = isStringQuotation(curr) ? 0 : 1
|
|
236
228
|
append()
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
current += curr
|
|
240
|
-
if (string.type === 0 && isStringQuotation(curr)) {
|
|
241
|
-
string.entered = false
|
|
242
|
-
append()
|
|
243
|
-
} else if (string.type === 1 && prev !== '\\' && curr === '/') {
|
|
244
|
-
string.entered = false
|
|
229
|
+
if (!(nonSpaceType === T_SIGN || nonSpaceType === T_COMMENT)) {
|
|
230
|
+
current = curr
|
|
245
231
|
append()
|
|
232
|
+
continue
|
|
246
233
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
234
|
+
let j = i + 1
|
|
235
|
+
let strEnd = i
|
|
236
|
+
const inString = () => j < code.length && code[j] !== '\n'
|
|
237
|
+
// string quotation
|
|
238
|
+
if (isStringQuotation(curr)) {
|
|
239
|
+
for (; inString(); j++) {
|
|
240
|
+
if (isStringQuotation(code[j])) {
|
|
241
|
+
strEnd = j
|
|
242
|
+
break
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
} else {
|
|
246
|
+
// regex
|
|
247
|
+
for (; inString(); j++) {
|
|
248
|
+
if (code[j] === '/' && code[j - 1] !== '\\') {
|
|
249
|
+
strEnd = j
|
|
250
|
+
break
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (strEnd !== i) {
|
|
255
|
+
current = code.slice(i, strEnd + 1)
|
|
260
256
|
append()
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
current
|
|
257
|
+
i = j
|
|
258
|
+
} else {
|
|
259
|
+
current = curr
|
|
264
260
|
append()
|
|
265
|
-
i++
|
|
266
261
|
}
|
|
262
|
+
} else if (isCommentStart(c_n)) {
|
|
263
|
+
const start = i
|
|
264
|
+
if (next === '/') {
|
|
265
|
+
for (; i < code.length && code[i] !== '\n'; i++);
|
|
266
|
+
} else {
|
|
267
|
+
for (; i < code.length && code[i - 1] + code[i] !== '*/'; i++);
|
|
268
|
+
}
|
|
269
|
+
current = code.slice(start, i + 1)
|
|
270
|
+
append()
|
|
267
271
|
} else if (curr === ' ' || curr === '\n') {
|
|
268
272
|
if (
|
|
269
273
|
curr === ' ' &&
|