Rich Harris 6 months ago
parent 7e62ef0981
commit 53ab2aa4f2

@ -1,7 +1,7 @@
// @ts-check
import { eslintCompatPlugin } from '@oxlint/plugins';
import { definePlugin } from '@oxlint/plugins';
const plugin = eslintCompatPlugin({
export default definePlugin({
meta: {
name: 'custom'
},
@ -11,24 +11,32 @@ const plugin = eslintCompatPlugin({
// ensures that TypeScript does not pick up more ambient types
// (for example from Node) that shouldn't be available in the browser.
'no-compiler-imports': {
create(context) {
createOnce(context) {
return {
Program: () => {
before() {
// Skip files in the compiler directory - this rule is only for runtime code
const filename = context.filename || context.getFilename?.();
if (filename && filename.includes('/src/compiler/')) {
return;
if (context.filename.includes('/src/compiler/')) {
return false;
}
// Do a simple string search because ESLint doesn't provide a way to check JSDoc comments.
},
Program: () => {
// Do a simple string search because Oxlint doesn't provide a way to check JSDoc comments.
// The string search could in theory yield false positives, but in practice it's unlikely.
const text = context.sourceCode.getText();
const idx = Math.max(text.indexOf('../compiler/'), text.indexOf('#compiler'));
if (idx !== -1) {
const match = /(?:(\.\.\/)(compiler)\/|(#compiler))/.exec(text);
if (match) {
const offset = match[1]?.length ?? 0;
const length = (match[2] || match[3]).length;
const start = match.index + offset;
const end = start + length;
context.report({
loc: {
start: context.sourceCode.getLocFromIndex(idx),
end: context.sourceCode.getLocFromIndex(idx + 12)
start: context.sourceCode.getLocFromIndex(start),
end: context.sourceCode.getLocFromIndex(end)
},
message:
'References to compiler code are forbidden in runtime code (both for type and value imports)'
@ -40,5 +48,3 @@ const plugin = eslintCompatPlugin({
}
}
});
export default plugin;

Loading…
Cancel
Save