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