From 231be9a6748e57cf257c80799b52fdb862ebabb0 Mon Sep 17 00:00:00 2001 From: fisker Date: Thu, 6 Jun 2024 20:25:35 +0800 Subject: [PATCH] Merge "space" config --- browser.js | 36 +++++++++++++++---------------- package.json | 8 +++++-- readme.md | 21 +++++++++++++++++- space-browser.js | 19 ++++++++++++++++ space.js | 19 ++++++++++++++++ test/test.js | 56 ++++++++++++++++++++++++++++++++++++++++++------ 6 files changed, 131 insertions(+), 28 deletions(-) create mode 100644 space-browser.js create mode 100644 space.js diff --git a/browser.js b/browser.js index a8a1e86..9aa9e41 100644 --- a/browser.js +++ b/browser.js @@ -2,24 +2,24 @@ import globals from 'globals'; import confusingBrowserGlobals from 'confusing-browser-globals'; import eslintConfigXo from './index.js'; -const [nodeConfig] = eslintConfigXo; +const [config] = eslintConfigXo; -const config = { - ...nodeConfig, - languageOptions: { - ...nodeConfig.languageOptions, - globals: { - ...globals.es2021, - ...globals.browser, +export default [ + { + ...config, + languageOptions: { + ...config.languageOptions, + globals: { + ...globals.es2021, + ...globals.browser, + }, + }, + rules: { + ...config.rules, + 'no-restricted-globals': [ + 'error', + ...confusingBrowserGlobals, + ], }, }, - rules: { - ...nodeConfig.rules, - 'no-restricted-globals': [ - 'error', - ...confusingBrowserGlobals, - ], - }, -}; - -export default [config]; +]; diff --git a/package.json b/package.json index a090eb8..efa0cdd 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,9 @@ "type": "module", "exports": { ".": "./index.js", - "./browser": "./browser.js" + "./browser": "./browser.js", + "./space": "./space.js", + "./space/browser": "./space-browser.js" }, "repository": "xojs/eslint-config-xo", "funding": "https://github.com/sponsors/sindresorhus", @@ -24,7 +26,9 @@ }, "files": [ "index.js", - "browser.js" + "browser.js", + "space.js", + "space-browser.js" ], "keywords": [ "eslintconfig", diff --git a/readme.md b/readme.md index 0e3b710..5d58840 100644 --- a/readme.md +++ b/readme.md @@ -26,7 +26,6 @@ export default [ ]; ``` - This package also exposes [`eslint-config-xo/browser`](browser.js) if you're in the browser: ```js @@ -37,6 +36,26 @@ export default [ ]; ``` +This package also exposes [`eslint-config-xo/space`](space.js) if you're in favor of 2-space indent: + +```js +import eslintConfigXoSpace from 'eslint-config-xo/space'; + +export default [ + ...eslintConfigXoSpace, +]; +``` + +This package also exposes [`eslint-config-xo/space`](space-browser.js) if you're in favor of 2-space indent and in browser: + +```js +import eslintConfigXoSpaceBrowser from 'eslint-config-xo/space/browser'; + +export default [ + ...eslintConfigXoSpaceBrowser, +]; +``` + ## Use the XO CLI instead XO is an ESLint wrapper with great defaults. diff --git a/space-browser.js b/space-browser.js new file mode 100644 index 0000000..551a610 --- /dev/null +++ b/space-browser.js @@ -0,0 +1,19 @@ +import eslintConfigXoBrowser from './browser.js'; + +const [config] = eslintConfigXoBrowser; + +export default [ + { + ...config, + rules: { + ...config.rules, + indent: [ + 'error', + 2, + { + SwitchCase: 1, + }, + ], + }, + }, +]; diff --git a/space.js b/space.js new file mode 100644 index 0000000..f701655 --- /dev/null +++ b/space.js @@ -0,0 +1,19 @@ +import eslintConfigXo from './index.js'; + +const [config] = eslintConfigXo; + +export default [ + { + ...config, + rules: { + ...config.rules, + indent: [ + 'error', + 2, + { + SwitchCase: 1, + }, + ], + }, + }, +]; diff --git a/test/test.js b/test/test.js index 57c114d..472130c 100644 --- a/test/test.js +++ b/test/test.js @@ -2,6 +2,8 @@ import test from 'ava'; 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); @@ -16,16 +18,56 @@ async function runEslint(string, config) { return firstResult.messages; } -test('main', async t => { - t.true(Array.isArray(eslintConfigXoNode)); +test('node', async t => { + for (const config of [eslintConfigXoNode, eslintConfigXoSpaceNode]) { + t.true(Array.isArray(config)); - const errors = await runEslint('\'use strict\';\nconsole.log("unicorn")\n', eslintConfigXoNode); - 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 => { - t.true(Array.isArray(eslintConfigXoBrowser)); + for (const config of [eslintConfigXoBrowser, eslintConfigXoSpaceBrowser]) { + t.true(Array.isArray(config)); - const errors = await runEslint('\'use strict\';\nprocess.exit();\n', eslintConfigXoBrowser); - t.true(hasRule(errors, 'no-undef'), JSON.stringify(errors)); + // 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)); + } +}); + +test('space', async t => { + const fixture = ` +export function foo() { +\treturn true; +} +`.trim(); + + 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)); + } });