Skip to content

Commit

Permalink
feat(eslint): Add new option to specify file extensions and deprecate…
Browse files Browse the repository at this point in the history
… glob option
  • Loading branch information
danez committed Jun 11, 2020
1 parent e351155 commit 5de25e7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
24 changes: 20 additions & 4 deletions packages/spire-plugin-eslint/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const { createFixture } = require('spire-test-utils');
const { stat, readFile } = require('fs-extra');
const { join } = require('path');

const configWithEslintPlugin = (config) =>
const configWithEslintPlugin = (options = {}) =>
JSON.stringify({
name: 'spire-plugin-eslint-test',
spire: {
plugins: [[require.resolve('spire-plugin-eslint'), { config }]],
plugins: [[require.resolve('spire-plugin-eslint'), options]],
},
});

Expand All @@ -31,7 +31,7 @@ describe('spire-plugin-eslint', () => {
'--debug',
]);
expect(stdout).toMatch(/Using linters:/);
expect(stdout).toMatch(/\.js/);
expect(stdout).toMatch(/\*\.\(js\|jsx\|mjs\|ts\|tsx\)/);
await fixture.clean();
});

Expand Down Expand Up @@ -60,6 +60,20 @@ describe('spire-plugin-eslint', () => {
await fixture.clean();
});

test('prints deprecation warning for glob option', async () => {
const fixture = await createFixture({
'package.json': configWithEslintPlugin({ glob: 'abc' }),
});
await fixture.run('spire', ['hook', 'precommit']);

await expect(
fixture.run('spire', ['hook', 'precommit'])
).resolves.toMatchObject({
stdout: /The glob option is deprecated\. Use the option `fileExtensions` instead\./,
});
await fixture.clean();
});

test('creates custom eslint config for editors', async () => {
const fixture = await createFixture({
'node_modules/eslint-config-cool-test/package.json': JSON.stringify({
Expand All @@ -68,7 +82,9 @@ describe('spire-plugin-eslint', () => {
main: 'index.js',
}),
'node_modules/eslint-config-cool-test/index.js': 'module.exports = {};',
'package.json': configWithEslintPlugin('eslint-config-cool-test'),
'package.json': configWithEslintPlugin({
config: 'eslint-config-cool-test',
}),
});
await fixture.run('spire', ['hook', 'postinstall']);
const eslintConfig = join(fixture.cwd, '.eslintrc.js');
Expand Down
21 changes: 18 additions & 3 deletions packages/spire-plugin-eslint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ function eslint(
allowCustomConfig = true,
eslintIgnore: defaultEslintIgnore = '.gitignore',
allowCustomIgnore = true,
glob = '*.js',
fileExtensions = ['.js', '.jsx', '.mjs', '.ts', '.tsx'],
/* @deprecated */
glob,
}
) {
async function hasCustomEslintConfig() {
Expand Down Expand Up @@ -71,8 +73,9 @@ function eslint(
: (await hasFile(defaultEslintIgnore))
? ['--ignore-path', defaultEslintIgnore]
: [];
const eslintExtensions = ['--ext', fileExtensions.join(',')];
setState({
eslintArgs: [...eslintConfig, ...eslintIgnore],
eslintArgs: [...eslintConfig, ...eslintIgnore, ...eslintExtensions],
});
// Inform user about disallowed overrides
if (hasCustomConfig && !allowCustomConfig) {
Expand All @@ -87,10 +90,22 @@ function eslint(
}
},
async precommit() {
if (glob) {
console.warn(
'spire-plugin-eslint: The glob option is deprecated. Use the option `fileExtensions` instead.'
);
}
setState((prev) => ({
linters: [
...prev.linters,
{ [glob]: ['eslint', ...prev.eslintArgs, '--fix'] },
{
[glob ||
`*.(${fileExtensions.map((ext) => ext.substr(1)).join('|')})`]: [
'eslint',
...prev.eslintArgs,
'--fix',
],
},
],
}));
},
Expand Down

0 comments on commit 5de25e7

Please sign in to comment.