feat: add ignoreUnclosed option to nextToken method #1191
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR stems from an issue in postcss-less and a short discussion on twitter with @ai.
Use Case
PostCSS v7 has a wonderful parser class structure that enabled syntaxes to be created by inheriting from the base
Parser
class and selectively overriding methods to handle syntax nuances - all without having to reimplement a parser or tokenizer. That's a massive advantage to developers. However, there are some strict rules hardcoded into the tokenizer. Because the tokenizer is optimized for performance, it cannot be made a class to be inherited from. A mechanism is then needed to work around/with the hardcoded tokenizer rules.In some situations it would be preferable to fetch the next token from a tokenizer while ignoring any
unclosed
errors for only the next token request. This is different than instantiating the tokenizer with theignoreErrors
options, as it only ignoresunclosed
for a singletokenizer.nextToken()
request.This PR implements an
ignoreUnclosed
option. The naming of that option was chosen because that option is only used on lines where theelse
cases result in theunclosed(...)
method being called, and an "Unclosed X" error is thrown. Hence the naming is more specific, but should aid in usability and providing a distinction between the top-level tokenizer optionignoreErrors
and this newnextToken
method option.Example Usage: