This commit is contained in:
Iliyan Angelov
2025-09-14 23:24:25 +03:00
commit c67067a2a4
71311 changed files with 6800714 additions and 0 deletions

View File

@@ -0,0 +1,136 @@
"use strict";
const ESLINT_VERSION = require("../utils/eslint-version.cjs");
function* it(children) {
if (Array.isArray(children)) yield* children;else yield children;
}
function traverse(node, visitorKeys, visitor) {
const {
type
} = node;
if (!type) return;
const keys = visitorKeys[type];
if (!keys) return;
for (const key of keys) {
for (const child of it(node[key])) {
if (child && typeof child === "object") {
visitor.enter(child);
traverse(child, visitorKeys, visitor);
visitor.exit(child);
}
}
}
}
const convertNodesVisitor = {
enter(node) {
if (node.innerComments) {
delete node.innerComments;
}
if (node.trailingComments) {
delete node.trailingComments;
}
if (node.leadingComments) {
delete node.leadingComments;
}
},
exit(node) {
if (node.extra) {
delete node.extra;
}
{
if (node.loc.identifierName) {
delete node.loc.identifierName;
}
}
if (node.type === "TypeParameter") {
node.type = "Identifier";
node.typeAnnotation = node.bound;
delete node.bound;
}
if (node.type === "QualifiedTypeIdentifier") {
delete node.id;
}
if (node.type === "ObjectTypeProperty") {
delete node.key;
}
if (node.type === "ObjectTypeIndexer") {
delete node.id;
}
if (node.type === "FunctionTypeParam") {
delete node.name;
}
if (node.type === "ImportDeclaration") {
delete node.isType;
}
if (node.type === "TemplateLiteral" || node.type === "TSTemplateLiteralType") {
for (let i = 0; i < node.quasis.length; i++) {
const q = node.quasis[i];
q.range[0] -= 1;
if (q.tail) {
q.range[1] += 1;
} else {
q.range[1] += 2;
}
q.loc.start.column -= 1;
if (q.tail) {
q.loc.end.column += 1;
} else {
q.loc.end.column += 2;
}
if (ESLINT_VERSION >= 8) {
q.start -= 1;
if (q.tail) {
q.end += 1;
} else {
q.end += 2;
}
}
}
}
}
};
function convertNodes(ast, visitorKeys) {
traverse(ast, visitorKeys, convertNodesVisitor);
}
function convertProgramNode(ast) {
const body = ast.program.body;
Object.assign(ast, {
type: "Program",
sourceType: ast.program.sourceType,
body
});
delete ast.program;
delete ast.errors;
if (ast.comments.length) {
const lastComment = ast.comments[ast.comments.length - 1];
if (ast.tokens.length) {
const lastToken = ast.tokens[ast.tokens.length - 1];
if (lastComment.end > lastToken.end) {
ast.range[1] = lastToken.end;
ast.loc.end.line = lastToken.loc.end.line;
ast.loc.end.column = lastToken.loc.end.column;
if (ESLINT_VERSION >= 8) {
ast.end = lastToken.end;
}
}
}
} else {
if (!ast.tokens.length) {
ast.loc.start.line = 1;
ast.loc.end.line = 1;
}
}
if (body != null && body.length) {
ast.loc.start.line = body[0].loc.start.line;
ast.range[0] = body[0].start;
if (ESLINT_VERSION >= 8) {
ast.start = body[0].start;
}
}
}
module.exports = function convertAST(ast, visitorKeys) {
convertNodes(ast, visitorKeys);
convertProgramNode(ast);
};
//# sourceMappingURL=convertAST.cjs.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,10 @@
"use strict";
module.exports = function convertComments(comments) {
for (const comment of comments) {
comment.type = comment.type === "CommentBlock" ? "Block" : "Line";
comment.range || (comment.range = [comment.start, comment.end]);
}
};
//# sourceMappingURL=convertComments.cjs.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["convertComments","comments","comment","type","range","start","end"],"sources":["../../src/convert/convertComments.cts"],"sourcesContent":["import type { Comment } from \"@babel/types\";\n\nexport = function convertComments(comments: Comment[]) {\n for (const comment of comments) {\n // @ts-expect-error eslint\n comment.type = comment.type === \"CommentBlock\" ? \"Block\" : \"Line\";\n\n // sometimes comments don't get ranges computed,\n // even with options.ranges === true\n\n // @ts-expect-error eslint\n comment.range ||= [comment.start, comment.end];\n }\n};\n"],"mappings":";;iBAES,SAASA,eAAeA,CAACC,QAAmB,EAAE;EACrD,KAAK,MAAMC,OAAO,IAAID,QAAQ,EAAE;IAE9BC,OAAO,CAACC,IAAI,GAAGD,OAAO,CAACC,IAAI,KAAK,cAAc,GAAG,OAAO,GAAG,MAAM;IAMjED,OAAO,CAACE,KAAK,KAAbF,OAAO,CAACE,KAAK,GAAK,CAACF,OAAO,CAACG,KAAK,EAAEH,OAAO,CAACI,GAAG,CAAC;EAChD;AACF,CAAC","ignoreList":[]}

View File

@@ -0,0 +1,161 @@
"use strict";
const ESLINT_VERSION = require("../utils/eslint-version.cjs");
function convertTemplateType(tokens, tl) {
let curlyBrace = null;
let templateTokens = [];
const result = [];
function addTemplateType() {
const start = templateTokens[0];
const end = templateTokens[templateTokens.length - 1];
const value = templateTokens.reduce((result, token) => {
if (token.value) {
result += token.value;
} else if (token.type.label !== tl.template) {
result += token.type.label;
}
return result;
}, "");
result.push({
type: "Template",
value: value,
start: start.start,
end: end.end,
loc: {
start: start.loc.start,
end: end.loc.end
}
});
templateTokens = [];
}
tokens.forEach(token => {
switch (token.type.label) {
case tl.backQuote:
if (curlyBrace) {
result.push(curlyBrace);
curlyBrace = null;
}
templateTokens.push(token);
if (templateTokens.length > 1) {
addTemplateType();
}
break;
case tl.dollarBraceL:
templateTokens.push(token);
addTemplateType();
break;
case tl.braceR:
if (curlyBrace) {
result.push(curlyBrace);
}
curlyBrace = token;
break;
case tl.template:
if (curlyBrace) {
templateTokens.push(curlyBrace);
curlyBrace = null;
}
templateTokens.push(token);
break;
default:
if (curlyBrace) {
result.push(curlyBrace);
curlyBrace = null;
}
result.push(token);
}
});
return result;
}
function convertToken(token, source, tl) {
const {
type
} = token;
const {
label
} = type;
const newToken = token;
newToken.range = [token.start, token.end];
if (label === tl.name) {
const tokenValue = token.value;
if (tokenValue === "let" || tokenValue === "static" || tokenValue === "yield") {
newToken.type = "Keyword";
} else {
newToken.type = "Identifier";
}
} else if (label === tl.semi || label === tl.comma || label === tl.parenL || label === tl.parenR || label === tl.braceL || label === tl.braceR || label === tl.slash || label === tl.dot || label === tl.bracketL || label === tl.bracketR || label === tl.ellipsis || label === tl.arrow || label === tl.pipeline || label === tl.star || label === tl.incDec || label === tl.colon || label === tl.question || label === tl.template || label === tl.backQuote || label === tl.dollarBraceL || label === tl.at || label === tl.logicalOR || label === tl.logicalAND || label === tl.nullishCoalescing || label === tl.bitwiseOR || label === tl.bitwiseXOR || label === tl.bitwiseAND || label === tl.equality || label === tl.relational || label === tl.bitShift || label === tl.plusMin || label === tl.modulo || label === tl.exponent || label === tl.bang || label === tl.tilde || label === tl.doubleColon || label === tl.hash || label === tl.questionDot || label === tl.braceHashL || label === tl.braceBarL || label === tl.braceBarR || label === tl.bracketHashL || label === tl.bracketBarL || label === tl.bracketBarR || label === tl.doubleCaret || label === tl.doubleAt || type.isAssign) {
var _newToken$value;
newToken.type = "Punctuator";
(_newToken$value = newToken.value) != null ? _newToken$value : newToken.value = label;
} else if (label === tl.jsxTagStart) {
newToken.type = "Punctuator";
newToken.value = "<";
} else if (label === tl.jsxTagEnd) {
newToken.type = "Punctuator";
newToken.value = ">";
} else if (label === tl.jsxName) {
newToken.type = "JSXIdentifier";
} else if (label === tl.jsxText) {
newToken.type = "JSXText";
} else if (type.keyword === "null") {
newToken.type = "Null";
} else if (type.keyword === "false" || type.keyword === "true") {
newToken.type = "Boolean";
} else if (type.keyword) {
newToken.type = "Keyword";
} else if (label === tl.num) {
newToken.type = "Numeric";
newToken.value = source.slice(token.start, token.end);
} else if (label === tl.string) {
newToken.type = "String";
newToken.value = source.slice(token.start, token.end);
} else if (label === tl.regexp) {
newToken.type = "RegularExpression";
const value = token.value;
newToken.regex = {
pattern: value.pattern,
flags: value.flags
};
newToken.value = `/${value.pattern}/${value.flags}`;
} else if (label === tl.bigint) {
newToken.type = "Numeric";
newToken.value = `${token.value}n`;
} else if (label === tl.privateName) {
newToken.type = "PrivateIdentifier";
} else if (label === tl.templateNonTail || label === tl.templateTail || label === tl.Template) {
newToken.type = "Template";
}
;
return newToken;
}
module.exports = function convertTokens(tokens, code, tokLabels) {
const result = [];
const templateTypeMergedTokens = convertTemplateType(tokens, tokLabels);
for (let i = 0, {
length
} = templateTypeMergedTokens; i < length - 1; i++) {
const token = templateTypeMergedTokens[i];
const tokenType = token.type;
if (tokenType === "CommentLine" || tokenType === "CommentBlock") {
continue;
}
{
if (ESLINT_VERSION >= 8 && i + 1 < length && tokenType.label === tokLabels.hash) {
const nextToken = templateTypeMergedTokens[i + 1];
if (nextToken.type.label === tokLabels.name && token.end === nextToken.start) {
i++;
nextToken.type = "PrivateIdentifier";
nextToken.start -= 1;
nextToken.loc.start.column -= 1;
nextToken.range = [nextToken.start, nextToken.end];
result.push(nextToken);
continue;
}
}
}
result.push(convertToken(token, code, tokLabels));
}
return result;
};
//# sourceMappingURL=convertTokens.cjs.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,25 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.convertError = convertError;
exports.convertFile = convertFile;
const convertTokens = require("./convertTokens.cjs");
const convertComments = require("./convertComments.cjs");
const convertAST = require("./convertAST.cjs");
function convertFile(ast, code, tokLabels, visitorKeys) {
ast.tokens = convertTokens(ast.tokens, code, tokLabels);
convertComments(ast.comments);
convertAST(ast, visitorKeys);
return ast;
}
function convertError(err) {
if (err instanceof SyntaxError) {
err.lineNumber = err.loc.line;
err.column = err.loc.column;
}
return err;
}
//# sourceMappingURL=index.cjs.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["convertTokens","require","convertComments","convertAST","convertFile","ast","code","tokLabels","visitorKeys","tokens","comments","convertError","err","SyntaxError","lineNumber","loc","line","column"],"sources":["../../src/convert/index.cts"],"sourcesContent":["import convertTokens = require(\"./convertTokens.cts\");\nimport convertComments = require(\"./convertComments.cts\");\nimport convertAST = require(\"./convertAST.cts\");\nimport type { AST, ParseResult } from \"../types.cts\";\n\nexport function convertFile(\n ast: ParseResult,\n code: string,\n tokLabels: Record<string, any>,\n visitorKeys: Record<string, string[]>,\n) {\n ast.tokens = convertTokens(ast.tokens as any, code, tokLabels);\n convertComments(ast.comments);\n convertAST(ast, visitorKeys);\n return ast as unknown as AST.Program;\n}\n\nexport function convertError(err: Error) {\n if (err instanceof SyntaxError) {\n // @ts-expect-error eslint\n err.lineNumber = err.loc.line;\n // @ts-expect-error eslint\n err.column = err.loc.column;\n }\n return err;\n}\n"],"mappings":";;;;;;;MAAOA,aAAa,GAAAC,OAAA,CAAW,qBAAqB;AAAA,MAC7CC,eAAe,GAAAD,OAAA,CAAW,uBAAuB;AAAA,MACjDE,UAAU,GAAAF,OAAA,CAAW,kBAAkB;AAGvC,SAASG,WAAWA,CACzBC,GAAgB,EAChBC,IAAY,EACZC,SAA8B,EAC9BC,WAAqC,EACrC;EACAH,GAAG,CAACI,MAAM,GAAGT,aAAa,CAACK,GAAG,CAACI,MAAM,EAASH,IAAI,EAAEC,SAAS,CAAC;EAC9DL,eAAe,CAACG,GAAG,CAACK,QAAQ,CAAC;EAC7BP,UAAU,CAACE,GAAG,EAAEG,WAAW,CAAC;EAC5B,OAAOH,GAAG;AACZ;AAEO,SAASM,YAAYA,CAACC,GAAU,EAAE;EACvC,IAAIA,GAAG,YAAYC,WAAW,EAAE;IAE9BD,GAAG,CAACE,UAAU,GAAGF,GAAG,CAACG,GAAG,CAACC,IAAI;IAE7BJ,GAAG,CAACK,MAAM,GAAGL,GAAG,CAACG,GAAG,CAACE,MAAM;EAC7B;EACA,OAAOL,GAAG;AACZ","ignoreList":[]}