Skip to content

Commit

Permalink
chore: add eslint plugin
Browse files Browse the repository at this point in the history
crimx committed Jan 22, 2025
1 parent 067e031 commit 6580327
Showing 6 changed files with 574 additions and 1,594 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -401,6 +401,23 @@ dispose.add(timeout(() => console.log("timeout"), 1000));
// The `timeout` disposer will be removed from the `dispose` after 1s.
```

## Eslint Plugin

This package comes with a eslint plugin:

```
// eslint.config.mjs
import disposable from "@wopjs/disposable/eslint-plugin.js";
export default [
disposable.recommended
]
```

Rules:

- `disposable/readonly-dispose`: Enforce `dispose` method to be `readonly`.

## License

MIT @ [wopjs](https://github.com/wopjs)
42 changes: 42 additions & 0 deletions eslint-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const plugin = {
meta: {
name: "eslint-plugin-disposable",
},
rules: {
"readonly-dispose": {
create(context) {
function checkProperty(node) {
context.report({
fix(fixer) {
return fixer.insertTextBefore(node, "readonly ");
},
message: `Use readonly for Disposable to avoid leaks through accidental reassignment.`,
node,
});
}

return {
"PropertyDefinition[readonly!=true][key.name=dispose][typeAnnotation.typeAnnotation.typeName.name=/Disposer|IDisposable|DisposableType|DisposableDisposer|DisposableStore|DisposableMap|DisposableOne/]":
checkProperty,
"PropertyDefinition[readonly!=true][key.name=dispose][value.type=CallExpression][value.callee.name=/disposableStore|disposableMap|disposableOne/]":
checkProperty,
};
},
meta: {
fixable: "code",
},
},
},
};

module.exports = {
plugin,
recommended: {
plugins: {
disposable: plugin,
},
rules: {
"disposable/readonly-dispose": "error",
},
},
};
92 changes: 7 additions & 85 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,87 +1,9 @@
import jsEslint from "@eslint/js";
import eslintConfigPrettier from "eslint-config-prettier";
import importPlugin from "eslint-plugin-import";
import tsEslint from "typescript-eslint";
import wopjs, { defineConfig } from "@wopjs/eslint-config";

export default tsEslint.config(
jsEslint.configs.recommended,
...tsEslint.configs.recommended,
importPlugin.flatConfigs.recommended,
importPlugin.flatConfigs.typescript,
eslintConfigPrettier,
{
ignores: ["**/dist/", "**/public/", "**/docs/", "**/node_modules/"],
},
{
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
globals: {
console: true,
process: true,
},
},
rules: {
"import/no-unresolved": "off",
"import/newline-after-import": [
"error",
{ considerComments: true, count: 1 },
],
"import/consistent-type-specifier-style": ["error", "prefer-top-level"],
"import/no-duplicates": ["error", { considerQueryString: true }],
"import/order": [
"error",
{
"newlines-between": "always",
alphabetize: { order: "asc", caseInsensitive: true },
groups: [
"object",
"type",
["builtin", "external", "internal"],
["parent", "sibling", "index"],
],
pathGroups: [
{
pattern: "*.+(scss|css|less)",
patternOptions: { matchBase: true },
group: "object",
},
{ pattern: "~/**", group: "internal", position: "after" },
{ pattern: "../**", group: "parent", position: "before" },
],
pathGroupsExcludedImportTypes: [
"builtin",
"external",
"object",
"type",
],
distinctGroup: false,
},
],
},
import disposable from "./eslint-plugin.js";

export default defineConfig(...wopjs, disposable.recommended, {
rules: {
"@typescript-eslint/no-explicit-any": "off",
},
{
files: ["**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}"],
plugins: {
"@typescript-eslint": tsEslint.plugin,
},
languageOptions: {
parser: tsEslint.parser,
},
rules: {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/consistent-type-imports": "error",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
destructuredArrayIgnorePattern: "^_",
ignoreRestSiblings: true,
},
],
},
}
);
});
Loading

0 comments on commit 6580327

Please sign in to comment.