Skip to content

Commit

Permalink
chore: better normalize pathRuleList option
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Dec 17, 2022
1 parent 1c41338 commit 9a14b9e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
26 changes: 21 additions & 5 deletions lib/generator.ts
Expand Up @@ -91,6 +91,25 @@ function stringOrArrayWithFallback<T extends string | readonly string[]>(
return stringOrArray && stringOrArray.length > 0 ? stringOrArray : fallback;
}

function stringOrArrayToArrayWithFallback(
stringOrArray: undefined | string | readonly string[],
fallback: readonly string[]
): readonly string[] {
const asArray =
stringOrArray instanceof Array // eslint-disable-line unicorn/no-instanceof-array -- using Array.isArray() loses type information about the array.
? stringOrArray
: stringOrArray
? [stringOrArray]
: [];
const csvStringItem = asArray.find((item) => item.includes(','));
if (csvStringItem) {
throw new Error(
`Provide property as array, not a CSV string: ${csvStringItem}`
);
}
return asArray && asArray.length > 0 ? asArray : fallback;
}

// eslint-disable-next-line complexity
export async function generate(path: string, options?: GenerateOptions) {
const plugin = await loadPlugin(path);
Expand All @@ -115,7 +134,7 @@ export async function generate(path: string, options?: GenerateOptions) {
options?.initRuleDocs ?? OPTION_DEFAULTS[OPTION_TYPE.INIT_RULE_DOCS];
const pathRuleDoc =
options?.pathRuleDoc ?? OPTION_DEFAULTS[OPTION_TYPE.PATH_RULE_DOC];
const pathRuleList = stringOrArrayWithFallback(
const pathRuleList = stringOrArrayToArrayWithFallback(
options?.pathRuleList,
OPTION_DEFAULTS[OPTION_TYPE.PATH_RULE_LIST]
);
Expand Down Expand Up @@ -269,10 +288,7 @@ export async function generate(path: string, options?: GenerateOptions) {
);
}

// eslint-disable-next-line unicorn/no-instanceof-array -- using Array.isArray() loses type information about the array.
for (const pathRuleListItem of pathRuleList instanceof Array
? pathRuleList
: [pathRuleList]) {
for (const pathRuleListItem of pathRuleList) {
// Find the exact filename.
const pathToFile = getPathWithExactFileNameCasing(
join(path, pathRuleListItem)
Expand Down
2 changes: 1 addition & 1 deletion lib/options.ts
Expand Up @@ -47,7 +47,7 @@ export const OPTION_DEFAULTS = {
[OPTION_TYPE.IGNORE_DEPRECATED_RULES]: false,
[OPTION_TYPE.INIT_RULE_DOCS]: false,
[OPTION_TYPE.PATH_RULE_DOC]: join('docs', 'rules', '{name}.md'),
[OPTION_TYPE.PATH_RULE_LIST]: 'README.md',
[OPTION_TYPE.PATH_RULE_LIST]: ['README.md'],
[OPTION_TYPE.POSTPROCESS]: (content: string) => content,
[OPTION_TYPE.RULE_DOC_NOTICES]: Object.entries(
NOTICE_TYPE_DEFAULT_PRESENCE_AND_ORDERING
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 9a14b9e

Please sign in to comment.