@oxlint/plugins 1.43.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +95 -0
- package/index.cjs +110 -0
- package/index.d.ts +3954 -0
- package/index.js +110 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present VoidZero Inc. & Contributors
|
|
4
|
+
Copyright (c) 2023 Boshen
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# @oxlint/plugins
|
|
2
|
+
|
|
3
|
+
Plugin utilities for [Oxlint](https://oxc.rs/docs/guide/usage/linter/js-plugins).
|
|
4
|
+
|
|
5
|
+
This package provides optional functions to assist in creating Oxlint JS plugins and rules.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @oxlint/plugins
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Define functions
|
|
16
|
+
|
|
17
|
+
Use `definePlugin` and `defineRule` if authoring your plugin in TypeScript for type safety.
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { definePlugin, defineRule } from "@oxlint/plugins";
|
|
21
|
+
|
|
22
|
+
const rule = defineRule({
|
|
23
|
+
create(context) {
|
|
24
|
+
return {
|
|
25
|
+
Program(node) {
|
|
26
|
+
// Rule logic here
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
export default definePlugin({
|
|
33
|
+
meta: { name: "oxlint-plugin-amazing" },
|
|
34
|
+
rules: { amazing: rule },
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Types
|
|
39
|
+
|
|
40
|
+
This package also includes types for plugins and rules.
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import type { Context, Rule, ESTree } from "@oxlint/plugins";
|
|
44
|
+
|
|
45
|
+
const rule: Rule = {
|
|
46
|
+
create(context: Context) {
|
|
47
|
+
return {
|
|
48
|
+
Program(node: ESTree.Program) {
|
|
49
|
+
// Rule logic here
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### ESLint compatibility
|
|
57
|
+
|
|
58
|
+
If your plugin uses Oxlint's [alternative `createOnce` API](https://oxc.rs/docs/guide/usage/linter/js-plugins#alternative-api),
|
|
59
|
+
use `eslintCompatPlugin` to convert the plugin so it will also work with ESLint.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { eslintCompatPlugin } from "@oxlint/plugins";
|
|
63
|
+
|
|
64
|
+
const rule = {
|
|
65
|
+
createOnce(context) {
|
|
66
|
+
return {
|
|
67
|
+
Program(node) {
|
|
68
|
+
// Rule logic here
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export default eslintCompatPlugin({
|
|
75
|
+
meta: { name: "oxlint-plugin-amazing" },
|
|
76
|
+
rules: { amazing: rule },
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Node.js version
|
|
81
|
+
|
|
82
|
+
This package requires Node.js 12.22.0+, 14.17.0+, 16.0.0+, or later.
|
|
83
|
+
This matches the minimum Node.js version required by ESLint 8.
|
|
84
|
+
|
|
85
|
+
This package provides both ESM and CommonJS entry points.
|
|
86
|
+
|
|
87
|
+
So a plugin which depends on `@oxlint/plugins` can be:
|
|
88
|
+
|
|
89
|
+
- Used with any version of Oxlint.
|
|
90
|
+
- Used with ESLint 8+.
|
|
91
|
+
- Published as either ESM or CommonJS.
|
|
92
|
+
|
|
93
|
+
## Docs
|
|
94
|
+
|
|
95
|
+
For full documentation, see [Oxlint JS Plugins docs](https://oxc.rs/docs/guide/usage/linter/js-plugins).
|
package/index.cjs
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
function definePlugin(plugin) {
|
|
2
|
+
return plugin;
|
|
3
|
+
}
|
|
4
|
+
function defineRule(rule) {
|
|
5
|
+
return rule;
|
|
6
|
+
}
|
|
7
|
+
const EMPTY_VISITOR = {};
|
|
8
|
+
function eslintCompatPlugin(plugin) {
|
|
9
|
+
if (typeof plugin != "object" || !plugin) throw Error("Plugin must be an object");
|
|
10
|
+
let { rules } = plugin;
|
|
11
|
+
if (typeof rules != "object" || !rules) throw Error("Plugin must have an object as `rules` property");
|
|
12
|
+
for (let ruleName in rules) Object.hasOwn(rules, ruleName) && convertRule(rules[ruleName]);
|
|
13
|
+
return plugin;
|
|
14
|
+
}
|
|
15
|
+
function convertRule(rule) {
|
|
16
|
+
if (typeof rule != "object" || !rule) throw Error("Rule must be an object");
|
|
17
|
+
if ("create" in rule) return;
|
|
18
|
+
let context = null, visitor, beforeHook;
|
|
19
|
+
rule.create = (eslintContext) => (context === null && ({context, visitor, beforeHook} = createContextAndVisitor(rule)), Object.defineProperties(context, {
|
|
20
|
+
id: { value: eslintContext.id },
|
|
21
|
+
options: { value: eslintContext.options },
|
|
22
|
+
report: { value: eslintContext.report }
|
|
23
|
+
}), Object.setPrototypeOf(context, Object.getPrototypeOf(eslintContext)), beforeHook !== null && beforeHook() === !1 ? EMPTY_VISITOR : visitor);
|
|
24
|
+
}
|
|
25
|
+
const FILE_CONTEXT = Object.freeze({
|
|
26
|
+
get filename() {
|
|
27
|
+
throw Error("Cannot access `context.filename` in `createOnce`");
|
|
28
|
+
},
|
|
29
|
+
getFilename() {
|
|
30
|
+
throw Error("Cannot call `context.getFilename` in `createOnce`");
|
|
31
|
+
},
|
|
32
|
+
get physicalFilename() {
|
|
33
|
+
throw Error("Cannot access `context.physicalFilename` in `createOnce`");
|
|
34
|
+
},
|
|
35
|
+
getPhysicalFilename() {
|
|
36
|
+
throw Error("Cannot call `context.getPhysicalFilename` in `createOnce`");
|
|
37
|
+
},
|
|
38
|
+
get cwd() {
|
|
39
|
+
throw Error("Cannot access `context.cwd` in `createOnce`");
|
|
40
|
+
},
|
|
41
|
+
getCwd() {
|
|
42
|
+
throw Error("Cannot call `context.getCwd` in `createOnce`");
|
|
43
|
+
},
|
|
44
|
+
get sourceCode() {
|
|
45
|
+
throw Error("Cannot access `context.sourceCode` in `createOnce`");
|
|
46
|
+
},
|
|
47
|
+
getSourceCode() {
|
|
48
|
+
throw Error("Cannot call `context.getSourceCode` in `createOnce`");
|
|
49
|
+
},
|
|
50
|
+
get languageOptions() {
|
|
51
|
+
throw Error("Cannot access `context.languageOptions` in `createOnce`");
|
|
52
|
+
},
|
|
53
|
+
get settings() {
|
|
54
|
+
throw Error("Cannot access `context.settings` in `createOnce`");
|
|
55
|
+
},
|
|
56
|
+
extend(extension) {
|
|
57
|
+
return Object.freeze(Object.assign(Object.create(this), extension));
|
|
58
|
+
},
|
|
59
|
+
get parserOptions() {
|
|
60
|
+
throw Error("Cannot access `context.parserOptions` in `createOnce`");
|
|
61
|
+
},
|
|
62
|
+
get parserPath() {
|
|
63
|
+
throw Error("Cannot access `context.parserPath` in `createOnce`");
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
function createContextAndVisitor(rule) {
|
|
67
|
+
let { createOnce } = rule;
|
|
68
|
+
if (createOnce == null) throw Error("Rules must define either a `create` or `createOnce` method");
|
|
69
|
+
if (typeof createOnce != "function") throw Error("Rule `createOnce` property must be a function");
|
|
70
|
+
let context = Object.create(FILE_CONTEXT, {
|
|
71
|
+
id: {
|
|
72
|
+
value: "",
|
|
73
|
+
enumerable: !0,
|
|
74
|
+
configurable: !0
|
|
75
|
+
},
|
|
76
|
+
options: {
|
|
77
|
+
value: null,
|
|
78
|
+
enumerable: !0,
|
|
79
|
+
configurable: !0
|
|
80
|
+
},
|
|
81
|
+
report: {
|
|
82
|
+
value: null,
|
|
83
|
+
enumerable: !0,
|
|
84
|
+
configurable: !0
|
|
85
|
+
}
|
|
86
|
+
}), { before: beforeHook, after: afterHook, ...visitor } = createOnce.call(rule, context);
|
|
87
|
+
if (beforeHook === void 0) beforeHook = null;
|
|
88
|
+
else if (beforeHook !== null && typeof beforeHook != "function") throw Error("`before` property of visitor must be a function if defined");
|
|
89
|
+
if (afterHook != null) {
|
|
90
|
+
if (typeof afterHook != "function") throw Error("`after` property of visitor must be a function if defined");
|
|
91
|
+
let maxAttrs = -1;
|
|
92
|
+
for (let key in visitor) {
|
|
93
|
+
if (!Object.hasOwn(visitor, key) || !key.endsWith(":exit")) continue;
|
|
94
|
+
let end = key.length - 5, count = 0;
|
|
95
|
+
for (let i = 0; i < end; i++) {
|
|
96
|
+
let c = key.charCodeAt(i);
|
|
97
|
+
(c === 91 || c === 46 || c === 58) && count++;
|
|
98
|
+
}
|
|
99
|
+
count > maxAttrs && (maxAttrs = count);
|
|
100
|
+
}
|
|
101
|
+
let key = `Program${"[type]".repeat(maxAttrs + 1)}:exit`;
|
|
102
|
+
visitor[key] = (_node) => afterHook();
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
context,
|
|
106
|
+
visitor,
|
|
107
|
+
beforeHook
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
exports.definePlugin = definePlugin, exports.defineRule = defineRule, exports.eslintCompatPlugin = eslintCompatPlugin;
|