Skip to content

Commit

Permalink
Migrate to flat config (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Aug 3, 2024
1 parent e669d26 commit ec210f2
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 66 deletions.
1 change: 0 additions & 1 deletion .eslintrc.cjs

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 22
- 20
- 18
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand Down
38 changes: 23 additions & 15 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
'use strict';
const path = require('path');
const confusingBrowserGlobals = require('confusing-browser-globals');
import globals from 'globals';
import confusingBrowserGlobals from 'confusing-browser-globals';
import eslintConfigXo from './index.js';

module.exports = {
extends: path.join(__dirname, 'index.js'),
env: {
node: false,
browser: true,
},
rules: {
'no-restricted-globals': [
'error',
...confusingBrowserGlobals,
],
const [config] = eslintConfigXo;

export default [
{
...config,
languageOptions: {
...config.languageOptions,
globals: {
...globals.es2021,
...globals.browser,
},
},
rules: {
...config.rules,
'no-restricted-globals': [
'error',
...confusingBrowserGlobals,
],
},
},
};
];
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default} from './index.js';
25 changes: 15 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
'use strict';
import globals from 'globals';

module.exports = {
parserOptions: {
ecmaVersion: 'latest',
const config = {
languageOptions: {
sourceType: 'module',
ecmaFeatures: {
jsx: true,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
globals: {
...globals.es2021,
...globals.node,
},
},
env: {
es2021: true,
node: true,
linterOptions: {
reportUnusedDisableDirectives: 'error',
},
reportUnusedDisableDirectives: true,
rules: {
'comma-dangle': [
'error',
Expand Down Expand Up @@ -693,3 +696,5 @@ module.exports = {
],
},
};

export default [config];
19 changes: 14 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
"version": "0.45.0",
"description": "ESLint shareable config for XO",
"license": "MIT",
"type": "module",
"exports": {
".": "./index.js",
"./browser": "./browser.js",
"./space": "./space.js",
"./space/browser": "./space-browser.js"
},
"repository": "xojs/eslint-config-xo",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
Expand All @@ -19,7 +26,9 @@
},
"files": [
"index.js",
"browser.js"
"browser.js",
"space.js",
"space-browser.js"
],
"keywords": [
"eslintconfig",
Expand Down Expand Up @@ -50,12 +59,12 @@
"simple"
],
"dependencies": {
"confusing-browser-globals": "1.0.11"
"confusing-browser-globals": "1.0.11",
"globals": "^15.3.0"
},
"devDependencies": {
"ava": "^2.4.0",
"eslint": "^8.56.0",
"is-plain-obj": "^3.0.0"
"ava": "^6.1.3",
"eslint": "^9.4.0"
},
"peerDependencies": {
"eslint": ">=8.56.0"
Expand Down
51 changes: 32 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,44 @@ npm install --save-dev eslint-config-xo

## Usage

Add some ESLint config to your `package.json`:

```json
{
"name": "my-awesome-project",
"eslintConfig": {
"extends": "xo"
}
}
Add some ESLint config to your `eslint.config.js`:

```js
import eslintConfigXo from 'eslint-config-xo';

export default [
...eslintConfigXo,
];
```

This package also exposes [`eslint-config-xo/browser`](browser.js) if you're in the browser:

```js
import eslintConfigXoBrowser from 'eslint-config-xo/browser';

export default [
...eslintConfigXoBrowser,
];
```

Or to `.eslintrc`:
This package also exposes [`eslint-config-xo/space`](space.js) if you're in favor of 2-space indent:

```json
{
"extends": "xo"
}
```js
import eslintConfigXoSpace from 'eslint-config-xo/space';

export default [
...eslintConfigXoSpace,
];
```

This package also exposes [`xo/browser`](browser.js) if you're in the browser:
This package also exposes [`eslint-config-xo/space/browser`](space-browser.js) if you're in favor of 2-space indent and in browser:

```js
import eslintConfigXoSpaceBrowser from 'eslint-config-xo/space/browser';

```json
{
"extends": "xo/browser"
}
export default [
...eslintConfigXoSpaceBrowser,
];
```

## Use the XO CLI instead
Expand Down
19 changes: 19 additions & 0 deletions space-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import eslintConfigXoBrowser from './browser.js';

const [config] = eslintConfigXoBrowser;

export default [
{
...config,
rules: {
...config.rules,
indent: [
'error',
2,
{
SwitchCase: 1,
},
],
},
},
];
19 changes: 19 additions & 0 deletions space.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import eslintConfigXo from './index.js';

const [config] = eslintConfigXo;

export default [
{
...config,
rules: {
...config.rules,
indent: [
'error',
2,
{
SwitchCase: 1,
},
],
},
},
];
64 changes: 51 additions & 13 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import test from 'ava';
import isPlainObj from 'is-plain-obj';
import {ESLint} from 'eslint';
import eslintConfigXoNode from '../index.js';
import eslintConfigXoBrowser from '../browser.js';
import eslintConfigXoSpaceNode from '../space.js';
import eslintConfigXoSpaceBrowser from '../space-browser.js';

const hasRule = (errors, ruleId) => errors.some(error => error.ruleId === ruleId);

async function runEslint(string, config) {
const eslint = new ESLint({
useEslintrc: false,
overrideConfigFile: true,
overrideConfig: config,
});

Expand All @@ -15,21 +18,56 @@ async function runEslint(string, config) {
return firstResult.messages;
}

test('main', async t => {
const config = require('../index.js');
test('node', async t => {
for (const config of [eslintConfigXoNode, eslintConfigXoSpaceNode]) {
t.true(Array.isArray(config));

t.true(isPlainObj(config));
t.true(isPlainObj(config.rules));

const errors = await runEslint('\'use strict\';\nconsole.log("unicorn")\n', config);
t.true(hasRule(errors, 'quotes'), JSON.stringify(errors));
// eslint-disable-next-line no-await-in-loop
const errors = await runEslint('\'use strict\';\nconsole.log("unicorn")\n', config);
t.true(hasRule(errors, 'quotes'), JSON.stringify(errors));
}
});

test('browser', async t => {
const config = require('../browser.js');
for (const config of [eslintConfigXoBrowser, eslintConfigXoSpaceBrowser]) {
t.true(Array.isArray(config));

// eslint-disable-next-line no-await-in-loop
const errors = await runEslint('\'use strict\';\nprocess.exit();\n', config);
t.true(hasRule(errors, 'no-undef'), JSON.stringify(errors));
}
});

t.true(isPlainObj(config));
test('space', async t => {
const fixture = `
export function foo() {
\treturn true;
}
`.trim();

const errors = await runEslint('\'use strict\';\nprocess.exit();\n', config);
t.true(hasRule(errors, 'no-undef'), JSON.stringify(errors));
for (const {
expected,
config,
} of [
{
config: eslintConfigXoSpaceNode,
expected: true,
},
{
config: eslintConfigXoSpaceBrowser,
expected: true,
},
{
config: eslintConfigXoNode,
expected: false,
},
{
config: eslintConfigXoBrowser,
expected: false,
},
]) {
// eslint-disable-next-line no-await-in-loop
const errors = await runEslint(fixture, config);
t.is(hasRule(errors, 'indent'), expected, JSON.stringify(errors));
}
});

0 comments on commit ec210f2

Please sign in to comment.