You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
8.6 KiB
Plaintext

2 years ago
{"version":3,"names":["ESLINT_VERSION","require","it","children","Array","isArray","traverse","node","visitorKeys","visitor","type","keys","key","child","enter","exit","convertNodesVisitor","innerComments","trailingComments","leadingComments","extra","loc","identifierName","typeAnnotation","bound","id","name","isType","i","quasis","length","q","range","tail","start","column","end","convertNodes","ast","convertProgramNode","sourceType","program","body","errors","comments","lastComment","tokens","lastToken","line","module","exports","convertAST"],"sources":["../../src/convert/convertAST.cjs"],"sourcesContent":["const ESLINT_VERSION = require(\"../utils/eslint-version.cjs\");\n\nfunction* it(children) {\n if (Array.isArray(children)) yield* children;\n else yield children;\n}\n\nfunction traverse(node, visitorKeys, visitor) {\n const { type } = node;\n if (!type) return;\n const keys = visitorKeys[type];\n if (!keys) return;\n\n for (const key of keys) {\n for (const child of it(node[key])) {\n if (child && typeof child === \"object\") {\n visitor.enter(child);\n traverse(child, visitorKeys, visitor);\n visitor.exit(child);\n }\n }\n }\n}\n\nconst convertNodesVisitor = {\n enter(node) {\n if (node.innerComments) {\n delete node.innerComments;\n }\n\n if (node.trailingComments) {\n delete node.trailingComments;\n }\n\n if (node.leadingComments) {\n delete node.leadingComments;\n }\n },\n exit(node) {\n // Used internally by @babel/parser.\n if (node.extra) {\n delete node.extra;\n }\n\n if (node.loc.identifierName) {\n delete node.loc.identifierName;\n }\n\n if (node.type === \"TypeParameter\") {\n node.type = \"Identifier\";\n node.typeAnnotation = node.bound;\n delete node.bound;\n }\n\n // flow: prevent \"no-undef\"\n // for \"Component\" in: \"let x: React.Component\"\n if (node.type === \"QualifiedTypeIdentifier\") {\n delete node.id;\n }\n // for \"b\" in: \"var a: { b: Foo }\"\n if (node.type === \"ObjectTypeProperty\") {\n delete node.key;\n }\n // for \"indexer\" in: \"var a: {[indexer: string]: number}\"\n if (node.type === \"ObjectTypeIndexer\") {\n delete node.id;\n }\n // for \"param\" in: \"var a: { func(param: Foo): Bar };\"\n if (node.type === \"FunctionTypeParam\") {\n delete node.name;\n }\n\n // modules\n if (node.type === \"ImportDeclaration\") {\n delete node.isType;\n }\n\n // template string range fixes\n if (node.type === \"TemplateLiteral\") {\n for (let i = 0; i < node.quasis.length; i++) {\n const q = node.quasis[i];\n q.range[0] -= 1;\n if (q.tail) {\n q.range[1] += 1;\n } else {\n q.range[1] += 2;\n }\n q.loc.start.column -= 1;\n if (q.tail) {\n q.loc.end.column += 1;\n } else {\n q.loc.end.column += 2;\n }\n\n if (ESLINT_VERSION >= 8) {\n q.start -= 1;\n if (q.tail) {\n q.end += 1;\n } else {\n q.end += 2;\n }\n }\n }\n }\n },\n};\n\nfunction convertNodes(ast, visitorKeys) {\n traverse(ast, visitorKeys, convertNodesVisitor);\n}\n\nfunction convertProgramNode(ast) {\n ast.type = \"Program\";\n ast.sourceType = ast.program.sourceType;\n ast.body = ast.program.body;\n delete ast.program;\n delete ast.errors;\n\n if (ast.comments.length) {\n const lastComment = ast.comments[ast.comments.length - 1];\n\n if (ast.tokens.length) {\n const lastToken = ast.tokens[ast.tokens.length - 1];\n\n if (lastComment.end > lastToken.end) {\n // If there is a comment after the last token, the program ends at the\n // last token and not the comment\n ast.range[1] = lastToken.end;\n ast.loc.end.line = lastToken.loc.end.line;\n ast.loc.end.column = lastToken.loc.end.column;\n\n if (ESLINT_VERSION >= 8) {\n ast.end = lastToken.end;\n }\n }\n }\n }