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

Add support for ava.config.js #192

Merged
merged 11 commits into from
Jul 14, 2018

Conversation

HenriBeck
Copy link
Contributor

@HenriBeck HenriBeck commented May 31, 2018

Adds support for checking the ava.config.js when there is no ava config inside package.json.
This uses the esm module for parsing the config file.

Fixes #191

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @HenriBeck!

Config loading is a little more involved, I've left some inline comments.

Would be good to have tests as well.

util.js Outdated
@@ -33,7 +34,15 @@ exports.getAvaConfig = filepath => {

try {
const packageContent = JSON.parse(fs.readFileSync(filepath, 'utf8'));
return (packageContent && packageContent.ava) || defaultResult;

if (packageContent && packageContent.ava) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking AVA ignores "ava": {} in favor of an ava.config.js file. We should do the same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the docs:

To use an ava.config.js file:

It must be in the same directory as your package.json
Your package.json must not contain an ava property (or, if it does, it must be an empty object)

I thought it would prefer the ava object from package.json

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it throws an exception if it finds config in package.json (that is, the "ava" key contains a non-empty object) as well as an ava.config.js file. Therefore if you find an empty object here, you need to check for ava.config.js as well. But it may not exist, and that should be OK.

I'm not sure what we should do here when both configurations are provided. If we throw an exception how does ESLint surface that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you throw an error in ESLint, it just ends the process. So I think throwing an error isn't an option here.

util.js Outdated
const fileOptions = path.parse(filepath);
const avaConfig = esmRequire(path.join(fileOptions.dir, 'ava.config.js'));

return avaConfig.default;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config file may export a factory method that's invoked with an options argument. For here, that should be {projectDir: fileOptions.dir}.

We'll need to ignore MODULE_NOT_FOUND errors and return defaultResult instead.

@HenriBeck
Copy link
Contributor Author

@novemberborn I added support for the function.
About the first change request, either I miss understood the ava docs or they are wrong.

util.js Outdated
@@ -33,7 +34,15 @@ exports.getAvaConfig = filepath => {

try {
const packageContent = JSON.parse(fs.readFileSync(filepath, 'utf8'));
return (packageContent && packageContent.ava) || defaultResult;

if (packageContent && packageContent.ava) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it throws an exception if it finds config in package.json (that is, the "ava" key contains a non-empty object) as well as an ava.config.js file. Therefore if you find an empty object here, you need to check for ava.config.js as well. But it may not exist, and that should be OK.

I'm not sure what we should do here when both configurations are provided. If we throw an exception how does ESLint surface that?

util.js Outdated
@@ -2,6 +2,7 @@
const fs = require('fs');
const path = require('path');
const pkg = require('./package');
const esmRequire = require('esm')(module);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be in sync with AVA itself, you need to set {cjs: false, mode: 'all'} options.

@sindresorhus sindresorhus changed the title Add support for the ava.config.js Add support for ava.config.js Jun 24, 2018
@novemberborn
Copy link
Member

I've fixed the merge conflicts and brought the logic in line with AVA itself. I've also added some tests and updated the relevant rule documentation.

@HenriBeck are you happy with these changes?

@sindresorhus any thoughts? Would be good to merge and ship so we're compatible with both the latest AVA and ESLint.

@HenriBeck
Copy link
Contributor Author

Yeah, I think it's the right thing to return the default config when there is no config/invalid config because throwing an error would completely abort eslint.

A rule could be added which would validate the config maybe.

@novemberborn
Copy link
Member

Yeah, I think it's the right thing to return the default config when there is no config/invalid config because throwing an error would completely abort eslint.

Thanks. I'm waiting for a new esm release that adds an option we need, so will merge and release when that's done.

A rule could be added which would validate the config maybe.

Yea, see #199.

@sindresorhus sindresorhus merged commit 415b7ba into avajs:master Jul 14, 2018
@sindresorhus
Copy link
Member

Looks great :)

sindresorhus pushed a commit that referenced this pull request Jul 14, 2018
Co-authored-by: Mark Wubben <[email protected]>

Adds support for checking the `ava.config.js` when there is no `ava` config inside `package.json`.
This uses the `esm` module for parsing the config file.

Fixes #191
sindresorhus pushed a commit that referenced this pull request Jul 14, 2018
Co-authored-by: Mark Wubben <[email protected]>

Adds support for checking the `ava.config.js` when there is no `ava` config inside `package.json`.
This uses the `esm` module for parsing the config file.

Fixes #191
sindresorhus pushed a commit that referenced this pull request Jul 14, 2018
Adds support for checking the `ava.config.js` when there is no `ava` config inside `package.json`.
This uses the `esm` module for parsing the config file.

Fixes #191

Co-authored-by: Mark Wubben <[email protected]>
forresst added a commit to forresst/eslint-plugin-ava that referenced this pull request Jul 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants