Skip to content

Commit

Permalink
Merge pull request #732 from davidtaylorhq/eslint9-support
Browse files Browse the repository at this point in the history
Add support for eslint 9
  • Loading branch information
NullVoxPopuli authored Nov 11, 2024
2 parents f69dd78 + 101261b commit 2cc7aa3
Show file tree
Hide file tree
Showing 18 changed files with 239 additions and 82 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-oranges-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-decorator-position': major
---

Adds support for ESLint 9 with flat configs, and drops ESLint 6 compatibility.
11 changes: 10 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,18 @@ jobs:
- { node: "16", pnpm: "7" }
# pnpm 9 only supports node >= 18
- { node: "18", pnpm: "9" }
- { node: "20", pnpm: "9" }
- { node: "22", pnpm: "9" }
eslint:
- "eslint@9"
- "eslint@8"
- "eslint@7"
- "eslint@6"
exclude:
- eslint: "eslint@9"
node: { node: "14", pnpm: "6" }
- eslint: "eslint@9"
node: { node: "16", pnpm: "7" }

steps:
- uses: actions/checkout@v4
- uses: wyvox/action-setup-pnpm@v3
Expand Down Expand Up @@ -104,6 +112,7 @@ jobs:
# tese are all relative to the smoke-tests directory
script:
- integration/position-default
- integration/flat-config
- integration/position-prettier
- integration/external-config-prettier
- examples/ember
Expand Down
82 changes: 79 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

### 1. Install plugin

```shell
pnpm add -D eslint-plugin-decorator-position
```

Or

```shell
yarn add --dev eslint-plugin-decorator-position
```
Expand All @@ -24,7 +30,28 @@ Or
npm install --save-dev eslint-plugin-decorator-position
```

### 2. Modify your `.eslintrc.js`
### 2. Modify your eslint config

For modern 'flat config', append the eslint-plugin-decorator-position config to your `eslint.config.js` file:

```diff
module.exports = [
+ ...require('eslint-plugin-decorator-position/config/recommended'),
{
languageOptions: {
parser: require('@babel/eslint-parser'),
parserOptions: {
requireConfigFile: false,
babelOptions: {
plugins: [['@babel/plugin-proposal-decorators', { legacy: true }]],
},
},
},
},
];
```

Or for the legacy eslint config format, extend the base config provided by eslint-plugin-decorator-position:

```javascript
// .eslintrc.js
Expand All @@ -47,6 +74,30 @@ module.exports = {
Since eslint 8, the printWidth option must be specified to be compatible
with the eslint-plugin-prettier rule `prettier/prettier`

```diff
// eslint.config.js
module.exports = [
...require('eslint-plugin-decorator-position/config/recommended'),
{
languageOptions: {
parser: require('@babel/eslint-parser'),
parserOptions: {
requireConfigFile: false,
babelOptions: {
plugins: [['@babel/plugin-proposal-decorators', { legacy: true }]],
},
},
},
},
+ rules: {
+ 'decorator-position/decorator-position': ['error', { printWidth: 100 }],
+ 'prettier/prettier': ['error', { printWidth: 100 }]
+ }
]
```

or

```javascript
// .eslintrc.js
module.exports = {
Expand All @@ -66,10 +117,35 @@ If there is a `.prettierrc.js` file, that will be read instead, and `printwidth`

## 🧰 Configurations

'Flat' configurations:
| | Name | Description |
|:---|:-----|:------------|
| | [base](./lib/config/base.js) | contains no rules settings, but the basic eslint configuration suitable for any project. You can use it to configure rules as you wish. |
| :hamster: | [ember](./lib/config/ember.js) | extends the `base` configuration by enabling the recommended rules for ember projects. |
| | [`/config/base`](./lib/config/base.js) | Only installs the plugin. You can use it to configure rules as you wish. |
| | [`/config/rules`](./lib/config/rules.js) | Only configures the rules. Expects the plugin to be installed. |
| | [`/config/recommended`](./lib/config/recommended.js) | Installs the plugin and configures the rules |

Legacy configurations:
| | Name | Description |
|:---|:-----|:------------|
| | [base](./lib/config-legacy/base.js) | contains no rules settings, but the basic eslint configuration suitable for any project. You can use it to configure rules as you wish. |
| :hamster: | [ember](./lib/config-legacy/ember.js) | extends the `base` configuration by enabling the recommended rules for ember projects. |

## Manual plugin installation

If you prefer to manage the config yourself, the plugin can be installed like this:

```diff
// eslint.config.js
module.exports = [
// ... your existing config
plugins: {
+ "decorator-position": require("eslint-plugin-decorator-position"),
},
rules: {
+ 'decorator-position/decorator-position': 'error',
}
]
```

## 🍟 Rules

Expand Down
50 changes: 50 additions & 0 deletions lib/config-legacy/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { resolve } = require('path');

// Eslint v8+ does not have CLIEngine.
const { CLIEngine: v7AndEarlier } = require('eslint');

const isEslint7 = Boolean(v7AndEarlier);

const esLint7Config = {
root: true,

parser: 'babel-eslint',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
legacyDecorators: true,
},
},

env: {
browser: true,
es6: true,
},

plugins: ['decorator-position'],
};

const esLint8Config = {
root: true,
parser: '@babel/eslint-parser',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
legacyDecorators: true,
},
babelOptions: {
configFile: resolve(__dirname, '../../babel.config.cjs'),
},
},

env: {
browser: true,
es6: true,
},

plugins: ['decorator-position'],
};

module.exports = isEslint7 ? esLint7Config : esLint8Config;
File renamed without changes.
53 changes: 6 additions & 47 deletions lib/config/base.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,9 @@
const { resolve } = require('path');
const DecoratorPosition = require('../index');

// Eslint v8+ does not have CLIEngine.
const { CLIEngine: v7AndEarlier } = require('eslint');

const isEslint7 = Boolean(v7AndEarlier);

const esLint7Config = {
root: true,

parser: 'babel-eslint',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
legacyDecorators: true,
},
},

env: {
browser: true,
es6: true,
},

plugins: ['decorator-position'],
};

const esLint8Config = {
root: true,
parser: '@babel/eslint-parser',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
legacyDecorators: true,
},
babelOptions: {
configFile: resolve(__dirname, '../../babel.config.cjs'),
module.exports = [
{
plugins: {
'decorator-position': DecoratorPosition,
},
},

env: {
browser: true,
es6: true,
},

plugins: ['decorator-position'],
};

module.exports = isEslint7 ? esLint7Config : esLint8Config;
];
1 change: 1 addition & 0 deletions lib/config/recommended.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = [...require('./base'), ...require('./rules')];
7 changes: 7 additions & 0 deletions lib/config/rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = [
{
rules: {
'decorator-position/decorator-position': ['error', {}],
},
},
];
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
'decorator-position': require('./rules/decorator-position'),
},
configs: {
base: require('./config/base'),
ember: require('./config/ember'),
base: require('./config-legacy/base'),
ember: require('./config-legacy/ember'),
},
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "lib/index.js",
"exports": {
".": "./lib/index.js",
"./lib/rules/decorator-position": "./lib/rules/decorator-position.js"
"./lib/rules/decorator-position": "./lib/rules/decorator-position.js",
"./config/*": "./lib/config/*.js"
},
"files": [
"lib",
Expand Down Expand Up @@ -83,7 +84,7 @@
},
"peerDependencies": {
"@babel/eslint-parser": "^7.18.2",
"eslint": "^6.0.0 || ^7.31.0 || ^8.0.0"
"eslint": "^7.31.0 || ^8.0.0 || ^9.0.0"
},
"peerDependenciesMeta": {
"@babel/eslint-parser": {
Expand Down
4 changes: 1 addition & 3 deletions scripts/-pnpm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ function testWithPnpm() {
echo "PWD: $(pwd)"
node_modules/.bin/eslint . \
--no-ignore \
--no-eslintrc \
--config $config_path \
--fix \
--ext js,ts
--fix

git diff --exit-code ./
}
Expand Down
6 changes: 2 additions & 4 deletions scripts/-yarn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function testWithYarn() {

echo "Running tests for $target with yarn"

config_path=".eslintrc.js"
config_path=$(test -f ".eslintrc.js" && echo ".eslintrc.js" || echo "eslint.config.js")

echo ""
echo ""
Expand All @@ -42,10 +42,8 @@ function testWithYarn() {
echo "$(pwd)"
node_modules/.bin/eslint . \
--no-ignore \
--no-eslintrc \
--config $config_path \
--fix \
--ext js,ts
--fix

git diff --exit-code ./
}
25 changes: 25 additions & 0 deletions smoke-tests/integration/flat-config/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export class Foo {
@attr title;

@belongsTo('user') author;
@hasMany('comments') comments;

@tracked uninitialized;
@tracked initialized = 2;


@foo
get myMethod() {

}

@foo
myMethod2() {

}

@foo
async myMethod3() {

}
}
14 changes: 14 additions & 0 deletions smoke-tests/integration/flat-config/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = [
{
languageOptions: {
parser: require('@babel/eslint-parser'),
parserOptions: {
requireConfigFile: false,
babelOptions: {
plugins: [['@babel/plugin-proposal-decorators', { legacy: true }]],
},
},
},
},
...require('eslint-plugin-decorator-position/config/recommended'),
];
13 changes: 13 additions & 0 deletions smoke-tests/integration/flat-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "test",
"version": "0.0.0",
"description": "smoke-test",
"license": "MIT",
"private": true,
"packageManager": "[email protected]",
"dependencies": {
"babel-eslint": "^10.0.3",
"eslint-plugin-decorator-position": "*",
"eslint": "^9.0.0"
}
}
Loading

0 comments on commit 2cc7aa3

Please sign in to comment.