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

Accept more types of eslint presets #96

Merged
merged 6 commits into from
Oct 8, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/mrm-task-eslint/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ See [Mrm docs](../../docs/Getting_started.md) for more details.

### `eslintPreset` (default: `eslint:recommended`)

ESLint preset name (not npm package name, e.g. `airbnb`).
ESLint preset name (e.g. `airbnb` or `eslint-config-airbnb`).

### `eslintPeerDependencies` (optional)

Expand Down
62 changes: 61 additions & 1 deletion packages/mrm-task-eslint/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,68 @@ Object {
}
`;

exports[`should use a custom preset 1`] = `
exports[`should use custom preset \`@scoped/custom-config-name/variant\` 1`] = `
"{
\\"extends\\": \\"@scoped/custom-config-name/variant\\"
}"
`;

exports[`should use custom preset \`@scoped/custom-config-name\` 1`] = `
"{
\\"extends\\": \\"@scoped/custom-config-name\\"
}"
`;

exports[`should use custom preset \`@scoped/eslint-config/variant\` 1`] = `
"{
\\"extends\\": \\"@scoped/eslint-config/variant\\"
}"
`;

exports[`should use custom preset \`@scoped/eslint-config\` 1`] = `
"{
\\"extends\\": \\"@scoped/eslint-config\\"
}"
`;

exports[`should use custom preset \`@scoped/eslint-config-alt/variant\` 1`] = `
"{
\\"extends\\": \\"@scoped/eslint-config-alt/variant\\"
}"
`;

exports[`should use custom preset \`@scoped/eslint-config-alt\` 1`] = `
"{
\\"extends\\": \\"@scoped/eslint-config-alt\\"
}"
`;

exports[`should use custom preset \`@scoped\` 1`] = `
"{
\\"extends\\": \\"@scoped\\"
}"
`;

exports[`should use custom preset \`airbnb/whitespace\` 1`] = `
"{
\\"extends\\": \\"airbnb/whitespace\\"
}"
`;

exports[`should use custom preset \`airbnb\` 1`] = `
"{
\\"extends\\": \\"airbnb\\"
}"
`;

exports[`should use custom preset \`eslint-config-airbnb/whitespace\` 1`] = `
"{
\\"extends\\": \\"eslint-config-airbnb/whitespace\\"
}"
`;

exports[`should use custom preset \`eslint-config-airbnb\` 1`] = `
"{
\\"extends\\": \\"eslint-config-airbnb\\"
}"
`;
31 changes: 30 additions & 1 deletion packages/mrm-task-eslint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@ const {
getExtsFromCommand,
} = require('mrm-core');

const getConfigName = (configName, scope, prefix) => {
if (!scope && !configName.startsWith(prefix)) {
return `${prefix}-${configName}`;
} else if (scope && !configName) {
return prefix;
} else {
return configName;
}
};

const normalizePresetPackageName = presetName => {
const prefix = 'eslint-config';
const presetNameRegex = /^(?:(@[^/]+)\/?)?((?:eslint-config-)?[^/]*)(?:\/[^/]+)?$/;
const match = presetName.match(presetNameRegex);

if (!match) {
throw new Error(
`Invalid preset name is passed to the eslint task: ${presetName}`
);
}

const [, scope = '', configNameRaw] = match;
const configName = getConfigName(configNameRaw, scope, prefix);

const packageName = `${scope ? `${scope}/` : ''}${configName}`;

return packageName;
};

module.exports = function task({
eslintPreset,
eslintPeerDependencies,
Expand All @@ -25,7 +54,7 @@ module.exports = function task({

// Preset
if (eslintPreset !== 'eslint:recommended') {
packages.push(`eslint-config-${eslintPreset}`);
packages.push(normalizePresetPackageName(eslintPreset));
}

// Peer dependencies
Expand Down
25 changes: 22 additions & 3 deletions packages/mrm-task-eslint/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,34 @@ it('should add ESLint', async () => {
expect(install).toBeCalledWith(['eslint']);
});

it('should use a custom preset', async () => {
it.each([
['airbnb', 'eslint-config-airbnb'],
['airbnb/whitespace', 'eslint-config-airbnb'],
['eslint-config-airbnb', 'eslint-config-airbnb'],
['eslint-config-airbnb/whitespace', 'eslint-config-airbnb'],
['@scoped', '@scoped/eslint-config'],
['@scoped/eslint-config', '@scoped/eslint-config'],
['@scoped/eslint-config/variant', '@scoped/eslint-config'],
['@scoped/custom-config-name', '@scoped/custom-config-name'],
['@scoped/eslint-config-alt', '@scoped/eslint-config-alt'],
['@scoped/eslint-config-alt/variant', '@scoped/eslint-config-alt'],
['@scoped/custom-config-name/variant', '@scoped/custom-config-name'],
])('should use custom preset `%s`', async (presetName, packageName) => {
vol.fromJSON({
'/package.json': packageJson,
});

task(await getTaskOptions(task, false, { eslintPreset: 'airbnb' }));
task(await getTaskOptions(task, false, { eslintPreset: presetName }));

expect(vol.toJSON()[configFile]).toMatchSnapshot();
expect(install).toBeCalledWith(['eslint', 'eslint-config-airbnb']);
expect(install).toBeCalledWith(['eslint', packageName]);
});

it('should throw when given a file path for the preset name', async () => {
const options = await getTaskOptions(task, false, {
eslintPreset: './path/to/config',
});
expect(() => task(options)).toThrow();
});

it('should not add a custom preset if it’s already there', async () => {
Expand Down