Skip to content

Commit b538cf7

Browse files
authored
Fix: Behaviour of print-config option with relative file path (#816)
1 parent f59e4fd commit b538cf7

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

‎cli.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ if (typeof cliOptions.printConfig === 'string') {
232232
process.exit(1);
233233
}
234234

235-
const config = await new Xo(linterOptions, baseXoConfigOptions).calculateConfigForFile(cliOptions.printConfig);
235+
const absoluteFilePath = path.resolve(cliOptions.cwd, cliOptions.printConfig);
236+
237+
const config = await new Xo(linterOptions, baseXoConfigOptions).calculateConfigForFile(absoluteFilePath);
236238
console.log(JSON.stringify(config, undefined, '\t'));
237239
} else {
238240
const xo = new Xo(linterOptions, baseXoConfigOptions);

‎test/cli.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ test('xo --print-config ts', async t => {
113113
t.true('rules' in config);
114114
});
115115

116+
test('xo --print-config relative path', async t => {
117+
const fileName = 'test.ts';
118+
const filePath = path.join(t.context.cwd, fileName);
119+
await fs.writeFile(filePath, dedent`console.log('hello');\n`, 'utf8');
120+
const {stdout} = await $`node ./dist/cli --cwd ${t.context.cwd} --print-config=${fileName}`;
121+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
122+
const config = JSON.parse(stdout);
123+
t.true(typeof config === 'object');
124+
t.true('rules' in config);
125+
});
126+
127+
test('xo --print-config no path', async t => {
128+
const {stderr}: ExecaError = await t.throwsAsync($`node ./dist/cli --cwd ${t.context.cwd} --print-config`);
129+
t.is('The `--print-config` flag must be used with exactly one filename', stderr?.toString() ?? '');
130+
});
131+
116132
test('xo --ignore', async t => {
117133
const testFile = path.join(t.context.cwd, 'test.js');
118134
const ignoredFile = path.join(t.context.cwd, 'ignored.js');

0 commit comments

Comments
 (0)