Skip to content
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

Fix incorrect end loc for style attr template #44

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions lib/template/template-tokenize.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function templateTokenize(...args) {

function nextToken(...args) {
const returned = [];
let token, line, column;
let token, lastPos;
let depth = 0;

while ((token = tokenizer.nextToken.apply(tokenizer, args))) {
Expand All @@ -19,8 +19,7 @@ function templateTokenize(...args) {
}
}
if (depth || returned.length) {
line = token[4] || token[2] || line;
column = token[5] || token[3] || column;
lastPos = token[3] || token[2] || lastPos;
returned.push(token);
}
if (!depth) {
Expand All @@ -32,9 +31,7 @@ function templateTokenize(...args) {
"word",
returned.map((token) => token[1]).join(""),
returned[0][2],
returned[0][3],
line,
column,
lastPos,
];
}
return token;
Expand Down
6 changes: 6 additions & 0 deletions test-fixtures/ast/test.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
let bgOpacity = 0.5;
$: color = bgOpacity < 0.6 ? '#000' : '#fff';
</script>
<p style="color: {color}; --opacity: {bgOpacity};">This is a paragraph.</p>
<p style="color: {color}">This is a paragraph.</p>
157 changes: 157 additions & 0 deletions test/__snapshots__/ast.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,163 @@ Object {
}
`;

exports[`AST tests test.svelte ast 1`] = `
Object {
"inputs": Array [
Object {
"css": "color: {color}; --opacity: {bgOpacity};",
"file": "/test.svelte",
"hasBOM": false,
},
Object {
"css": "color: {color}",
"file": "/test.svelte",
"hasBOM": false,
},
Object {
"css": "<script>
let bgOpacity = 0.5;
$: color = bgOpacity < 0.6 ? '#000' : '#fff';
</script>
<p style=\\"color: {color}; --opacity: {bgOpacity};\\">This is a paragraph.</p>
<p style=\\"color: {color}\\">This is a paragraph.</p>
",
"file": "/test.svelte",
"hasBOM": false,
},
],
"nodes": Array [
Object {
"document": "$document",
"indexes": Object {},
"lastEach": 1,
"nodes": Array [
Object {
"prop": "color",
"raws": Object {
"before": "",
"between": ": ",
},
"source": Object {
"end": Object {
"column": 25,
"line": 5,
"offset": 112,
},
"inputId": 0,
"start": Object {
"column": 11,
"line": 5,
"offset": 98,
},
},
"type": "decl",
"value": "{color}",
},
Object {
"prop": "--opacity",
"raws": Object {
"before": " ",
"between": ": ",
},
"source": Object {
"end": Object {
"column": 49,
"line": 5,
"offset": 136,
},
"inputId": 0,
"start": Object {
"column": 27,
"line": 5,
"offset": 114,
},
},
"type": "decl",
"value": "{bgOpacity}",
},
],
"raws": Object {
"after": "",
"codeBefore": "<script>
let bgOpacity = 0.5;
$: color = bgOpacity < 0.6 ? '#000' : '#fff';
</script>
<p style=\\"",
"semicolon": true,
},
"source": Object {
"end": undefined,
"inputId": 0,
"start": Object {
"column": 11,
"line": 5,
"offset": 98,
},
},
"type": "root",
},
Object {
"document": "$document",
"indexes": Object {},
"lastEach": 1,
"nodes": Array [
Object {
"prop": "color",
"raws": Object {
"before": "",
"between": ": ",
},
"source": Object {
"end": Object {
"column": 24,
"line": 6,
"offset": 187,
},
"inputId": 0,
"start": Object {
"column": 11,
"line": 6,
"offset": 174,
},
},
"type": "decl",
"value": "{color}",
},
],
"raws": Object {
"after": "",
"codeAfter": "\\">This is a paragraph.</p>
",
"codeBefore": "\\">This is a paragraph.</p>
<p style=\\"",
"semicolon": false,
},
"source": Object {
"end": undefined,
"inputId": 0,
"start": Object {
"column": 11,
"line": 6,
"offset": 174,
},
},
"type": "root",
},
],
"raws": Object {},
"source": Object {
"end": undefined,
"inputId": 0,
"start": Object {
"column": 1,
"line": 1,
},
},
"type": "document",
}
`;

exports[`AST tests test.vue ast 1`] = `
Object {
"inputs": Array [
Expand Down