Skip to content

node.source.end.offset is wrong value in some cases #2030

Closed

Description

@mizdra

Summary

In f09e296, offset property is added to Node.Position. But, the property is wrong value in some cases. The offset is 1 larger than expected.

Reproduction

import { Declaration, parse, Rule } from 'postcss';

const css = parse('a { one: X }');
const a = css.first as Rule;
let one = a.first as Declaration;

console.log(css.source?.end); // ok
console.log(a.source?.end); // NG
console.log(one.source?.end); // NG
$ npm start

> start
> ts-node index.ts

{ column: 13, line: 1, offset: 12 }
{ column: 12, line: 1, offset: 12 }
{ column: 10, line: 1, offset: 10 }

Expected Behavior

The following will be output:

$ npm start

> start
> ts-node index.ts

{ column: 13, line: 1, offset: 12 }
{ column: 12, line: 1, offset: 11 }
{ column: 10, line: 1, offset: 9 }

Additional Information

Activity

  1. mizdra commented on Mar 22, 2025

    @mizdra
    ContributorAuthor

    Off topic, but why don't css.end and a.end match? I think css.end should 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?

  2. mizdra commented on Mar 22, 2025

    @mizdra
    ContributorAuthor

    Uh, is #2030 (comment) expected behavior?

    According to #1879:

    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).

  3. ai commented on Mar 22, 2025

    @ai
    Member

    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.

  4. idoros commented on Mar 22, 2025

    @idoros
    Contributor

    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:

    const source = "a { one: X }";
    const root = parse(source);
    
    root // offset =  start: 0, end: 12
    source.slice(0, 12) // "a { one: X }"
    
    const rule = root.first; // offset = start: 0, end: 12
    source.slice(0, 12) // "a { one: X }"
    
    const decl = 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".

  5. mizdra commented on Mar 23, 2025

    @mizdra
    ContributorAuthor

    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.

    const text = 'a { color: black }'
    const root = postcss.parse(text)
    const color = root.first.first
    
    // this is inclusive offset
    console.log(color.source.end.offset) // 15
    
    const sliced = 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.

    --- a/lib/parser.js
    +++ b/lib/parser.js
    @@ -66,4 +66,5 @@ class Parser {
               node.source.end = this.getPosition(token[2])
               node.source.end.offset++
    +          node.source.exclusiveEnd = this.getPosition(node.source.end.offset)
               this.semicolon = true
               break
    @@ -81,4 +82,5 @@ class Parser {
                   node.source.end = this.getPosition(prev[3] || prev[2])
                   node.source.end.offset++
    +              node.source.exclusiveEnd = this.getPosition(node.source.end.offset)
                 }
               }
    @@ -106,4 +108,5 @@ class Parser {
             node.source.end = this.getPosition(token[3] || token[2])
             node.source.end.offset++
    +        node.source.exclusiveEnd = this.getPosition(node.source.end.offset)
             this.spaces = node.raws.between
             node.raws.between = ''
    @@ -175,4 +178,5 @@ class Parser {
         node.source.end = this.getPosition(token[3] || token[2])
         node.source.end.offset++
    +    node.source.exclusiveEnd = this.getPosition(node.source.end.offset)
    
         let text = token[1].slice(2, -2)
    @@ -207,4 +211,5 @@ class Parser {
         )
         node.source.end.offset++
    +    node.source.exclusiveEnd = this.getPosition(node.source.end.offset)
    
         while (tokens[0][0] !== 'word') {
    @@ -326,4 +331,5 @@ class Parser {
           this.current.source.end = this.getPosition(token[2])
           this.current.source.end.offset++
    +      this.current.source.exclusiveEnd = this.getPosition(this.current.source.end.offset)
           this.current = this.current.parent
         } else {

    The size of the AST will increase, but backward compatibility will be maintained. There may also be a slight increase in parsing costs.

  6. romainmenke commented on Mar 23, 2025

    @romainmenke
    Contributor

    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:

    this.root.source.end = this.getPosition(this.tokenizer.position())

    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 :)

  7. mizdra commented on Mar 24, 2025

    @mizdra
    ContributorAuthor

    column/line and offset are two completely different systems and making them the same (inclusive vs. exclusive) would just be wrong.

    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

    /**
    * The inclusive ending position for the source
    * code of a node.
    */
    end?: Position

    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.

  8. ai commented on Jul 22, 2026

    @ai
    Member

    Should be fixed in the latest version

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions