Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 24, 2020
1 parent b0dfcbd commit 482e209
Show file tree
Hide file tree
Showing 19 changed files with 142 additions and 125 deletions.
6 changes: 3 additions & 3 deletions config/default.tsconfig.json
@@ -1,5 +1,5 @@
{
"include": [
"**/*d.ts"
]
"include": [
"**/*d.ts"
]
}
70 changes: 42 additions & 28 deletions lib/constants.js
Expand Up @@ -11,37 +11,51 @@ const DEFAULT_IGNORES = [
];

/**
* List of options that values will be concatenanted during option merge.
* Only applies to options defined as an Array.
*/
const MERGE_OPTIONS_CONCAT = ['extends', 'envs', 'globals', 'plugins'];
List of options that values will be concatenanted during option merge.
Only applies to options defined as an Array.
*/
const MERGE_OPTIONS_CONCAT = [
'extends',
'envs',
'globals',
'plugins'
];

const TYPESCRIPT_EXTENSION = ['ts', 'tsx'];
const DEFAULT_EXTENSION = ['js', 'jsx', ...TYPESCRIPT_EXTENSION];
const TYPESCRIPT_EXTENSION = [
'ts',
'tsx'
];

const DEFAULT_EXTENSION = [
'js',
'jsx',
...TYPESCRIPT_EXTENSION
];

/**
* Define the rules config that are overwritten only for specific version of Node.js based on `engines.node` in package.json or the `node-version` option.
*
* The keys are rule names and the values are an Object with a valid semver (`4.0.0` is valid `4` is not) as keys and the rule configuration as values.
*
* Each entry define the rule config and the maximum Node.js version for which to set it.
* The entry with the lowest version that is compliant with the `engines.node`/`node-version` range will be used.
*
* @type {Object}
*
* @example
* ```js
* {
* 'plugin/rule': {
* '6.0.0': ['error', {prop: 'node-6-conf'}],
* '8.0.0': ['error', {prop: 'node-8-conf'}]
* }
* }
*```
* With `engines.node` set to `>=4` the rule `plugin/rule` will not be used.
* With `engines.node` set to `>=6` the rule `plugin/rule` will be used with the config `{prop: 'node-6-conf'}`.
* With `engines.node` set to `>=8` the rule `plugin/rule` will be used with the config `{prop: 'node-8-conf'}`.
*/
Define the rules config that are overwritten only for specific version of Node.js based on `engines.node` in package.json or the `nodeVersion` option.
The keys are rule names and the values are an Object with a valid semver (`4.0.0` is valid `4` is not) as keys and the rule configuration as values.
Each entry define the rule config and the maximum Node.js version for which to set it.
The entry with the lowest version that is compliant with the `engines.node`/`nodeVersion` range will be used.
@type {Object}
@example
```
{
'plugin/rule': {
'6.0.0': ['error', {prop: 'node-6-conf'}],
'8.0.0': ['error', {prop: 'node-8-conf'}]
}
}
```
With `engines.node` set to `>=4` the rule `plugin/rule` will not be used.
With `engines.node` set to `>=6` the rule `plugin/rule` will be used with the config `{prop: 'node-6-conf'}`.
With `engines.node` set to `>=8` the rule `plugin/rule` will be used with the config `{prop: 'node-8-conf'}`.
*/
const ENGINE_RULES = {
'unicorn/prefer-spread': {
'5.0.0': 'off'
Expand Down
43 changes: 23 additions & 20 deletions lib/options-manager.js
Expand Up @@ -43,8 +43,8 @@ const DEFAULT_CONFIG = {
};

/**
* Define the shape of deep properties for mergeWith
*/
Define the shape of deep properties for `mergeWith`.
*/
const getEmptyOptions = () => ({
rules: {},
settings: {},
Expand All @@ -67,9 +67,9 @@ const mergeFn = (previousValue, value, key) => {
const isTypescript = file => TYPESCRIPT_EXTENSION.includes(path.extname(file).slice(1));

/**
* Find config for `lintText`.
* The config files are searched starting from `options.filename` if defined or `options.cwd` otherwise.
*/
Find config for `lintText`.
The config files are searched starting from `options.filename` if defined or `options.cwd` otherwise.
*/
const mergeWithFileConfig = options => {
options.cwd = path.resolve(options.cwd || process.cwd());
const configExplorer = cosmiconfigSync(MODULE_NAME, {searchPlaces: CONFIG_FILES, loaders: {noExt: defaultLoaders['.json']}, stopDir: options.cwd});
Expand Down Expand Up @@ -105,9 +105,9 @@ const mergeWithFileConfig = options => {
};

/**
* Find config for each files found by `lintFiles`.
* The config files are searched starting from each files.
*/
Find config for each files found by `lintFiles`.
The config files are searched starting from each files.
*/
const mergeWithFileConfigs = async (files, options) => {
options.cwd = path.resolve(options.cwd || process.cwd());

Expand Down Expand Up @@ -219,8 +219,8 @@ const normalizeSpaces = options => typeof options.space === 'number' ? options.s
const isFileIgnored = (file, options) => micromatch.isMatch(path.relative(options.cwd, file), options.ignores);

/**
* Merge option passed via CLI/API via options founf in config files.
*/
Merge option passed via CLI/API via options founf in config files.
*/
const mergeOptions = (xoOptions, enginesOptions, options) => {
const mergedOptions = normalizeOptions({
...xoOptions,
Expand All @@ -235,11 +235,11 @@ const mergeOptions = (xoOptions, enginesOptions, options) => {
};

/**
* Transform an XO options into ESLint compatible options:
* - apply rules based on XO options (e.g `spaces` => `indent` rules or `semicolon` => `semi` rule)
* - resolve the extended configurations
* - apply rules based on Prettier config if `prettier` option is `true`
*/
Transform an XO options into ESLint compatible options:
- Apply rules based on XO options (e.g `spaces` => `indent` rules or `semicolon` => `semi` rule).
- Resolve the extended configurations.
- Apply rules based on Prettier config if `prettier` option is `true`.
*/
const buildConfig = (options, prettierOptions) =>
flow(
buildXOConfig(options),
Expand Down Expand Up @@ -348,11 +348,14 @@ const buildPrettierConfig = (options, prettierConfig) => config => {
if (options.prettier) {
// The prettier plugin uses Prettier to format the code with `--fix`
config.plugins = config.plugins.concat('prettier');

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

// The `prettier/prettier` rule reports errors if the code is not formatted in accordance to Prettier
config.rules['prettier/prettier'] = ['error', mergeWithPrettierConfig(options, prettierConfig)];

// If the user has the React, Flowtype, or Standard plugin, add the corresponding Prettier rule overrides
// See https://github.com/prettier/eslint-config-prettier for the list of plugins overrrides
for (const [plugin, prettierConfig] of Object.entries(PRETTIER_CONFIG_OVERRIDE)) {
Expand Down Expand Up @@ -433,11 +436,11 @@ const applyOverrides = (file, options) => {
};

/**
* Builds a list of overrides for a particular path, and a hash value.
* The hash value is a binary representation of which elements in the `overrides` array apply to the path.
*
* If `overrides.length === 4`, and only the first and third elements apply, then our hash is: 1010 (in binary)
*/
Builds a list of overrides for a particular path, and a hash value.
The hash value is a binary representation of which elements in the `overrides` array apply to the path.
If `overrides.length === 4`, and only the first and third elements apply, then our hash is: 1010 (in binary)
*/
const findApplicableOverrides = (path, overrides) => {
let hash = 0;
const applicable = [];
Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -51,8 +51,8 @@
"typescript"
],
"dependencies": {
"@typescript-eslint/eslint-plugin": "^2.19.2",
"@typescript-eslint/parser": "^2.19.2",
"@typescript-eslint/eslint-plugin": "^2.21.0",
"@typescript-eslint/parser": "^2.21.0",
"arrify": "^2.0.1",
"cosmiconfig": "^6.0.0",
"debug": "^4.1.0",
Expand Down Expand Up @@ -93,7 +93,7 @@
"devDependencies": {
"ava": "^1.1.0",
"coveralls": "^3.0.6",
"eslint-config-xo-react": "^0.22.0",
"eslint-config-xo-react": "^0.23.0",
"eslint-plugin-react": "^7.14.3",
"eslint-plugin-react-hooks": "^2.0.1",
"execa": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/config-files/xo_config_js/xo.config.js
@@ -1,3 +1,3 @@
module.exports = {
esnext: true
esnext: true
}
14 changes: 7 additions & 7 deletions test/fixtures/ignores/package.json
@@ -1,9 +1,9 @@
{
"name": "application-name",
"version": "0.0.1",
"xo": {
"ignore": [
"!{test/,}fixture{s,}/**"
]
}
"name": "application-name",
"version": "0.0.1",
"xo": {
"ignore": [
"!{test/,}fixture{s,}/**"
]
}
}
@@ -1,10 +1,10 @@
{
"xo": {
"overrides": [
{
"files": "semicolon.js",
"prettier": true
}
]
}
"xo": {
"overrides": [
{
"files": "semicolon.js",
"prettier": true
}
]
}
}
16 changes: 8 additions & 8 deletions test/fixtures/nested-configs/child-override/package.json
@@ -1,10 +1,10 @@
{
"xo": {
"overrides": [
{
"files": "two-spaces.js",
"space": 4
}
]
}
"xo": {
"overrides": [
{
"files": "two-spaces.js",
"space": 4
}
]
}
}
6 changes: 3 additions & 3 deletions test/fixtures/nested-configs/child/package.json
@@ -1,5 +1,5 @@
{
"xo": {
"semicolon": false
}
"xo": {
"semicolon": false
}
}
6 changes: 3 additions & 3 deletions test/fixtures/nested-configs/package.json
@@ -1,5 +1,5 @@
{
"xo": {
"semicolon": true
}
"xo": {
"semicolon": true
}
}
2 changes: 1 addition & 1 deletion test/fixtures/nested/child-empty/package.json
@@ -1,3 +1,3 @@
{
"xo": {}
"xo": {}
}
6 changes: 3 additions & 3 deletions test/fixtures/nested/child/package.json
@@ -1,5 +1,5 @@
{
"xo": {
"esnext": false
}
"xo": {
"esnext": false
}
}
6 changes: 3 additions & 3 deletions test/fixtures/nested/package.json
@@ -1,5 +1,5 @@
{
"xo": {
"esnext": true
}
"xo": {
"esnext": true
}
}
38 changes: 19 additions & 19 deletions test/fixtures/overrides/package.json
@@ -1,21 +1,21 @@
{
"xo": {
"space": 2,
"semicolon": false,
"overrides": [
{
"files": "test/*.js",
"esnext": true,
"space": 3
},
{
"files": "test/foo.js",
"space": 4
}
],
"rules": {
"import/no-extraneous-dependencies": 0,
"ava/no-ignored-test-files": 0
}
}
"xo": {
"space": 2,
"semicolon": false,
"overrides": [
{
"files": "test/*.js",
"esnext": true,
"space": 3
},
{
"files": "test/foo.js",
"space": 4
}
],
"rules": {
"import/no-extraneous-dependencies": 0,
"ava/no-ignored-test-files": 0
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions test/fixtures/project/package.json
@@ -1,5 +1,5 @@
{
"xo": {
"extends": ["custom"]
}
"xo": {
"extends": ["custom"]
}
}
6 changes: 3 additions & 3 deletions test/fixtures/typescript/child/package.json
@@ -1,5 +1,5 @@
{
"xo": {
"semicolon": false
}
"xo": {
"semicolon": false
}
}
8 changes: 4 additions & 4 deletions test/fixtures/typescript/child/tsconfig.json
@@ -1,6 +1,6 @@
{
"include": [
"**/*.ts",
"**/*.tsx"
]
"include": [
"**/*.ts",
"**/*.tsx"
]
}
6 changes: 3 additions & 3 deletions test/fixtures/typescript/package.json
@@ -1,5 +1,5 @@
{
"xo": {
"space": 4
}
"xo": {
"space": 4
}
}

0 comments on commit 482e209

Please sign in to comment.