Skip to content

Commit

Permalink
chore: ensure arrays instead of CSV strings are passed around
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Dec 17, 2022
1 parent 1c41338 commit d32d8cd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/generator.ts
Expand Up @@ -88,6 +88,13 @@ function stringOrArrayWithFallback<T extends string | readonly string[]>(
stringOrArray: undefined | T,
fallback: T
): T {
// TODO: check CSV string in array too
// Just normalize everything into arrays?
if (typeof stringOrArray === 'string' && stringOrArray.includes(',')) {
throw new Error(
`Provide property as array, not a CSV string: ${stringOrArray}`
);
}
return stringOrArray && stringOrArray.length > 0 ? stringOrArray : fallback;
}

Expand Down
57 changes: 57 additions & 0 deletions test/lib/generate/file-paths-test.ts
Expand Up @@ -262,6 +262,63 @@ describe('generate (file paths)', function () {
});
});

describe('multiple rules lists but incorrectly using CSV string for option', function () {
beforeEach(function () {
mockFs({
'package.json': JSON.stringify({
name: 'eslint-plugin-test',
exports: 'index.js',
type: 'module',
}),

'index.js': `
export default {
rules: {
'no-foo': { meta: { }, create(context) {} },
},
};`,

'README.md':
'<!-- begin auto-generated rules list --><!-- end auto-generated rules list -->',
'rules/list.md':
'<!-- begin auto-generated rules list --><!-- end auto-generated rules list -->',
'docs/rules/no-foo.md': '',

// Needed for some of the test infrastructure to work.
node_modules: mockFs.load(PATH_NODE_MODULES),
});
});

afterEach(function () {
mockFs.restore();
jest.resetModules();
});

it('throws an error', async function () {
await expect(
generate('.', {
pathRuleList: `README.md,${join('rules', 'list.md')}`,
})
).rejects.toThrow(
`Provide property as array, not a CSV string: README.md,${join(
'rules',
'list.md'
)}`
);

await expect(
generate('.', {
pathRuleList: [`README.md,${join('rules', 'list.md')}`],
})
).rejects.toThrow(
`Provide property as array, not a CSV string: README.md,${join(
'rules',
'list.md'
)}`
);
});
});

describe('empty array of rule lists (happens when CLI option is not passed)', function () {
beforeEach(function () {
mockFs({
Expand Down

0 comments on commit d32d8cd

Please sign in to comment.