File size: 7,032 Bytes
04ec17f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
import { parseFiles } from "@ast-grep/napi";
import MagicString from "magic-string";
import { chalk, fs, path } from "zx";
import { errors } from "./errors.js";
import { root } from "./utils.js";
/**
* @typedef {import("@ast-grep/napi").SgNode} SgNode
*/
export function ast_grep() {
const task_queue = [];
const task = parseFiles([root("esm")], (err, tree) => {
const filename = path.basename(tree.filename(), ".js");
if (filename === "index") {
return;
}
const source = new MagicString(tree.root().text());
source.prepend(`"use strict";\n\n`);
if (filename.startsWith("_ts")) {
const match = tree.root().find(`export { $NAME as _ } from "tslib"`);
if (match) {
const name = match.getMatch("NAME").text();
const range = match.range();
source.update(
range.start.index,
range.end.index,
`exports._ = require("tslib").${name};`,
);
task_queue.push(
fs.writeFile(root("cjs", `${filename}.cjs`), source.toString(), {
encoding: "utf-8",
}),
);
} else {
report_noexport(tree.filename());
}
return;
}
// rewrite export named function
const match = tree.root().find({
rule: {
kind: "export_statement",
pattern: "export { $FUNC as _ }",
},
});
if (match) {
const func = match.getMatch("FUNC");
const func_name = func.text();
if (func_name !== filename) {
report_export_mismatch(tree.filename(), match);
}
const range = match.range();
source.update(
range.start.index,
range.end.index,
`exports._ = ${func_name};`,
);
// since we match the { export x as _ } pattern,
// we need to find the assignment expression from the root
tree
.root()
.findAll({
rule: {
pattern: func_name,
kind: "identifier",
inside: { kind: "assignment_expression", field: "left" },
},
})
.forEach((match) => {
const range = match.range();
source.prependLeft(range.start.index, `exports._ = `);
});
} else {
report_noexport(tree.filename(tree.filename()));
}
// rewrite import
tree
.root()
.findAll({ rule: { pattern: `import { _ as $BINDING } from "$SOURCE"` } })
.forEach((match) => {
const import_binding = match.getMatch("BINDING").text();
const import_source = match.getMatch("SOURCE").text();
const import_basename = path.basename(import_source, ".js");
if (import_binding !== import_basename) {
report_import_mismatch(tree.filename(), match);
}
const range = match.range();
source.update(
range.start.index,
range.end.index,
`var ${import_binding} = require("./${import_binding}.cjs");`,
);
tree
.root()
.findAll({
rule: {
pattern: import_binding,
kind: "identifier",
inside: {
not: {
kind: "import_specifier",
},
},
},
})
.forEach((match) => {
const range = match.range();
const ref_name = match.text();
source.update(
range.start.index,
range.end.index,
`${ref_name}._`,
);
});
});
task_queue.push(
fs.writeFile(root("cjs", `${filename}.cjs`), source.toString(), {
encoding: "utf-8",
}),
);
});
task_queue.push(task);
return task_queue;
}
/**
* @param {string} filename
* @param {SgNode} match
*/
function report_export_mismatch(filename, match) {
const func = match.getMatch("FUNC");
const func_range = func.range();
const text = match.text().split("\n");
const offset = func_range.start.line - match.range().start.line;
text.splice(
offset + 1,
text.length,
chalk.red(
[
" ".repeat(func_range.start.column),
"^".repeat(func_range.end.column - func_range.start.column),
]
.join(""),
),
);
errors.push(
[
`${chalk.bold.red("error")}: mismatch exported function name.`,
"",
`${chalk.blue("-->")} ${filename}:${func_range.start.line + 1}:${func_range.start.column + 1}`,
"",
...text,
"",
`${
chalk.bold(
"note:",
)
} The exported name should be the same as the filename.`,
"",
]
.join("\n"),
);
}
/**
* @param {string} filename
* @param {SgNode} match
*/
function report_import_mismatch(filename, match) {
const binding_range = match.getMatch("BINDING").range();
const source_range = match.getMatch("SOURCE").range();
errors.push(
[
`${chalk.bold.red("error")}: mismatch imported binding name.`,
"",
`${chalk.blue("-->")} ${filename}:${match.range().start.line + 1}`,
"",
match.text(),
[
" ".repeat(binding_range.start.column),
chalk.red("^".repeat(binding_range.end.column - binding_range.start.column)),
" ".repeat(source_range.start.column - binding_range.end.column),
chalk.blue("-".repeat(source_range.end.column - source_range.start.column)),
]
.join(""),
`${
chalk.bold(
"note:",
)
} The imported binding name should be the same as the import source basename.`,
"",
]
.join("\n"),
);
}
/**
* @param {string} filename
*/
function report_noexport(filename) {
errors.push(
[`${chalk.bold.red("error")}: exported name not found`, `${chalk.blue("-->")} ${filename}`].join("\n"),
);
}
|