Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add eslint@9 support and flat config #3942

Merged
merged 9 commits into from
Oct 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add eslint@9 and flat config
  • Loading branch information
dartess committed Oct 16, 2024
commit 5c9f4bcd3d3b067fa3cb598b9b5e02be068f6625
31 changes: 29 additions & 2 deletions packages/eslint-plugin-mobx/README.md
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@ npm install --save-dev eslint @typescript-eslint/parser eslint-plugin-mobx

## Configuration

### Legacy Config

```javascript
// .eslintrc.js
module.exports = {
@@ -24,12 +26,37 @@ module.exports = {
"mobx/exhaustive-make-observable": "warn",
"mobx/unconditional-make-observable": "error",
"mobx/missing-make-observable": "error",
"mobx/missing-observer": "warn",
"mobx/no-anonymous-observer": "warn"
"mobx/missing-observer": "warn"
}
}
```

### Flat Config

```javascript
// eslint.config.js
import pluginMobx from "eslint-plugin-mobx";

export default [
// ...

// Either extend our recommended configuration:
pluginMobx.flatConfigs.recommended,

// ...or specify and customize individual rules:
{
plugins: { "mobx": pluginMobx },
rules: {
// these values are the same as recommended
"mobx/exhaustive-make-observable": "warn",
"mobx/unconditional-make-observable": "error",
"mobx/missing-make-observable": "error",
"mobx/missing-observer": "warn"
}
},
];
```

## Rules

### mobx/exhaustive-make-observable
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { RuleTester } from "eslint";
import { getRuleTester } from "./utils/getRuleTester";

import rule from "../src/exhaustive-make-observable.js";

const tester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {}
});
const tester = getRuleTester();

const decoratedFields = [
'@observable o = 5',
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { RuleTester } from "eslint";
import { getRuleTester } from "./utils/getRuleTester";

import rule from "../src/missing-make-observable.js";

const tester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {}
});
const tester = getRuleTester();

const fields = [
'@observable o = 5',
7 changes: 2 additions & 5 deletions packages/eslint-plugin-mobx/__tests__/missing-observer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { RuleTester } from "eslint"
import { getRuleTester } from "./utils/getRuleTester";

import rule from "../src/missing-observer.js"

const tester = new RuleTester({
parser: require.resolve("@typescript-eslint/parser"),
parserOptions: {}
})
const tester = getRuleTester();

const valids = [
"observer(function Named() { });",
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { RuleTester } from "eslint"
import { getRuleTester } from "./utils/getRuleTester";

import rule from "../src/no-anonymous-observer.js"

const tester = new RuleTester({
parser: require.resolve("@typescript-eslint/parser"),
parserOptions: {}
})
const tester = getRuleTester();

const valids = ["observer(function Name() {})", "observer(class Name {})"]

Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { RuleTester } from "eslint";
import { getRuleTester } from "./utils/getRuleTester";

import rule from "../src/unconditional-make-observable.js";

const tester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {}
});
const tester = getRuleTester();

const valid1 = {
code: `
29 changes: 29 additions & 0 deletions packages/eslint-plugin-mobx/__tests__/utils/getRuleTester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const version = process.env.ESLINT_V;

const { RuleTester } = require(`eslint-${version}`);
const typescriptEslintParser = require("@typescript-eslint/parser");

function getRuleTesterConfig() {
switch (version) {
case "7":
return {
parser: require.resolve("@typescript-eslint/parser"),
parserOptions: {},
};
case "9":
return {
languageOptions: {
parser: typescriptEslintParser,
parserOptions: {},
},
};
default:
throw new Error(`Unknown or unspecified ESLINT_V (${String(version)})`);
}
}

function getRuleTester() {
return new RuleTester(getRuleTesterConfig());
}

export { getRuleTester }
3 changes: 2 additions & 1 deletion packages/eslint-plugin-mobx/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const buildConfig = require("../../jest.base.config")

module.exports = buildConfig(__dirname, {
testRegex: "__tests__/.+\\.(t|j)sx?$"
setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
testRegex: "__tests__/[^/]+\\.(t|j)sx?$"
})
4 changes: 4 additions & 0 deletions packages/eslint-plugin-mobx/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @see https://github.com/jsdom/jsdom/issues/3363 */
global.structuredClone = val => {
return JSON.parse(JSON.stringify(val))
}
19 changes: 12 additions & 7 deletions packages/eslint-plugin-mobx/package.json
Original file line number Diff line number Diff line change
@@ -25,18 +25,21 @@
],
"homepage": "https://mobx.js.org/",
"peerDependencies": {
"eslint": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
"eslint": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0 || ^9.0.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^7.0.0",
"@babel/core": "^7.16.0",
"@babel/preset-env": "^7.16.4",
"rollup": "^2.60.2",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-node-resolve": "13.0.6"
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "13.0.6",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"cross-env": "^7.0.3",
"eslint-7": "npm:eslint@^7.0.0",
"eslint-9": "npm:eslint@^9.0.0",
"rollup": "^2.60.2"
},
"keywords": [
"eslint",
@@ -45,7 +48,9 @@
"mobx"
],
"scripts": {
"test": "jest",
"test:7": "cross-env ESLINT_V=7 jest",
"test:9": "cross-env ESLINT_V=9 jest",
"test": "npm run test:7 && npm run test:9",
"build": "yarn rollup --config",
"prepublishOnly": "yarn build"
}
2 changes: 2 additions & 0 deletions packages/eslint-plugin-mobx/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import babel from "@rollup/plugin-babel";
import json from "@rollup/plugin-json";
import pkg from "./package.json";

export default [
@@ -13,6 +14,7 @@ export default [
babelHelpers: "bundled",
exclude: "**/node_modules/**",
}),
json(),
urugator marked this conversation as resolved.
Show resolved Hide resolved
],
output: [
{ file: pkg.main, format: "cjs", exports: "auto" },
41 changes: 30 additions & 11 deletions packages/eslint-plugin-mobx/src/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
"use strict"

const { name, version } = require("../package.json");

const exhaustiveMakeObservable = require("./exhaustive-make-observable.js")
const unconditionalMakeObservable = require("./unconditional-make-observable.js")
const missingMakeObservable = require("./missing-make-observable.js")
const missingObserver = require("./missing-observer")
const noAnonymousObserver = require("./no-anonymous-observer.js")

module.exports = {
configs: {
recommended: {
plugins: ["mobx"],
rules: {
"mobx/exhaustive-make-observable": "warn",
"mobx/unconditional-make-observable": "error",
"mobx/missing-make-observable": "error",
"mobx/missing-observer": "warn"
}
}
const pluginMobx = {
meta: {
name,
version
},
rules: {
"exhaustive-make-observable": exhaustiveMakeObservable,
@@ -25,4 +20,28 @@ module.exports = {
"missing-observer": missingObserver,
"no-anonymous-observer": noAnonymousObserver
}
};

const recommendedRules = {
"mobx/exhaustive-make-observable": "warn",
"mobx/unconditional-make-observable": "error",
"mobx/missing-make-observable": "error",
"mobx/missing-observer": "warn"
}

module.exports = {
...pluginMobx,
configs: {
recommended: {
plugins: ["mobx"],
rules: recommendedRules,
}
},
flatConfigs: {
recommended: {
name: "react-hooks/recommended",
plugins: { "mobx": pluginMobx },
rules: recommendedRules
}
}
}
Loading
Loading