mirror of
https://github.com/oxc-project/oxc.git
synced 2026-09-14 19:36:11 +08:00
8c7ea7678d
## Summary - keep UTF-8 translation offsets absolute when the converter is built from source with a trimmed BOM or partial-source prefix - extend the Oxlint BOM fixture with the reported comment-before-Unicode case - assert the JS plugin API returns the complete comment range and source slice Without the fix, the integration snapshot reports an end offset of 4 and slices `"/*x*"`. With the fix, it reports an end offset of 5 and slices `"/*x*/"`. Fixes #26162 *Update*: #26173 fixed the same bug in the same way, and has been merged. This PR adds more tests, so merging it on top. Co-authored-by: camc314 <18101008+camc314@users.noreply.github.com>
47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
import assert from "node:assert";
|
|
|
|
import type { Plugin } from "#oxlint/plugins";
|
|
|
|
const plugin: Plugin = {
|
|
meta: {
|
|
name: "bom-plugin",
|
|
},
|
|
rules: {
|
|
bom: {
|
|
create(context) {
|
|
// Check file has not been formatted by accident.
|
|
// We want the fixture files not to have trailing whitespace to check diagnostics at very end of file.
|
|
const sourceText = context.sourceCode.text;
|
|
assert(sourceText.endsWith(";"), "Fixture file has been formatted");
|
|
|
|
return {
|
|
Program(node) {
|
|
const comments = context.sourceCode.getAllComments().map((comment) => ({
|
|
start: comment.start,
|
|
end: comment.end,
|
|
text: sourceText.slice(comment.start, comment.end),
|
|
}));
|
|
context.report({
|
|
message:
|
|
"\n"
|
|
+ `hasBOM: ${context.sourceCode.hasBOM}\n`
|
|
+ `sourceText: ${JSON.stringify(sourceText)}\n`
|
|
+ `Program span: ${node.start}-${node.end}`
|
|
+ (comments.length === 0 ? "" : `\ncomments: ${JSON.stringify(comments)}`),
|
|
node,
|
|
});
|
|
},
|
|
DebuggerStatement(node) {
|
|
context.report({
|
|
message: `Debugger statement at ${node.start}-${node.end}`,
|
|
node,
|
|
});
|
|
},
|
|
};
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
export default plugin;
|