Skip to content

Commit

Permalink
Use nullish assignment for all rules to respect user's config
Browse files Browse the repository at this point in the history
  • Loading branch information
melusc committed Jul 22, 2024
1 parent 8bdc3b3 commit 752e01a
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions lib/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ const buildXOConfig = options => config => {
for (const [rule, ruleConfig] of Object.entries(ENGINE_RULES)) {
for (const minVersion of Object.keys(ruleConfig).sort(semver.rcompare)) {
if (!options.nodeVersion || semver.intersects(options.nodeVersion, `<${minVersion}`)) {
config.baseConfig.rules[rule] = ruleConfig[minVersion];
config.baseConfig.rules[rule] ??= ruleConfig[minVersion];
}
}
}
Expand All @@ -361,45 +361,45 @@ const buildXOConfig = options => config => {

if (options.space && !options.prettier) {
if (options.ts) {
config.baseConfig.rules['@typescript-eslint/indent'] = ['error', spaces, {SwitchCase: 1}];
config.baseConfig.rules['@typescript-eslint/indent'] ??= ['error', spaces, {SwitchCase: 1}];
} else {
config.baseConfig.rules.indent = ['error', spaces, {SwitchCase: 1}];
config.baseConfig.rules.indent ??= ['error', spaces, {SwitchCase: 1}];
}

// Only apply if the user has the React plugin
if (options.cwd && resolveFrom.silent('eslint-plugin-react', options.cwd)) {
config.baseConfig.plugins.push('react');
config.baseConfig.rules['react/jsx-indent-props'] = ['error', spaces];
config.baseConfig.rules['react/jsx-indent'] = ['error', spaces];
config.baseConfig.rules['react/jsx-indent-props'] ??= ['error', spaces];
config.baseConfig.rules['react/jsx-indent'] ??= ['error', spaces];
}
}

if (options.semicolon === false && !options.prettier) {
if (options.ts) {
config.baseConfig.rules['@typescript-eslint/semi'] = ['error', 'never'];
config.baseConfig.rules['@typescript-eslint/semi'] ??= ['error', 'never'];
} else {
config.baseConfig.rules.semi = ['error', 'never'];
config.baseConfig.rules.semi ??= ['error', 'never'];
}

config.baseConfig.rules['semi-spacing'] = ['error', {
config.baseConfig.rules['semi-spacing'] ??= ['error', {
before: false,
after: true,
}];
}

if (options.ts) {
config.baseConfig.rules['unicorn/import-style'] = 'off';
config.baseConfig.rules['node/file-extension-in-import'] = 'off';
config.baseConfig.rules['unicorn/import-style'] ??= 'off';
config.baseConfig.rules['node/file-extension-in-import'] ??= 'off';

// Disabled because of https://github.com/benmosher/eslint-plugin-import/issues/1590
config.baseConfig.rules['import/export'] = 'off';
config.baseConfig.rules['import/export'] ??= 'off';

// Does not work when the TS definition exports a default const.
config.baseConfig.rules['import/default'] = 'off';
config.baseConfig.rules['import/default'] ??= 'off';

// Disabled as it doesn't work with TypeScript.
// This issue and some others: https://github.com/benmosher/eslint-plugin-import/issues/1341
config.baseConfig.rules['import/named'] = 'off';
config.baseConfig.rules['import/named'] ??= 'off';
}

config.baseConfig.settings['import/resolver'] = gatherImportResolvers(options);
Expand Down Expand Up @@ -444,7 +444,7 @@ const buildPrettierConfig = (options, prettierConfig) => config => {
config.baseConfig.extends.push('plugin:prettier/recommended');

// 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)];
config.baseConfig.rules['prettier/prettier'] ??= ['error', mergeWithPrettierConfig(options, prettierConfig)];
}

return config;
Expand Down

0 comments on commit 752e01a

Please sign in to comment.