| 1 |
const ESLINT_VERSION = require("../utils/eslint-version.cjs"); |
| 2 |
|
| 3 |
function* it(children) { |
| 4 |
if (Array.isArray(children)) yield* children; |
| 5 |
else yield children; |
| 6 |
} |
| 7 |
|
| 8 |
function traverse(node, visitorKeys, visitor) { |
| 9 |
const { type } = node; |
| 10 |
if (!type) return; |
| 11 |
const keys = visitorKeys[type]; |
| 12 |
if (!keys) return; |
| 13 |
|
| 14 |
for (const key of keys) { |
| 15 |
for (const child of it(node[key])) { |
| 16 |
if (child && typeof child === "object") { |
| 17 |
visitor.enter(child); |
| 18 |
traverse(child, visitorKeys, visitor); |
| 19 |
visitor.exit(child); |
| 20 |
} |
| 21 |
} |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
const convertNodesVisitor = { |
| 26 |
enter(node) { |
| 27 |
if (node.innerComments) { |
| 28 |
delete node.innerComments; |
| 29 |
} |
| 30 |
|
| 31 |
if (node.trailingComments) { |
| 32 |
delete node.trailingComments; |
| 33 |
} |
| 34 |
|
| 35 |
if (node.leadingComments) { |
| 36 |
delete node.leadingComments; |
| 37 |
} |
| 38 |
}, |
| 39 |
exit(node) { |
| 40 |
|
| 41 |
if (node.extra) { |
| 42 |
delete node.extra; |
| 43 |
} |
| 44 |
|
| 45 |
if (node.loc.identifierName) { |
| 46 |
delete node.loc.identifierName; |
| 47 |
} |
| 48 |
|
| 49 |
if (node.type === "TypeParameter") { |
| 50 |
node.type = "Identifier"; |
| 51 |
node.typeAnnotation = node.bound; |
| 52 |
delete node.bound; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
if (node.type === "QualifiedTypeIdentifier") { |
| 58 |
delete node.id; |
| 59 |
} |
| 60 |
|
| 61 |
if (node.type === "ObjectTypeProperty") { |
| 62 |
delete node.key; |
| 63 |
} |
| 64 |
|
| 65 |
if (node.type === "ObjectTypeIndexer") { |
| 66 |
delete node.id; |
| 67 |
} |
| 68 |
|
| 69 |
if (node.type === "FunctionTypeParam") { |
| 70 |
delete node.name; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
if (node.type === "ImportDeclaration") { |
| 75 |
delete node.isType; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
if (node.type === "TemplateLiteral") { |
| 80 |
for (let i = 0; i < node.quasis.length; i++) { |
| 81 |
const q = node.quasis[i]; |
| 82 |
q.range[0] -= 1; |
| 83 |
if (q.tail) { |
| 84 |
q.range[1] += 1; |
| 85 |
} else { |
| 86 |
q.range[1] += 2; |
| 87 |
} |
| 88 |
q.loc.start.column -= 1; |
| 89 |
if (q.tail) { |
| 90 |
q.loc.end.column += 1; |
| 91 |
} else { |
| 92 |
q.loc.end.column += 2; |
| 93 |
} |
| 94 |
|
| 95 |
if (ESLINT_VERSION >= 8) { |
| 96 |
q.start -= 1; |
| 97 |
if (q.tail) { |
| 98 |
q.end += 1; |
| 99 |
} else { |
| 100 |
q.end += 2; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
}, |
| 106 |
}; |
| 107 |
|
| 108 |
function convertNodes(ast, visitorKeys) { |
| 109 |
traverse(ast, visitorKeys, convertNodesVisitor); |
| 110 |
} |
| 111 |
|
| 112 |
function convertProgramNode(ast) { |
| 113 |
ast.type = "Program"; |
| 114 |
ast.sourceType = ast.program.sourceType; |
| 115 |
ast.body = ast.program.body; |
| 116 |
delete ast.program; |
| 117 |
delete ast.errors; |
| 118 |
|
| 119 |
if (ast.comments.length) { |
| 120 |
const lastComment = ast.comments[ast.comments.length - 1]; |
| 121 |
|
| 122 |
if (ast.tokens.length) { |
| 123 |
const lastToken = ast.tokens[ast.tokens.length - 1]; |
| 124 |
|
| 125 |
if (lastComment.end > lastToken.end) { |
| 126 |
|
| 127 |
|
| 128 |
ast.range[1] = lastToken.end; |
| 129 |
ast.loc.end.line = lastToken.loc.end.line; |
| 130 |
ast.loc.end.column = lastToken.loc.end.column; |
| 131 |
|
| 132 |
if (ESLINT_VERSION >= 8) { |
| 133 |
ast.end = lastToken.end; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} else { |
| 138 |
if (!ast.tokens.length) { |
| 139 |
ast.loc.start.line = 1; |
| 140 |
ast.loc.end.line = 1; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
if (ast.body && ast.body.length > 0) { |
| 145 |
ast.loc.start.line = ast.body[0].loc.start.line; |
| 146 |
ast.range[0] = ast.body[0].start; |
| 147 |
|
| 148 |
if (ESLINT_VERSION >= 8) { |
| 149 |
ast.start = ast.body[0].start; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
module.exports = function convertAST(ast, visitorKeys) { |
| 155 |
convertNodes(ast, visitorKeys); |
| 156 |
convertProgramNode(ast); |
| 157 |
}; |