Skip to content

Commit

Permalink
Fix interpolation ranges if there is a comment inside an interpolation.
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
hudochenkov committed Dec 29, 2023
1 parent de5b7f9 commit 75756b5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
40 changes: 40 additions & 0 deletions lib/__tests__/__snapshots__/parseJs.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,46 @@ exports[`simple interpolations properties property value with two interpolations
]
`;

exports[`simple interpolations properties property value. interpolation with a comment after value 1`] = `
[
{
"css": "color: \${red /* hello */}",
"interpolationRanges": [
{
"end": 52,
"start": 34,
},
],
"locationStart": {
"column": 28,
"line": 1,
},
"rangeEnd": 52,
"rangeStart": 27,
},
]
`;

exports[`simple interpolations properties property value. interpolation with a comment before value 1`] = `
[
{
"css": "color: \${/* hello */ red}",
"interpolationRanges": [
{
"end": 52,
"start": 34,
},
],
"locationStart": {
"column": 28,
"line": 1,
},
"rangeEnd": 52,
"rangeStart": 27,
},
]
`;

exports[`simple interpolations selectors comment in selector with interpolation 1`] = `
[
{
Expand Down
12 changes: 12 additions & 0 deletions lib/__tests__/parseJs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ describe('simple interpolations', () => {
expect(document).toMatchSnapshot();
});

test('property value. interpolation with a comment after value', () => {
let document = parseJs('let Component = styled.div`color: ${red /* hello */}`;');

expect(document).toMatchSnapshot();
});

test('property value. interpolation with a comment before value', () => {
let document = parseJs('let Component = styled.div`color: ${/* hello */ red}`;');

expect(document).toMatchSnapshot();
});

test('property value with symbol right before interpolation', () => {
let document = parseJs('let Component = styled.div`margin: -${space}`;');

Expand Down
11 changes: 9 additions & 2 deletions lib/parseJs.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,16 @@ function getNodeCssData(node, inputCode, sourceFile) {
let templateSpan = node.template.templateSpans[index];

// To include `${`
let start = templateSpan.expression.pos - 2;
let start = templateSpan.pos - 2;
// To include `}`
let end = templateSpan.expression.end + 1;
let end = templateSpan.literal.end - templateSpan.literal.text.length;

if (ts.isTemplateTail(templateSpan.literal)) {
end = end - 1;
} else {
// If it's a TemplateMiddle
end = end - 2;
}

interpolationRanges.push({ start, end });
}
Expand Down

0 comments on commit 75756b5

Please sign in to comment.