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

feat: apply .ignores to lintText #169

Merged
merged 5 commits into from
Dec 13, 2016
Merged

Conversation

marionebl
Copy link
Contributor

@marionebl marionebl commented Nov 26, 2016

  • add multimatch-powered short-circuit to lintText
  • add supporting test cases
  • throw for opts.ignores without opts.filename
  • use default ignores when opts.filename is given
  • closes lintText does not apply ignores config #168

BREAKING CHANGES:

  • lintText now uses the same default ignores as lintFiles with opts.filename
    • /node_modules/
    • /bower_components/
    • coverage/**
    • {tmp,temp}/**
    • **/*.min.js
    • **/bundle.js
    • fixtures{-*,}.{js,jsx}
    • fixture{s,}/**
    • {test,tests,spec,tests}/fixture{s,}/**
    • vendor/**
    • dist/**
  • lintText now throws when providing opts.ignores without opts.filename

- add multimatch-powered short-circuit to `lintText`
- add supporting test cases
- closes #168
}]
};
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This needs to take into account the overrides logic and should be after the below overrides logic. Right now it wouldn't work if you used config overrides that enabled it again. Should also have a test for that to ensure it works.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Amounts to another breaking change as the default ignores now take effect

Copy link
Contributor Author

Choose a reason for hiding this comment

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

const optionsManager = require('./options-manager');

exports.lintText = (str, opts) => {
opts = optionsManager.preprocess(opts);

if (opts.cwd && opts.filename) {
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 user-friendly, I think we should throw an error if the user of lintText tries to specify the ignore option, but didn't supply cwd and filename.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! Guess this is a breaking change as passing ignores before these changes did nothing.

*  merge applicable overrides before ignoring files
*  throw for opts.ignores without opts.filename
*  use default ignores when opts.filename is given
*  add test case for default ignores

BREAKING CHANGES:
* lintText now uses the same default ignores as lintFiles with opts.filename
  * **/node_modules/**
  * **/bower_components/**
  * coverage/**
  * {tmp,temp}/**
  * **/*.min.js
  * **/bundle.js
  * fixtures{-*,}.{js,jsx}
  * fixture{s,}/**
  * {test,tests,spec,__tests__}/fixture{s,}/**
  * vendor/**
  * dist/**
* lintText now throws when providing opts.ignores without opts.filename
t.is(result.warningCount, 0);
});

test('.lintText() - respect overrides', t => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

const foundOverrides = optionsManager.findApplicableOverrides(filename, overrides);
opts = optionsManager.mergeApplicableOverrides(opts, foundOverrides.applicable);
}

opts = optionsManager.buildConfig(opts);
const defaultIgnores = optionsManager.getIgnores({}).ignores;

if (opts.ignores && !arrayEqual(defaultIgnores, opts.ignores) && typeof opts.filename !== 'string') {
Copy link
Member

@sindresorhus sindresorhus Nov 29, 2016

Choose a reason for hiding this comment

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

I don't really understand the purpose of the !arrayEqual(defaultIgnores, opts.ignores) check. ?

I would also prefer using lodash.isequal instead as it's more general and could be used for other deep checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • array-equal => lodash.isequal
  • the check is there to test if the user provided ignores via .ignores or .overrides[index].ignores. opts.ignores is always defined

const defaultIgnores = optionsManager.getIgnores({}).ignores;

if (opts.ignores && !arrayEqual(defaultIgnores, opts.ignores) && typeof opts.filename !== 'string') {
throw new Error(`xo: opts.filename must be string when providing opts.ignores. Received value "${opts.filename}" of type "${typeof opts.filename}" for opts.filename.`);
Copy link
Member

@sindresorhus sindresorhus Nov 29, 2016

Choose a reason for hiding this comment

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

This is a bit too verbose. People will most likely never hit this as we're the only ones using the API. I also don't see the point of the xo: prefix. It's clear from the stack trace where the error is coming from.

I'd prefer something like:

> The `ignores` option requires the `filename` option to be defined.

overrides: [
{
files: ['ignored/**/*.js'],
ignores: []
Copy link
Member

Choose a reason for hiding this comment

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

In addition, can you also do an overrides test that has overridden ignores?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@marionebl
Copy link
Contributor Author

@sindresorhus Could you have another look?

@sindresorhus
Copy link
Member

the check is there to test if the user provided ignores via .ignores or .overrides[index].ignores. opts.ignores is always defined

Feels a bit overkill. How about just putting a variable at the top with:

const hasIgnores = Array.isArray(opts.ignores);

And using that further down.

@marionebl
Copy link
Contributor Author

marionebl commented Nov 29, 2016

Feels a bit overkill. How about just putting a variable at the top with:

const hasIgnores = Array.isArray(opts.ignores);
And using that further down.

That would not handle ignores in opts.overrides then. We probably could check on both places but I thought the current solution to be more elegant. Change accordingly?

@sindresorhus
Copy link
Member

Right, forgot about the ignores in overrides. I'm still not getting how !isEqual(defaultIgnores, opts.ignores) works though (might be because it's late here and I'm half asleep). Wouldn't it have a false-positive if both defaultIgnores were opts.ignores empty arrays?

@marionebl
Copy link
Contributor Author

Not sure if the current approach is good enough if it is hard to grasp. Let's defer this until tomorrow and have some sleep 💤

@marionebl
Copy link
Contributor Author

Coming back to this: Given there was a case with two empty array this would do the right thing and skip the error-producing code, right?

@marionebl
Copy link
Contributor Author

@sindresorhus Can we move forward?

@sindresorhus sindresorhus merged commit 2e14d1b into master Dec 13, 2016
@sindresorhus sindresorhus deleted the feat/apply-ignores-lint-text branch December 13, 2016 12:18
@sindresorhus
Copy link
Member

Yes, sorry about the delay. Looks great. Thank you :)

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.

lintText does not apply ignores config
2 participants