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: Report end indices for nodes #83

Merged
merged 7 commits into from
Nov 29, 2021

Conversation

adalinesimonian
Copy link
Contributor

Follow-up on #12, needed for stylelint/stylelint#5694

We're implementing range reporting in Stylelint, and as such, need ways to know not just the starting index of a node, but its end index. This isn't as straightforward as looking at the start of the next node, or even the end of the declaration value. Functions may or may not have closing parentheses, which affects where the end is. Nested nodes or functions make it difficult to know downstream precisely where the end of a nested node is.

This PR adds an end index to every token/node. To be as accurate as possible, instead of implementing the feature first, I went through each test case manually and set the expected end indices where they should be. Then, I implemented the feature such that it would make all the test cases pass. This was possible due to how comprehensive the tests are.

@adalinesimonian
Copy link
Contributor Author

Test results:

PS C:\Users\Adaline\GitHub\TrySound\postcss-value-parser> yarn test
yarn run v1.22.17
$ yarn lint
$ yarn lint:js && yarn lint:prettier
$ eslint . --cache
$ prettier "**/*.js" "**/*.ts" --list-different
$ tape test/*.js | tap-spec

  ValueParser


  i/o

    √ " rgba( 34 , 45 , 54, .5 ) "
    √ "w1 w2 w6 \n f(4) ( ) () \t \"s't\" 'st\\\"2'"

  walk

    √ should process all functions
    √ shouldn't process functions after falsy callback
    √ shouldn't process nodes with defined non-function type
    √ should process all functions with reverse mode

  Parse

    √ should correctly process empty input
    √ should process escaped parentheses (open)
    √ should process escaped parentheses (close)
    √ should process escaped parentheses (both)
    √ should process escaped parentheses (both)
    √ should process unopened parentheses as word
    √ should add before prop
    √ should add before and after prop
    √ should add value prop
    √ should process nested functions
    √ should process advanced nested functions
    √ should process divider (/)
    √ should process divider (:)
    √ should process divider (,)
    √ should process complex divider
    √ should process divider in function
    √ should process two spaced divider
    √ should process empty quoted strings (")
    √ should process empty quoted strings (')
    √ should process escaped quotes (')
    √ should process escaped quotes (')
    √ should process single quotes inside double quotes (')
    √ should process double quotes inside single quotes (')
    √ should process unclosed quotes
    √ should process unclosed quotes with ended backslash
    √ should process quoted strings
    √ should process quoted strings and words
    √ should process quoted strings and spaces
    √ should process escaped symbols as words
    √ should correctly proceess font value
    √ should correctly proceess color value
    √ should correctly process url function
    √ should add unclosed: true prop for url function
    √ should correctly process url function with quoted first argument
    √ should correctly parse spaces
    √ should correctly parse subtraction with spaces
    √ should correctly parse multiplication with spaces
    √ should correctly parse division with spaces
    √ should correctly parse multiplication without spaces
    √ should correctly parse division without spaces
    √ should correctly process nested calc functions
    √ should process colons with params
    √ should escape parentheses with backslash
    √ should parse parentheses correctly
    √ shouldn't throw an error on unclosed function
    √ should add unclosed: true prop for every unclosed function
    √ shouldn't throw an error on unopened function
    √ should process escaped spaces as word in fonts
    √ should parse double url and comma
    √ should parse empty url
    √ should parse empty url with space
    √ should parse empty url with multiple spaces
    √ should parse empty url with newline (LF)
    √ should parse empty url with newline (CRLF)
    √ should parse empty url with multiple newlines (LF)
    √ should parse empty url with multiple newlines (CRLF)
    √ should parse empty url with whitespace characters
    √ should parse comments
    √ should parse comments inside functions
    √ should parse comments at the end of url functions with quoted first argument
    √ should not parse comments at the start of url function with unquoted first argument
    √ should not parse comments at the end of url function with unquoted first argument
    √ should parse unclosed comments
    √ should respect escape character
    √ should parse unicode-range (single codepoint)
    √ should parse unicode-range (single codepoint) 2
    √ should parse unicode-range (single codepoint) 3
    √ should parse unicode-range (single codepoint) (lowercase)
    √ should parse unicode-range (codepoint range)
    √ should parse unicode-range (wildcard range)
    √ should parse unicode-range (multiple values)
    √ should parse invalid unicode-range as word
    √ should parse invalid unicode-range as word 2
    √ should parse invalid unicode-range as word 2

  Stringify

    √ Should correctly add quotes
    √ Should not close unclosed strings
    √ Should correctly add brackets
    √ Should not close unclosed functions
    √ Should correctly process advanced gradients
    √ Should correctly add comments
    √ Should correctly process comments inside functions
    √ Should correctly process empty url
    √ Should correctly process empty url with space
    √ Should correctly process empty url with spaces
    √ Should correctly process empty url with tab
    √ Should correctly process empty url with newline (LF)
    √ Should correctly process empty url with newline (LF)
    √ Should correctly process empty url with multiple newlines (CRLF)
    √ Should correctly process empty url with multiple newlines (CRLF)
    √ Should correctly process empty url whitespace characters
    √ Should correctly process unicode-range (single codepoint)
    √ Should correctly process unicode-range (codepoint range)
    √ Should correctly process unicode-range (wildcard range)
    √ Should correctly process unicode-range (multiple values)
    √ should be equal
    √ should be equal
    √ Shouldn't process nodes of work type
    √ should be equal

  Unit

    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent
    √ should be equivalent


  total:     171
  passing:   171
  duration:  38ms


Done in 2.13s.
PS C:\Users\Adaline\GitHub\TrySound\postcss-value-parser> 

@adalinesimonian adalinesimonian changed the title Report end indices for nodes feat: Report end indices for nodes Nov 24, 2021
@yoyo837
Copy link

yoyo837 commented Nov 25, 2021

Thanks for you great work. We need this.

The project doesn't seem to be active, let me make a friendly ping @alexander-akait.

@alexander-akait
Copy link
Collaborator

@TrySound Need you attention, thanks

@yoyo837
Copy link

yoyo837 commented Nov 26, 2021

Maybe the owner is not still watching this project.

@alexander-akait
Copy link
Collaborator

@yoyo837 Let's wait today, ping me if we will not answer tomorrow, I will then take over the issue

@yoyo837
Copy link

yoyo837 commented Nov 26, 2021

Let's wait today, ping me if we will not answer tomorrow, I will then take over the issue

Okay, thank you. 💗

Copy link
Collaborator

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Anyway, looks good

@yoyo837
Copy link

yoyo837 commented Nov 29, 2021

Let's have a merge. @alexander-akait 🚀

@alexander-akait alexander-akait merged commit fd00010 into TrySound:master Nov 29, 2021
@alexander-akait
Copy link
Collaborator

@adalinesimonian adalinesimonian deleted the end-indices branch November 29, 2021 20:23
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