You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
node.source.end.offset is wrong value in some cases #2030
import{Declaration,parse,Rule}from'postcss';constcss=parse('a { one: X }');consta=css.firstasRule;letone=a.firstasDeclaration;console.log(css.source?.end);// okconsole.log(a.source?.end);// NGconsole.log(one.source?.end);// NG
The problem stemmed from direct use of the token location as the offset when resolving line and column with getPosition(), which worked well for start.offset, but should point to the index immediately after the node for the end.offset to ensure that source.slice(start.offset, end.offset) provides the correct node source.
The solution is straightforward: I've reviewed the parser's instances where the end offset is set and made a simple adjustment off adding 1 (except the final root location that works as expected).
The start and end offset values look correct to me. They allow slicing the source string to extract the exact AST node and align with other parsers (css-tree, CSSOM, TypeScript, Acorn, etc.).
Looking at the input source "a { one: X }":
0 "a"
1 " "
2 "{"
3 " "
4 "o"
5 "n"
6 "e"
7 ":"
8 " "
9 "X"
10 " "
11 "}"
The parsed results:
constsource="a { one: X }";constroot=parse(source);root// offset = start: 0, end: 12source.slice(0,12)// "a { one: X }"construle=root.first;// offset = start: 0, end: 12source.slice(0,12)// "a { one: X }"constdecl=rule.first;// offset = start: 4, end: 10: source.slice(4,10)// "one: X"
If the rule's end offset were changed to 11, it would result in "a { one: X ".
Similarly, setting the declaration's end offset to 9 would produce "one: " instead of "one: X".
Thanks for explaining! But this behavior confuses the user. I hope I can improve it. What do you guys think?
I have two ideas to improve it.
Idea. 1: Make end.offset inclusive
Make end.offset inclusive and convert it to exclusive on the slice side of the source text.
consttext='a { color: black }'constroot=postcss.parse(text)constcolor=root.first.first// this is inclusive offsetconsole.log(color.source.end.offset)// 15constsliced=text.slice(color.source.start.offset,color.source.end.offset+1)console.log(sliced)// 'color: black'
This way, line, column, and offset in source.end are all inclusive, so there is no mixing of inclusive and exclusive properties. However, this is a breaking change and could be more confusing to users.
Idea. 2: Add source.exclusiveEnd.
Keep the end property but add an exclusiveEnd property.
This way, line, column, and offset in source.end are all inclusive, so there is no mixing of inclusive and exclusive properties. However, this is a breaking change and could be more confusing to users.
I think this would be a mistake.
column/line and offset are two completely different systems and making them the same (inclusive vs. exclusive) would just be wrong.
The current implementation really is correct (ignoring any potential bugs) and I don't think there is a strong use case for adding even more complexity.
Please don't change this :)
Add source.exclusiveEnd.
Is there any program or use case that would actually benefit from this?
Is this maybe a bug on the other end?
As far as I know you either need column/line or offset and both are correct for their respective use cases. A bug in a downstream program might make it appear as if PostCSS has a shortcoming?
But the actual value is { column: 13, line: 1, offset: 12 }. Is this a bug?
Yes this could be seen as a bug.
The column is calculated using the position of the tokenizer:
The tokenizer will always advance to the next position, even if this goes "out of bounds".
So the end of file is always "one position after the end of the file".
I think this is fairly harmless in practice because other programs will do bounds checks.
But maybe it is worth fixing this?
We should investigate the impact of such a change on Stylelint and Stylelint VSCode.
There is always the chance that my quick analysis is incorrect and the current code is correct after all :)
According to the jsdoc, node.source.end represents an inclusive position. That’s why I thought node.source.end.offset is an inclusive offset. Is my interpretation wrong?
I think this is fairly harmless in practice because other programs will do bounds checks.
But maybe it is worth fixing this?
I too am wondering if it is worth fixing it. Frankly, I am not interested in fixing this bug. I’ll let it slide.
Activity
mizdra commentedon Mar 22, 2025
Off topic, but why don't
css.endanda.endmatch? I thinkcss.endshould be{ column: 12, line: 1, offset: 11 }. That's the end of the file.But the actual value is
{ column: 13, line: 1, offset: 12 }. Is this a bug?mizdra commentedon Mar 22, 2025
Uh, is #2030 (comment) expected behavior?
According to #1879:
ai commentedon Mar 22, 2025
cc @idoros
Also, I think we should call @romainmenke to not be sure that we are not breaking any of his Stylelint fixes for position.
idoros commentedon Mar 22, 2025
The start and end offset values look correct to me. They allow slicing the source string to extract the exact AST node and align with other parsers (css-tree, CSSOM, TypeScript, Acorn, etc.).
Looking at the input source
"a { one: X }":The parsed results:
If the rule's end offset were changed to
11, it would result in"a { one: X ".Similarly, setting the declaration's end offset to
9would produce"one: "instead of"one: X".mizdra commentedon Mar 23, 2025
Thanks for explaining! But this behavior confuses the user. I hope I can improve it. What do you guys think?
I have two ideas to improve it.
Idea. 1: Make
end.offsetinclusiveMake
end.offsetinclusive and convert it to exclusive on the slice side of the source text.This way,
line,column, andoffsetinsource.endare all inclusive, so there is no mixing of inclusive and exclusive properties. However, this is a breaking change and could be more confusing to users.Idea. 2: Add
source.exclusiveEnd.Keep the
endproperty but add anexclusiveEndproperty.The size of the AST will increase, but backward compatibility will be maintained. There may also be a slight increase in parsing costs.
romainmenke commentedon Mar 23, 2025
I think this would be a mistake.
column/lineandoffsetare two completely different systems and making them the same (inclusive vs. exclusive) would just be wrong.The current implementation really is correct (ignoring any potential bugs) and I don't think there is a strong use case for adding even more complexity.
Please don't change this :)
Is there any program or use case that would actually benefit from this?
Is this maybe a bug on the other end?
As far as I know you either need
column/lineoroffsetand both are correct for their respective use cases. A bug in a downstream program might make it appear as if PostCSS has a shortcoming?Yes this could be seen as a bug.
The
columnis calculated using the position of the tokenizer:postcss/lib/parser.js
Line 340 in ff48c29
The tokenizer will always advance to the next position, even if this goes "out of bounds".
So the end of file is always "one position after the end of the file".
I think this is fairly harmless in practice because other programs will do bounds checks.
But maybe it is worth fixing this?
We should investigate the impact of such a change on Stylelint and Stylelint VSCode.
There is always the chance that my quick analysis is incorrect and the current code is correct after all :)
mizdra commentedon Mar 24, 2025
I understand what you're saying. However, how should I interpret the jsdoc for
node.source.end?postcss/lib/node.d.ts
Lines 66 to 70 in ff48c29
According to the jsdoc,
node.source.endrepresents an inclusive position. That’s why I thoughtnode.source.end.offsetis an inclusive offset. Is my interpretation wrong?I too am wondering if it is worth fixing it. Frankly, I am not interested in fixing this bug. I’ll let it slide.
ai commentedon Jul 22, 2026
Should be fixed in the latest version