Skip to content

Commit e4d839c

Browse files
committed
fix: only treat $ as part of the name inside an interpolation
Narrow the new branch to a `$` read while a `#{` or `@{` interpolation is open in the attribute name, and drop the three bare `$` name tests. `[$attr]` is not valid SCSS or CSS, and the broader condition collided with #335 on `[a$]`.
1 parent 3fe43f9 commit e4d839c

2 files changed

Lines changed: 19 additions & 22 deletions

File tree

‎src/__tests__/nonstandard.mjs‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ test("sass interpolation as an attribute name", "[#{$attr}]", (t, tree) => {
3838
t.deepEqual(tree.nodes[0].nodes[0].value, undefined);
3939
});
4040

41-
test("sass variable as an attribute name", "[$attr]", (t, tree) => {
42-
t.deepEqual(tree.nodes[0].nodes[0].attribute, "$attr");
43-
t.deepEqual(tree.nodes[0].nodes[0].operator, undefined);
44-
});
45-
4641
test(
4742
"sass interpolation on both sides of an attribute selector",
4843
"[#{$attr}=#{$value}]",
@@ -53,19 +48,6 @@ test(
5348
},
5449
);
5550

56-
// The `$` opening the name must not be mistaken for the suffix match operator,
57-
// and the `$=` that follows must still parse as one.
58-
test("sass variable name with a suffix match operator", '[$attr$="x"]', (t, tree) => {
59-
t.deepEqual(tree.nodes[0].nodes[0].attribute, "$attr");
60-
t.deepEqual(tree.nodes[0].nodes[0].operator, "$=");
61-
t.deepEqual(tree.nodes[0].nodes[0].value, "x");
62-
});
63-
64-
test("namespaced sass variable attribute name", "[ns|$attr]", (t, tree) => {
65-
t.deepEqual(tree.nodes[0].nodes[0].namespace, "ns");
66-
t.deepEqual(tree.nodes[0].nodes[0].attribute, "$attr");
67-
});
68-
6951
test("placeholder", "%foo", (t, tree) => {
7052
t.deepEqual(tree.nodes[0].nodes[0].type, "tag");
7153
t.deepEqual(tree.nodes[0].nodes[0].value, "%foo");

‎src/parser.js‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ function indexesOf(array, item) {
105105
return indexes;
106106
}
107107

108+
// A preprocessor interpolation (`#{...}` in SCSS, `@{...}` in Less) is open
109+
// when an opener has been consumed and its closing brace has not.
110+
function insideInterpolation(text) {
111+
if (!text) {
112+
return false;
113+
}
114+
const open = text.lastIndexOf("{");
115+
if (open < 1 || text.indexOf("}", open) !== -1) {
116+
return false;
117+
}
118+
const prefix = text[open - 1];
119+
return prefix === "#" || prefix === "@";
120+
}
121+
108122
function uniqs() {
109123
const list = Array.prototype.concat.apply([], arguments);
110124

@@ -255,12 +269,13 @@ export default class Parser {
255269
break;
256270
}
257271
if (
258-
(lastAdded === "attribute" || !node.attribute) &&
272+
lastAdded === "attribute" &&
273+
insideInterpolation(node.attribute) &&
259274
!(next && next[TOKEN.TYPE] === tokens.equals)
260275
) {
261-
// A `$` that is not followed by `=` is part of the attribute name
262-
// rather than the suffix match operator. Preprocessors emit these
263-
// through interpolation, e.g. `[#{$var}]` in SCSS.
276+
// A `$` inside an open interpolation in the attribute name, such as
277+
// `[#{$var}]` in SCSS, belongs to the name rather than being the
278+
// suffix match operator.
264279
let oldRawAttribute = getProp(node, "raws", "attribute");
265280
node.attribute = (node.attribute || "") + "$";
266281
if (oldRawAttribute) {

0 commit comments

Comments
 (0)