Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TS extends after base XO extends and before user extends #453

Merged
merged 1 commit into from Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 9 additions & 11 deletions lib/options-manager.js
Expand Up @@ -259,9 +259,9 @@ Transform an XO options into ESLint compatible options:
const buildConfig = (options, prettierOptions) =>
flow(
buildXOConfig(options),
buildTSConfig(options),
buildExtendsConfig(options),
buildPrettierConfig(options, prettierOptions),
buildTSConfig(options)
buildPrettierConfig(options, prettierOptions)
)(mergeWith(getEmptyOptions(), DEFAULT_CONFIG, normalizeOptions(options), mergeFn));

const buildXOConfig = options => config => {
Expand All @@ -282,10 +282,10 @@ const buildXOConfig = options => config => {
}

if (options.space && !options.prettier) {
config.rules.indent = ['error', spaces, {SwitchCase: 1}];

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

// Only apply if the user has the React plugin
Expand Down Expand Up @@ -378,6 +378,10 @@ const buildPrettierConfig = (options, prettierConfig) => config => {
config.baseConfig.extends = config.baseConfig.extends.concat(prettierConfig);
}
}

if (options.ts) {
config.baseConfig.extends = config.baseConfig.extends.concat('prettier/@typescript-eslint');
}
}

return config;
Expand Down Expand Up @@ -416,6 +420,7 @@ const mergeWithPrettierConfig = (options, prettierOptions) => {

const buildTSConfig = options => config => {
if (options.ts) {
config.baseConfig.extends = config.baseConfig.extends.concat('xo-typescript');
config.baseConfig.parser = require.resolve('@typescript-eslint/parser');
config.baseConfig.parserOptions = {
warnOnUnsupportedTypeScriptVersion: false,
Expand All @@ -427,13 +432,6 @@ const buildTSConfig = options => config => {
[new RegExp(`/node_modules/(?!.*\\.cache/${CACHE_DIR_NAME})`)]
};

if (options.prettier) {
config.baseConfig.extends = ['prettier/@typescript-eslint', ...config.baseConfig.extends];
}

config.baseConfig.extends = ['xo-typescript', ...config.baseConfig.extends];

delete config.parser;
delete config.tsConfigPath;
delete config.ts;
}
Expand Down
16 changes: 6 additions & 10 deletions test/options-manager.js
Expand Up @@ -67,12 +67,6 @@ test('buildConfig: space: 4', t => {
t.deepEqual(config.rules.indent, ['error', 4, {SwitchCase: 1}]);
});

test('buildConfig: space: 4 (ts file)', t => {
const config = manager.buildConfig({space: 4, ts: true});
t.deepEqual(config.rules.indent, ['error', 4, {SwitchCase: 1}]);
t.deepEqual(config.rules['@typescript-eslint/indent'], ['error', 4, {SwitchCase: 1}]);
});

test('buildConfig: semicolon', t => {
const config = manager.buildConfig({semicolon: false, nodeVersion: '12'});
t.deepEqual(config.rules.semi, ['error', 'never']);
Expand Down Expand Up @@ -121,9 +115,11 @@ test('buildConfig: prettier: true, typescript file', t => {
trailingComma: 'none'
}]);

// Config prettier/@typescript-eslint must always be after xo-typescript
t.deepEqual(config.baseConfig.extends[0], 'xo-typescript');
t.deepEqual(config.baseConfig.extends[1], 'prettier/@typescript-eslint');
// eslint-prettier-config must always be last
t.deepEqual(config.baseConfig.extends[config.baseConfig.extends.length - 1], 'prettier/@typescript-eslint');
t.deepEqual(config.baseConfig.extends[config.baseConfig.extends.length - 2], 'prettier/unicorn');
t.deepEqual(config.baseConfig.extends[config.baseConfig.extends.length - 3], 'prettier');
t.deepEqual(config.baseConfig.extends[config.baseConfig.extends.length - 4], 'xo-typescript');

// Indent rule is not enabled
t.is(config.rules.indent, undefined);
Expand Down Expand Up @@ -440,7 +436,7 @@ test('buildConfig: extends', t => {
test('buildConfig: typescript', t => {
const config = manager.buildConfig({ts: true, tsConfigPath: './tsconfig.json'});

t.deepEqual(config.baseConfig.extends[0], 'xo-typescript');
t.deepEqual(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