Skip to content

Get index of node within source string #12

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

Closed
jeddy3 opened this issue Oct 17, 2015 · 18 comments
Closed

Get index of node within source string #12

jeddy3 opened this issue Oct 17, 2015 · 18 comments

Comments

@jeddy3
Copy link

jeddy3 commented Oct 17, 2015

Would it be possible to support getting the string-index of a node within its source string? Similar to how postcss-selector-parser does it.

The use-case is for stylelint, to feed these indexes into PostCSS 5's new warning index option, in order to get accurate positions for errors.

@TrySound
Copy link
Owner

@jeddy3 Do you mean position start?

@jeddy3
Copy link
Author

jeddy3 commented Oct 17, 2015

Yep. I think so.

So given a value of:

red, rgba(0, 0, 0, 0);

You'll get something like

nodes: [
      type: 'function',
      value: 'rgba',
      before: "",
      after: "",
      sourceIndex: 5 // the start position of the `rgba` in the original value string

Although, now I'm wondering if I misunderstood our needs. I'm going to ask for @davidtheclark advice on this.

@davidtheclark I'm working on function-blacklist at the moment and I believe I need to get the start position of the offending function name to pass into report().

So given

a {
  background-image: 
    linear-gradient(45deg, blue, red),
    linear-gradient(90deg, black, white);
}

Am I right in thinking I'm after the start position of the two function names within the value string? And this is something we're hoping to get from the value parser?

@davidtheclark
Copy link
Collaborator

Yes, @jeddy3: That link to the postcss-selector-parser feature is dead on: that's what we need for a completely accurate position.

@jeddy3
Copy link
Author

jeddy3 commented Oct 18, 2015

Thanks for the clarification @davidtheclark .

@TrySound Do you think it'll be possible?

@TrySound
Copy link
Owner

@jeddy3 Yep, I'll do that.

@jeddy3
Copy link
Author

jeddy3 commented Oct 18, 2015

Thank you :)

@TrySound
Copy link
Owner

@jeddy3 @davidtheclark Do you expect sourceIndex in div since space before or div char?
word , -> sourceIndex: 5 without space
word , -> sourceIndex: 4 with space

@jeddy3
Copy link
Author

jeddy3 commented Oct 19, 2015

That's a good question. I've not used the postcss-selector-parser API yet, so I'm going to ping @ben-eb to ask which of these two behaviours would be most consistent with what is in postcss-selector-parser?

@ben-eb
Copy link
Collaborator

ben-eb commented Oct 19, 2015

Since sourceIndex is relative to the original string, whitespace must be taken into account for the position to be accurate. So for word , the comma would be sourceIndex: 5.

@TrySound
Copy link
Owner

@ben-eb You mean 4? :)

@ben-eb
Copy link
Collaborator

ben-eb commented Oct 19, 2015

sourceIndex should be zero based. 😄

@TrySound
Copy link
Owner

@ben-eb posDiv - before.length ?

@ben-eb
Copy link
Collaborator

ben-eb commented Oct 19, 2015

Whoops. I meant 4. 😄

@jeddy3
Copy link
Author

jeddy3 commented Oct 19, 2015

@ben-eb Thanks for the popping by to answer the question :)

@TrySound Thanks again for getting this done, and so quickly too :)

@davidtheclark
Copy link
Collaborator

Wait ... in the word ,, the index of , should be 5 --- I don't understand why you both settled on 4. 'word ,'.indexOf(',') === 5

@davidtheclark
Copy link
Collaborator

Nevertheless ... I reviewed a bunch of the tests in the PR https://github.com/TrySound/postcss-value-parser/pull/17/files and they all made sense/seemed right to me, so maybe I'm just missing something here.

@TrySound
Copy link
Owner

@davidtheclark It takes also the space before which is start of the node

{ type: 'div', sourceIndex: 4, value: ',', before: ' ', after: '' }

@davidtheclark
Copy link
Collaborator

👍

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

No branches or pull requests

4 participants