Skip to content

Commit

Permalink
Fix code style (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Jul 2, 2021
1 parent ab17a3c commit 0733cc5
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 28 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path';
import path from 'node:path';
import {ESLint} from 'eslint';
import globby from 'globby';
import {isEqual} from 'lodash-es';
Expand Down Expand Up @@ -169,11 +169,13 @@ const getFormatter = async name => {
return format;
};

export default {
const xo = {
getFormatter,
getErrorResults: ESLint.getErrorResults,
outputFixes: async ({results}) => ESLint.outputFixes(results),
getConfig,
lintText,
lintFiles,
};

export default xo;
5 changes: 3 additions & 2 deletions lib/open-report.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
import openEditor from 'open-editor';

const sortResults = (a, b) => a.errorCount + b.errorCount > 0 ? (a.errorCount - b.errorCount) : (a.warningCount - b.warningCount);
Expand Down Expand Up @@ -37,8 +36,10 @@ const getFiles = (report, predicate) => report.results
.sort(sortResults)
.map(result => resultToFile(result));

export default report => {
const openReport = report => {
const count = report.errorCount > 0 ? 'errorCount' : 'warningCount';
const files = getFiles(report, result => result[count] > 0);
openEditor(files);
};

export default openReport;
23 changes: 11 additions & 12 deletions lib/options-manager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
import os from 'os';
import path from 'path';
import os from 'node:os';
import path from 'node:path';
import fsExtra from 'fs-extra';
import arrify from 'arrify';
import {mergeWith, groupBy, flow, pick} from 'lodash-es';
Expand Down Expand Up @@ -43,7 +42,7 @@ const resolveFrom = (moduleId, fromDirectory = process.cwd()) => resolveModule(m
resolveFrom.silent = (moduleId, fromDirectory) => {
try {
return resolveFrom(moduleId, fromDirectory);
} catch { }
} catch {}
};

const resolveLocalConfig = name => resolveModule(normalizePackageName(name, 'eslint-config'), import.meta.url);
Expand Down Expand Up @@ -92,7 +91,7 @@ const getEmptyXOConfig = () => ({
const mergeFn = (previousValue, value, key) => {
if (Array.isArray(previousValue)) {
if (MERGE_OPTIONS_CONCAT.includes(key)) {
return previousValue.concat(value);
return [...previousValue, ...value];
}

return value;
Expand Down Expand Up @@ -186,7 +185,7 @@ const mergeWithFileConfigs = async (files, options, configFiles) => {

await Promise.all(Object.entries(groupBy(groups.filter(({options}) => Boolean(options.ts)), group => group.options.tsConfigPath || '')).map(
([tsConfigPath, groups]) => {
const files = [].concat(...groups.map(group => group.files));
const files = groups.flatMap(group => group.files);
const cachePath = getTsConfigCachePath(files, tsConfigPath);

for (const group of groups) {
Expand Down Expand Up @@ -386,7 +385,7 @@ const buildXOConfig = options => config => {

// Only apply if the user has the React plugin
if (options.cwd && resolveFrom.silent('eslint-plugin-react', options.cwd)) {
config.baseConfig.plugins = config.baseConfig.plugins.concat('react');
config.baseConfig.plugins.push('react');
config.baseConfig.rules['react/jsx-indent-props'] = ['error', spaces];
config.baseConfig.rules['react/jsx-indent'] = ['error', spaces];
}
Expand Down Expand Up @@ -452,10 +451,10 @@ const buildExtendsConfig = options => config => {
const buildPrettierConfig = (options, prettierConfig) => config => {
if (options.prettier) {
// The prettier plugin uses Prettier to format the code with `--fix`
config.baseConfig.plugins = config.baseConfig.plugins.concat('prettier');
config.baseConfig.plugins.push('prettier');

// The prettier config overrides ESLint stylistic rules that are handled by Prettier
config.baseConfig.extends = config.baseConfig.extends.concat('prettier');
config.baseConfig.extends.push('prettier');

// The `prettier/prettier` rule reports errors if the code is not formatted in accordance to Prettier
config.baseConfig.rules['prettier/prettier'] = ['error', mergeWithPrettierConfig(options, prettierConfig)];
Expand All @@ -464,7 +463,7 @@ const buildPrettierConfig = (options, prettierConfig) => config => {
// See https://github.com/prettier/eslint-config-prettier for the list of plugins overrrides
for (const [plugin, prettierConfig] of Object.entries(PRETTIER_CONFIG_OVERRIDE)) {
if (options.cwd && resolveFrom.silent(plugin, options.cwd)) {
config.baseConfig.extends = config.baseConfig.extends.concat(prettierConfig);
config.baseConfig.extends.push(prettierConfig);
}
}
}
Expand Down Expand Up @@ -505,7 +504,7 @@ const mergeWithPrettierConfig = (options, prettierOptions) => {

const buildTSConfig = options => config => {
if (options.ts) {
config.baseConfig.extends = config.baseConfig.extends.concat('xo-typescript');
config.baseConfig.extends.push('xo-typescript');
config.baseConfig.parser = require.resolve('@typescript-eslint/parser');
config.baseConfig.parserOptions = {
...config.baseConfig.parserOptions,
Expand All @@ -532,7 +531,7 @@ const applyOverrides = (file, options) => {

const {applicable, hash} = findApplicableOverrides(path.relative(options.cwd, file), overrides);

options = mergeWith(...[getEmptyXOConfig(), options].concat(applicable.map(override => normalizeOptions(override)), mergeFn));
options = mergeWith(getEmptyXOConfig(), options, ...applicable.map(override => normalizeOptions(override)), mergeFn);
delete options.files;
return {options, hash};
}
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,15 @@
"webpack": "^5.41.1"
},
"eslintConfig": {
"extends": "eslint-config-xo"
"extends": [
"eslint-config-xo",
"./config/plugins.cjs",
"./config/overrides.cjs"
]
},
"eslintIgnore": [
"test/fixtures"
"test/fixtures",
"coverage"
],
"ava": {
"timeout": "1m"
Expand Down
4 changes: 2 additions & 2 deletions test/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';
import test from 'ava';
import execa from 'execa';
import slash from 'slash';
Expand Down
6 changes: 5 additions & 1 deletion test/lint-files.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path';
import path from 'node:path';
import test from 'ava';
import createEsmUtils from 'esm-utils';
import xo from '../index.js';
Expand Down Expand Up @@ -200,6 +200,7 @@ test('typescript files', async t => {

test('typescript 2 space option', async t => {
const {errorCount, results} = await xo.lintFiles('two-spaces.tsx', {cwd: 'fixtures/typescript', space: 2});
// eslint-disable-next-line ava/assertion-arguments
t.is(errorCount, 0, JSON.stringify(results[0].messages));
});

Expand All @@ -222,6 +223,7 @@ test('webpack import resolver is used if webpack.config.js is found', async t =>
},
});

// eslint-disable-next-line ava/assertion-arguments
t.is(results[0].errorCount, 1, JSON.stringify(results[0].messages));

const errorMessage = results[0].messages[0].message;
Expand All @@ -247,6 +249,7 @@ test('webpack import resolver config can be passed through webpack option', asyn
},
});

// eslint-disable-next-line ava/assertion-arguments
t.is(results[0].errorCount, 1, JSON.stringify(results[0].messages));
});

Expand All @@ -262,6 +265,7 @@ test('webpack import resolver is used if {webpack: true}', async t => {
},
});

// eslint-disable-next-line ava/assertion-arguments
t.is(results[0].errorCount, 0, JSON.stringify(results[0]));
});

Expand Down
4 changes: 2 additions & 2 deletions test/lint-text.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {promises as fs} from 'fs';
import path from 'path';
import {promises as fs} from 'node:fs';
import path from 'node:path';
import test from 'ava';
import createEsmUtils from 'esm-utils';
import xo from '../index.js';
Expand Down
5 changes: 3 additions & 2 deletions test/open-report.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
/* eslint-disable ava/no-skip-test */
import path from 'node:path';
import test from 'ava';
import proxyquire from 'proxyquire';
import createEsmUtils from 'esm-utils';
Expand All @@ -13,7 +14,7 @@ test.skip('opens nothing when there are no errors nor warnings', async t => {

const openReport = proxyquire('../lib/open-report', {
'open-editor': files => {
if (files.length !== 0) {
if (files.length > 0) {
t.fail();
}
},
Expand Down
4 changes: 2 additions & 2 deletions test/options-manager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path';
import path from 'node:path';
import test from 'ava';
import {omit} from 'lodash-es';
import fsExtra from 'fs-extra';
Expand Down Expand Up @@ -429,7 +429,7 @@ test('buildConfig: extends', t => {
test('buildConfig: typescript', t => {
const config = manager.buildConfig({ts: true, tsConfigPath: './tsconfig.json'});

t.deepEqual(config.baseConfig.extends[config.baseConfig.extends.length - 1], 'xo-typescript');
t.is(config.baseConfig.extends[config.baseConfig.extends.length - 1], 'xo-typescript');
t.is(config.baseConfig.parser, require.resolve('@typescript-eslint/parser'));
t.deepEqual(config.baseConfig.parserOptions, {
warnOnUnsupportedTypeScriptVersion: false,
Expand Down
2 changes: 1 addition & 1 deletion test/print-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path';
import path from 'node:path';
import test from 'ava';
import execa from 'execa';
import tempWrite from 'temp-write';
Expand Down

0 comments on commit 0733cc5

Please sign in to comment.