Skip to content

Commit

Permalink
Fix a bug with relative extends (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdbond authored Oct 4, 2022
1 parent f9c7db9 commit e69a192
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ const buildExtendsConfig = options => config => {
if (options.extends && options.extends.length > 0) {
const configs = options.extends.map(name => {
// Don't do anything if it's a filepath
if (existsSync(name)) {
if (existsSync(path.resolve(options.cwd || process.cwd(), name))) {
return name;
}

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/config-files/extends-relative/.xo-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../extends.js"
}
5 changes: 5 additions & 0 deletions test/fixtures/config-files/extends-relative/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const object = {
a: 1
};

console.log(object.a);
17 changes: 17 additions & 0 deletions test/lint-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,20 @@ test(configType, {type: '.xo-config.js', dir: 'xo-config_js'});
test(configType, {type: '.xo-config.cjs', dir: 'xo-config_cjs'});
test(configType, {type: '.xo-config.json', dir: 'xo-config_json'});
test(configType, {type: '.xo-config', dir: 'xo-config'});

test('load config file with relative extends from different cwd', async t => {
const directory = 'extends-relative';

const {results, rulesMeta} = await xo.lintFiles('**/*', {
cwd: path.resolve('fixtures', 'config-files', directory),
});

t.true(
hasRule(
results,
path.resolve('fixtures', 'config-files', directory, 'file.js'),
'comma-dangle',
rulesMeta,
),
);
});
2 changes: 1 addition & 1 deletion test/lint-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ test('extends `react` support with `prettier` option', async t => {

test('regression test for #71', async t => {
const {results} = await xo.lintText('const foo = { key: \'value\' };\nconsole.log(foo);\n', {
extends: path.join(__dirname, 'fixtures/extends.js'),
extends: './fixtures/extends.js',
});
t.is(results[0].errorCount, 0);
});
Expand Down
4 changes: 3 additions & 1 deletion test/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,14 @@ test('buildConfig: extends', t => {
extends: [
'plugin:foo/bar',
'eslint-config-prettier',
'./fixtures/config-files/xo_config_js/xo.config.js',
],
});

t.deepEqual(config.baseConfig.extends.slice(-2), [
t.deepEqual(config.baseConfig.extends.slice(-3), [
'plugin:foo/bar',
path.resolve('../node_modules/eslint-config-prettier/index.js'),
'./fixtures/config-files/xo_config_js/xo.config.js',
]);
});

Expand Down

0 comments on commit e69a192

Please sign in to comment.