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

Proposal to replace #504 (ESLint/Vue) #574

Merged
merged 6 commits into from
Apr 17, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1106,13 +1106,27 @@ class Encore {
* options.extends = 'airbnb';
* options.emitWarning = false;
* });
*
* // configure Encore-specific options
* Encore.enableEslintLoader(() => {}, {
* // set optional Encore-specific options, for instance:
*
* // lint `.vue` files
* lintVue: true
* });
* ```
*
* Supported options:
* * {boolean} lintVue (default=false)
* Configure the loader to lint `.vue` files
* ```
*
* @param {string|object|function} eslintLoaderOptionsOrCallback
* @param {object} encoreOptions
* @returns {Encore}
*/
enableEslintLoader(eslintLoaderOptionsOrCallback = () => {}) {
webpackConfig.enableEslintLoader(eslintLoaderOptionsOrCallback);
enableEslintLoader(eslintLoaderOptionsOrCallback = () => {}, encoreOptions = {}) {
webpackConfig.enableEslintLoader(eslintLoaderOptionsOrCallback, encoreOptions);

return this;
}
Expand Down
27 changes: 16 additions & 11 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ class WebpackConfig {
this.vueOptions = {
useJsx: false,
};
this.eslintOptions = {
lintVue: false,
};

// Features/Loaders options callbacks
this.postCssLoaderOptionsCallback = () => {};
Expand Down Expand Up @@ -686,31 +689,33 @@ class WebpackConfig {
this.vueOptions = vueOptions;
}

enableEslintLoader(eslintLoaderOptionsOrCallback = () => {}) {
enableEslintLoader(eslintLoaderOptionsOrCallback = () => {}, eslintOptions = {}) {
this.useEslintLoader = true;

if (typeof eslintLoaderOptionsOrCallback === 'function') {
this.eslintLoaderOptionsCallback = eslintLoaderOptionsOrCallback;
return;
}

if (typeof eslintLoaderOptionsOrCallback === 'string') {
} else if (typeof eslintLoaderOptionsOrCallback === 'string') {
logger.deprecation('enableEslintLoader: Extending from a configuration is deprecated, please use a configuration file instead. See https://eslint.org/docs/user-guide/configuring for more information.');

this.eslintLoaderOptionsCallback = (options) => {
options.extends = eslintLoaderOptionsOrCallback;
};
return;
}

if (typeof eslintLoaderOptionsOrCallback === 'object') {
} else if (typeof eslintLoaderOptionsOrCallback === 'object') {
this.eslintLoaderOptionsCallback = (options) => {
Object.assign(options, eslintLoaderOptionsOrCallback);
};
return;
} else {
throw new Error('Argument 1 to enableEslintLoader() must be either a string, object or callback function.');
}

// Check allowed keys
for (const key of Object.keys(eslintOptions)) {
if (!(key in this.eslintOptions)) {
throw new Error(`"${key}" is not a valid key for enableEslintLoader(). Valid keys: ${Object.keys(this.eslintOptions).join(', ')}.`);
}
}

throw new Error('Argument 1 to enableEslintLoader() must be either a string, object or callback function.');
this.eslintOptions = eslintOptions;
}

enableBuildNotifications(enabled = true, notifierPluginOptionsCallback = () => {}) {
Expand Down
2 changes: 1 addition & 1 deletion lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ class ConfigGenerator {

if (this.webpackConfig.useEslintLoader) {
rules.push(applyRuleConfigurationCallback('eslint', {
test: /\.jsx?$/,
test: eslintLoaderUtil.getTest(this.webpackConfig),
loader: 'eslint-loader',
exclude: /node_modules/,
enforce: 'pre',
Expand Down
14 changes: 14 additions & 0 deletions lib/loaders/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,19 @@ Install ${chalk.yellow('babel-eslint')} to prevent potential parsing issues: ${p
};

return applyOptionsCallback(webpackConfig.eslintLoaderOptionsCallback, eslintLoaderOptions);
},

/**
* @param {WebpackConfig} webpackConfig
* @return {RegExp} to use for eslint-loader `test` rule
*/
getTest(webpackConfig) {
const extensions = ['jsx?'];

if (webpackConfig.eslintOptions.lintVue) {
extensions.push('vue');
}

return new RegExp(`\\.(${extensions.join('|')})$`);
}
};
12 changes: 12 additions & 0 deletions test/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -1277,4 +1277,16 @@ describe('WebpackConfig object', () => {
expect(() => config.enableIntegrityHashes(true, ['sha1', 'foo', 'sha256'])).to.throw('Invalid hash algorithm "foo"');
});
});

describe('enableEslintLoader', () => {
it('Should validate Encore-specific options', () => {
const config = createConfig();

expect(() => {
config.enableEslintLoader(() => {}, {
notExisting: false,
});
}).to.throw('"notExisting" is not a valid key for enableEslintLoader(). Valid keys: lintVue.');
});
});
});
16 changes: 16 additions & 0 deletions test/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,22 @@ describe('The config-generator function', () => {
expect(JSON.stringify(actualConfig.module.rules)).to.contain('eslint-loader');
expect(JSON.stringify(actualConfig.module.rules)).to.contain('extends-name');
});

it('enableEslintLoader(() => {}, {lintVue: true})', () => {
const config = createConfig();
config.addEntry('main', './main');
config.publicPath = '/';
config.outputPath = '/tmp';
config.enableEslintLoader(() => {}, {
lintVue: true,
});

const actualConfig = configGenerator(config);
expect(JSON.stringify(actualConfig.module.rules)).to.contain('eslint-loader');

const eslintRule = findRule(/\.(jsx?|vue)$/, actualConfig.module.rules);
expect(eslintRule.test.toString()).to.equal(/\.(jsx?|vue)$/.toString());
});
});

describe('addLoader() adds a custom loader', () => {
Expand Down
17 changes: 17 additions & 0 deletions test/loaders/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,21 @@ describe('loaders/eslint', () => {
const actualOptions = eslintLoader.getOptions(config);
expect(actualOptions).to.deep.equals({ foo: true });
});

it('getTest() base behavior', () => {
const config = createConfig();

const actualTest = eslintLoader.getTest(config);
expect(actualTest.toString()).to.equals(/\.(jsx?)$/.toString());
});

it('getTest() with Vue', () => {
const config = createConfig();
config.enableEslintLoader(() => {}, {
lintVue: true,
});

const actualTest = eslintLoader.getTest(config);
expect(actualTest.toString()).to.equals(/\.(jsx?|vue)$/.toString());
});
});