Skip to content

Inconsistent source end location #1876

@idoros

Description

@idoros

There seems to be an inconsistent source mapping results for nested nodes.

The simplest example shows that the root node range starts at 0 and ends after the closing } while the single rule range is closing before the ending }

const root = postcss.parse('.a{}');
/*
root.source {
    start: { offset: 0 },
    end:   { offset: 4 } // after '}'
}
root.nodes[0].source {
    start: { offset: 0 },
    end:   { offset: 3 } // before '}'
}
*/

With a single declaration (and no closing semicolon):

const root = postcss.parse('.a{b:c}');
/*
root.source {
    start: { offset: 0 },
    end:   { offset: 7 }
}
root.nodes[0].source {
    start: { offset: 0 },
    end:   { offset: 6 } // before '}'
}
root.nodes[0].nodes[0].source {
    start: { offset: 3 },
    end:   { offset: 5 } // before 'c'
}
*/

With a semicolon the declaration seems to end correctly, although I would prefer to include the semicolon as part of the declaration:

postcss.parse('.a{b:c;}');
/*
root.source {
    start: { offset: 0 },
    end:   { offset: 8 }
}
root.nodes[0].source {
    start: { offset: 0 },
    end:   { offset: 7 } // before '}'
}
root.nodes[0].nodes[0].source {
    start: { offset: 3 },
    end:   { offset: 6 } // before ';'
}
*/

With 2 declaration you can see that the second one is off and the first is "fixed" by the semicolon:

postcss.parse('.a{b:c;d:e}');
/*
root.source {
    start: { offset: 0 },
    end:   { offset: 11 }
}
root.nodes[0].source {
    start: { offset: 0 },
    end:   { offset: 10 } // before '}'
}
root.nodes[0].nodes[0].source {
    start: { offset: 3 },
    end:   { offset: 6 } // before ';'
}
root.nodes[0].nodes[1].source {
    start: { offset: 7 },
    end:   { offset: 9 } // before 'e'
}
*/

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions