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

fix: do not ignore incorrect patterns with braces of single value #992

Merged
merged 23 commits into from
Aug 6, 2021

Conversation

iiroj
Copy link
Member

@iiroj iiroj commented Jul 13, 2021

Technically brace patterns like *.{js} are invalid because braces should
always contain at least one comma or a sequence,
for example *.{js,ts} or file{1..10}.js.

The micromatch library used to match patterns to files will silently ignore
such invalid braces and thus lead to the pattern always matching zero files.
This is a unintuitive, so lint-staged should normalize such cases
by simply removing the unnecessary braces before matching files.

@iiroj iiroj mentioned this pull request Jul 13, 2021
@codecov
Copy link

codecov bot commented Jul 13, 2021

Codecov Report

Merging #992 (c9a24ce) into master (f8807d7) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #992   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           19        19           
  Lines          633       641    +8     
  Branches       148       146    -2     
=========================================
+ Hits           633       641    +8     
Impacted Files Coverage Δ
lib/generateTasks.js 100.00% <100.00%> (ø)
lib/index.js 100.00% <100.00%> (ø)
lib/makeCmdTasks.js 100.00% <100.00%> (ø)
lib/messages.js 100.00% <100.00%> (ø)
lib/validateBraces.js 100.00% <100.00%> (ø)
lib/validateConfig.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f8807d7...c9a24ce. Read the comment docs.

@iiroj iiroj force-pushed the braces-single-value branch from 711fdb2 to 80bd709 Compare July 13, 2021 12:21
@iiroj iiroj requested a review from okonet July 13, 2021 12:21
@iiroj iiroj force-pushed the braces-single-value branch from 80bd709 to 438b209 Compare July 13, 2021 12:22
@iiroj iiroj changed the title fix: correctly handle incorrect patterns withs braces of only single value fix: correctly handle incorrect patterns with braces of only single value Jul 13, 2021
@iiroj iiroj changed the title fix: correctly handle incorrect patterns with braces of only single value fix: do not ignore incorrect patterns with braces of single value Jul 13, 2021
@iiroj
Copy link
Member Author

iiroj commented Jul 13, 2021

@okonet and @theKashey, what do you think of this approach?

@okonet
Copy link
Collaborator

okonet commented Jul 13, 2021

I love it!

@iiroj
Copy link
Member Author

iiroj commented Jul 13, 2021

We don't seem to have a test for sequences like file{1..10}.js, so I'll add that as well.

@iiroj
Copy link
Member Author

iiroj commented Jul 13, 2021

@okonet on second thought, this same thing could be done earlier in the chain, while validating configuration. Thoughts? We could even go further and actually check the globs are valid using micromatch.

@okonet
Copy link
Collaborator

okonet commented Jul 14, 2021

I think it makes sense

@theKashey
Copy link

Believe it or not but I had #980 opened as the third tab in my browser for all this time.
Thank you so much for making it real

).map((file) => normalize(relative ? file : path.resolve(cwd, file)))
// Only worry about children of the CWD unless the pattern explicitly
// specifies that it concerns a parent directory.
const filteredFiles = relativeFiles.filter((file) => {

Choose a reason for hiding this comment

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

Not a real issue, but I have a habit to always provide a bit of advice if I can:
generateTask.js is more about "generate" and "task", and this moment is more about clearing the user input and some sort of validation.
Wondering - is it possible to move this logic into validateConfig, where it gravitates more remove connectivity with messages and logger. However, it will also create new connectivity between generateTasks and validateConfig which does not exist currently.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I actually had the same thought in #992 (comment) after making these changes.

It makes sense to move it there, however currently validateConfig mostly just throws errors, and in this case it would simply warn. I guess that's ok though.

Copy link
Member Author

Choose a reason for hiding this comment

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

This should now be moved to validateConfig

@iiroj
Copy link
Member Author

iiroj commented Jul 15, 2021

I improved the error messages generated from validateConfig a bit while I was at it:

Screenshot 2021-07-15 at 9 49 53

@iiroj
Copy link
Member Author

iiroj commented Jul 22, 2021

@okonet We should probably also handle escaped braces like *.\{js\}. With this implementation that would be reformatted as *.\js\ which doesn't seem right...

@okonet
Copy link
Collaborator

okonet commented Jul 22, 2021

Yeah that seems like a bug on our end? If brackets are escaped, I'd not modify them.

@iiroj
Copy link
Member Author

iiroj commented Jul 22, 2021

Let's see if the regex can be updated to detect this.

@iiroj iiroj force-pushed the braces-single-value branch from 7658e3b to c7eb8fc Compare July 22, 2021 13:33
@iiroj
Copy link
Member Author

iiroj commented Jul 22, 2021

@okonet I'm pretty sure I managed to get the RegExp working, but it was the hardest one I've done by far... Please see commit 2d7f2ec

okonet
okonet previously approved these changes Jul 22, 2021
iiroj added 7 commits July 25, 2021 18:57
…alue

Technically brace patterns like `*.{js}` are invalid because braces should
always contain at least one comma or a sequence,
for example `*.{js,ts}` or `file{1..10}.js`.

The `micromatch` library used to match patterns to files will silently ignore
such invalid braces and thus lead to the pattern always matching zero files.
This is a unintuitive, so lint-staged should normalize such cases
by simply removing the unnecessary braces before matching files.
@iiroj iiroj force-pushed the braces-single-value branch from 54f40ea to e195b6a Compare July 25, 2021 15:57
@iiroj
Copy link
Member Author

iiroj commented Jul 25, 2021

@okonet since brace expansion supports nested braces, I have to make their validation recursive to handle cases like this:

  ● validateBraces › should warn about `*.{js,{ts}}` and return fixed pattern

    expect(received).toEqual(expected) // deep equality

    Expected: "*.{js,ts}"
    Received: "*.js,{ts}"

@iiroj
Copy link
Member Author

iiroj commented Jul 25, 2021

I think I managed to make it work as expected with nested braces as well...

@iiroj
Copy link
Member Author

iiroj commented Aug 5, 2021

@okonet this could use a review

@iiroj iiroj merged commit b3d97cf into master Aug 6, 2021
@iiroj iiroj deleted the braces-single-value branch August 6, 2021 05:08
@github-actions
Copy link
Contributor

github-actions bot commented Aug 6, 2021

🎉 This PR is included in version 11.1.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

3 participants