| 1 |
const ESLINT_VERSION = require("../utils/eslint-version.cjs"); |
| 2 |
|
| 3 |
function convertTemplateType(tokens, tl) { |
| 4 |
let curlyBrace = null; |
| 5 |
let templateTokens = []; |
| 6 |
const result = []; |
| 7 |
|
| 8 |
function addTemplateType() { |
| 9 |
const start = templateTokens[0]; |
| 10 |
const end = templateTokens[templateTokens.length - 1]; |
| 11 |
|
| 12 |
const value = templateTokens.reduce((result, token) => { |
| 13 |
if (token.value) { |
| 14 |
result += token.value; |
| 15 |
} else if (token.type.label !== tl.template) { |
| 16 |
result += token.type.label; |
| 17 |
} |
| 18 |
|
| 19 |
return result; |
| 20 |
}, ""); |
| 21 |
|
| 22 |
result.push({ |
| 23 |
type: "Template", |
| 24 |
value: value, |
| 25 |
start: start.start, |
| 26 |
end: end.end, |
| 27 |
loc: { |
| 28 |
start: start.loc.start, |
| 29 |
end: end.loc.end, |
| 30 |
}, |
| 31 |
}); |
| 32 |
|
| 33 |
templateTokens = []; |
| 34 |
} |
| 35 |
|
| 36 |
tokens.forEach(token => { |
| 37 |
switch (token.type.label) { |
| 38 |
case tl.backQuote: |
| 39 |
if (curlyBrace) { |
| 40 |
result.push(curlyBrace); |
| 41 |
curlyBrace = null; |
| 42 |
} |
| 43 |
|
| 44 |
templateTokens.push(token); |
| 45 |
|
| 46 |
if (templateTokens.length > 1) { |
| 47 |
addTemplateType(); |
| 48 |
} |
| 49 |
|
| 50 |
break; |
| 51 |
|
| 52 |
case tl.dollarBraceL: |
| 53 |
templateTokens.push(token); |
| 54 |
addTemplateType(); |
| 55 |
break; |
| 56 |
|
| 57 |
case tl.braceR: |
| 58 |
if (curlyBrace) { |
| 59 |
result.push(curlyBrace); |
| 60 |
} |
| 61 |
|
| 62 |
curlyBrace = token; |
| 63 |
break; |
| 64 |
|
| 65 |
case tl.template: |
| 66 |
if (curlyBrace) { |
| 67 |
templateTokens.push(curlyBrace); |
| 68 |
curlyBrace = null; |
| 69 |
} |
| 70 |
|
| 71 |
templateTokens.push(token); |
| 72 |
break; |
| 73 |
|
| 74 |
default: |
| 75 |
if (curlyBrace) { |
| 76 |
result.push(curlyBrace); |
| 77 |
curlyBrace = null; |
| 78 |
} |
| 79 |
|
| 80 |
result.push(token); |
| 81 |
} |
| 82 |
}); |
| 83 |
|
| 84 |
return result; |
| 85 |
} |
| 86 |
|
| 87 |
function convertToken(token, source, tl) { |
| 88 |
const { type } = token; |
| 89 |
const { label } = type; |
| 90 |
token.range = [token.start, token.end]; |
| 91 |
|
| 92 |
if (label === tl.name) { |
| 93 |
if (token.value === "static") { |
| 94 |
token.type = "Keyword"; |
| 95 |
} else { |
| 96 |
token.type = "Identifier"; |
| 97 |
} |
| 98 |
} else if ( |
| 99 |
label === tl.semi || |
| 100 |
label === tl.comma || |
| 101 |
label === tl.parenL || |
| 102 |
label === tl.parenR || |
| 103 |
label === tl.braceL || |
| 104 |
label === tl.braceR || |
| 105 |
label === tl.slash || |
| 106 |
label === tl.dot || |
| 107 |
label === tl.bracketL || |
| 108 |
label === tl.bracketR || |
| 109 |
label === tl.ellipsis || |
| 110 |
label === tl.arrow || |
| 111 |
label === tl.pipeline || |
| 112 |
label === tl.star || |
| 113 |
label === tl.incDec || |
| 114 |
label === tl.colon || |
| 115 |
label === tl.question || |
| 116 |
label === tl.template || |
| 117 |
label === tl.backQuote || |
| 118 |
label === tl.dollarBraceL || |
| 119 |
label === tl.at || |
| 120 |
label === tl.logicalOR || |
| 121 |
label === tl.logicalAND || |
| 122 |
label === tl.nullishCoalescing || |
| 123 |
label === tl.bitwiseOR || |
| 124 |
label === tl.bitwiseXOR || |
| 125 |
label === tl.bitwiseAND || |
| 126 |
label === tl.equality || |
| 127 |
label === tl.relational || |
| 128 |
label === tl.bitShift || |
| 129 |
label === tl.plusMin || |
| 130 |
label === tl.modulo || |
| 131 |
label === tl.exponent || |
| 132 |
label === tl.bang || |
| 133 |
label === tl.tilde || |
| 134 |
label === tl.doubleColon || |
| 135 |
label === tl.hash || |
| 136 |
label === tl.questionDot || |
| 137 |
label === tl.braceHashL || |
| 138 |
label === tl.braceBarL || |
| 139 |
label === tl.braceBarR || |
| 140 |
label === tl.bracketHashL || |
| 141 |
label === tl.bracketBarL || |
| 142 |
label === tl.bracketBarR || |
| 143 |
label === tl.doubleCaret || |
| 144 |
label === tl.doubleAt || |
| 145 |
type.isAssign |
| 146 |
) { |
| 147 |
token.type = "Punctuator"; |
| 148 |
token.value ??= label; |
| 149 |
} else if (label === tl.jsxTagStart) { |
| 150 |
token.type = "Punctuator"; |
| 151 |
token.value = "<"; |
| 152 |
} else if (label === tl.jsxTagEnd) { |
| 153 |
token.type = "Punctuator"; |
| 154 |
token.value = ">"; |
| 155 |
} else if (label === tl.jsxName) { |
| 156 |
token.type = "JSXIdentifier"; |
| 157 |
} else if (label === tl.jsxText) { |
| 158 |
token.type = "JSXText"; |
| 159 |
} else if (type.keyword === "null") { |
| 160 |
token.type = "Null"; |
| 161 |
} else if (type.keyword === "false" || type.keyword === "true") { |
| 162 |
token.type = "Boolean"; |
| 163 |
} else if (type.keyword) { |
| 164 |
token.type = "Keyword"; |
| 165 |
} else if (label === tl.num) { |
| 166 |
token.type = "Numeric"; |
| 167 |
token.value = source.slice(token.start, token.end); |
| 168 |
} else if (label === tl.string) { |
| 169 |
token.type = "String"; |
| 170 |
token.value = source.slice(token.start, token.end); |
| 171 |
} else if (label === tl.regexp) { |
| 172 |
token.type = "RegularExpression"; |
| 173 |
const value = token.value; |
| 174 |
token.regex = { |
| 175 |
pattern: value.pattern, |
| 176 |
flags: value.flags, |
| 177 |
}; |
| 178 |
token.value = `/${value.pattern}/${value.flags}`; |
| 179 |
} else if (label === tl.bigint) { |
| 180 |
token.type = "Numeric"; |
| 181 |
token.value = `${token.value}n`; |
| 182 |
} else if (label === tl.privateName) { |
| 183 |
token.type = "PrivateIdentifier"; |
| 184 |
} else if (label === tl.templateNonTail || label === tl.templateTail) { |
| 185 |
token.type = "Template"; |
| 186 |
} |
| 187 |
|
| 188 |
if (typeof token.type !== "string") { |
| 189 |
|
| 190 |
delete token.type.rightAssociative; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
module.exports = function convertTokens(tokens, code, tl) { |
| 195 |
const result = []; |
| 196 |
const templateTypeMergedTokens = process.env.BABEL_8_BREAKING |
| 197 |
? tokens |
| 198 |
: convertTemplateType(tokens, tl); |
| 199 |
|
| 200 |
for (let i = 0, { length } = templateTypeMergedTokens; i < length - 1; i++) { |
| 201 |
const token = templateTypeMergedTokens[i]; |
| 202 |
const tokenType = token.type; |
| 203 |
if (tokenType === "CommentLine" || tokenType === "CommentBlock") { |
| 204 |
continue; |
| 205 |
} |
| 206 |
|
| 207 |
if (!process.env.BABEL_8_BREAKING) { |
| 208 |
|
| 209 |
|
| 210 |
if ( |
| 211 |
ESLINT_VERSION >= 8 && |
| 212 |
i + 1 < length && |
| 213 |
tokenType.label === tl.hash |
| 214 |
) { |
| 215 |
const nextToken = templateTypeMergedTokens[i + 1]; |
| 216 |
|
| 217 |
|
| 218 |
if (nextToken.type.label === tl.name && token.end === nextToken.start) { |
| 219 |
i++; |
| 220 |
|
| 221 |
nextToken.type = "PrivateIdentifier"; |
| 222 |
nextToken.start -= 1; |
| 223 |
nextToken.loc.start.column -= 1; |
| 224 |
nextToken.range = [nextToken.start, nextToken.end]; |
| 225 |
|
| 226 |
result.push(nextToken); |
| 227 |
continue; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
convertToken(token, code, tl); |
| 233 |
result.push(token); |
| 234 |
} |
| 235 |
|
| 236 |
return result; |
| 237 |
}; |