ember-scoped-css 3.4.0 → 3.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/all.cjs CHANGED
@@ -1,3 +1,3 @@
1
- const require_all = require('./all-DurjGbDW.cjs');
1
+ const require_all = require('./all-B28NLPAG.cjs');
2
2
 
3
3
  exports.scopedCSS = require_all.scopedCSS;
@@ -1,4 +1,4 @@
1
- const require_all = require('./all-DurjGbDW.cjs');
1
+ const require_all = require('./all-B28NLPAG.cjs');
2
2
 
3
3
  //#region src/build/public-exports/babel.js
4
4
  const scopedCSS = require_all.scopedCSS.babel;
@@ -1,4 +1,4 @@
1
- const require_all = require('./all-DurjGbDW.cjs');
1
+ const require_all = require('./all-B28NLPAG.cjs');
2
2
 
3
3
  //#region src/build/public-exports/rollup.js
4
4
  const scopedCSS = require_all.scopedCSS.rollup;
package/dist/cjs/vite.cjs CHANGED
@@ -1,4 +1,4 @@
1
- const require_all = require('./all-DurjGbDW.cjs');
1
+ const require_all = require('./all-B28NLPAG.cjs');
2
2
 
3
3
  //#region src/build/public-exports/vite.js
4
4
  const scopedCSS = require_all.scopedCSS.vite;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ember-scoped-css",
3
- "version": "3.4.0",
3
+ "version": "3.5.1",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "ember-addon"
@@ -75,14 +75,7 @@ function rewriteSelector(sel, postfix) {
75
75
  selectors.walk((selector) => {
76
76
  if (isInsideGlobal(selector)) return;
77
77
 
78
- // We never want to touch psuedo selectors since we and the user doesn't own them.
79
- if (selector.type === 'psuedo') return;
80
-
81
- // :nth-of-type has special syntax where the values passed to nth-of-type()
82
- // must either be exactly "odd", "even", or a simple formula
83
- //
84
- // https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type
85
- if (isNthOfType(selector)) return;
78
+ if (isInAnPlusB(selector)) return;
86
79
 
87
80
  if (selector.type === 'class') {
88
81
  selector.value += '_' + postfix;
@@ -121,10 +114,88 @@ function rewriteSelector(sel, postfix) {
121
114
  return transformed;
122
115
  }
123
116
 
124
- function isNthOfType(node) {
125
- if (!node) return false;
117
+ /**
118
+ * Pseudo-classes whose argument starts with an An+B value ("odd", "even", or a
119
+ * formula such as `2n + 1`).
120
+ *
121
+ * https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors#tree-structural_pseudo-classes
122
+ */
123
+ const AN_PLUS_B_PSEUDOS = new Set([
124
+ ':nth-child',
125
+ ':nth-last-child',
126
+ ':nth-of-type',
127
+ ':nth-last-of-type',
128
+ ':nth-col',
129
+ ':nth-last-col',
130
+ ]);
131
+
132
+ /**
133
+ * An An+B value is not a selector, but postcss-selector-parser has no dedicated
134
+ * node for it and parses it as ordinary tag/combinator nodes (`2n + 1` becomes
135
+ * tag `2n`, combinator `+`, tag `1`). Scoping those emits invalid CSS like
136
+ * `:nth-child(2.postfix)`, which invalidates the whole selector list.
137
+ *
138
+ * `:nth-child()` and `:nth-last-child()` also accept a trailing
139
+ * `of <selector-list>`. That part *is* a selector, so it is scoped as usual --
140
+ * only the An+B value ahead of the `of` keyword is off limits.
141
+ *
142
+ * @param {import('postcss-selector-parser').Node} node
143
+ * @returns {boolean}
144
+ */
145
+ function isInAnPlusB(node) {
146
+ let argument = node;
147
+
148
+ while (argument.parent) {
149
+ const parent = argument.parent;
150
+
151
+ if (
152
+ parent.type === 'pseudo' &&
153
+ AN_PLUS_B_PSEUDOS.has(parent.value.toLowerCase())
154
+ ) {
155
+ return !isInOfSelectorList(node, argument, parent);
156
+ }
157
+
158
+ argument = parent;
159
+ }
160
+
161
+ return false;
162
+ }
163
+
164
+ /**
165
+ * Whether `node` belongs to the `of <selector-list>` part of a positional
166
+ * pseudo-class rather than to its An+B value.
167
+ *
168
+ * The `of` keyword lives in the pseudo-class's first argument, and a comma in
169
+ * the selector list starts another argument -- `:nth-child(2 of .b, .c)` parses
170
+ * as the two arguments `2 of .b` and `.c` -- so everything after the first
171
+ * argument is part of the list.
172
+ *
173
+ * @param {import('postcss-selector-parser').Node} node
174
+ * @param {import('postcss-selector-parser').Container} argument the argument `node` sits in
175
+ * @param {import('postcss-selector-parser').Pseudo} pseudo
176
+ * @returns {boolean}
177
+ */
178
+ function isInOfSelectorList(node, argument, pseudo) {
179
+ const [first] = pseudo.nodes;
180
+
181
+ if (!first) return false;
182
+
183
+ const ofIndex = first.nodes.findIndex(
184
+ (sibling) => sibling.type === 'tag' && sibling.value.toLowerCase() === 'of',
185
+ );
186
+
187
+ if (ofIndex === -1) return false;
188
+ if (argument !== first) return true;
189
+ // The argument container itself sits ahead of everything in it.
190
+ if (node === argument) return false;
191
+
192
+ // `node` may be nested (e.g. `.b` in `:nth-child(2 of .b:hover)`), so compare
193
+ // against whichever ancestor is a direct child of the argument.
194
+ let top = node;
195
+
196
+ while (top.parent !== argument) top = top.parent;
126
197
 
127
- return node.parent?.value === ':nth-of-type' || isNthOfType(node.parent);
198
+ return first.nodes.indexOf(top) > ofIndex;
128
199
  }
129
200
 
130
201
  function isInsideKeyframes(node) {
@@ -45,6 +45,90 @@ it(`understands nth-of-type syntax`, function () {
45
45
  `);
46
46
  });
47
47
 
48
+ it(`understands nth-child syntax`, function () {
49
+ const css = `
50
+ li:nth-child(2) {}
51
+ li:nth-child(odd) {}
52
+ li:nth-child(2n + 1) {}
53
+ li:nth-child(-n + 3) {}
54
+ `;
55
+
56
+ const postfix = 'postfix';
57
+ const fileName = 'foo.css';
58
+ const rewritten = rewriteCss(css, postfix, fileName);
59
+
60
+ expect(rewritten).toMatchInlineSnapshot(`
61
+ "/* foo.css */
62
+
63
+ li.postfix:nth-child(2) {}
64
+ li.postfix:nth-child(odd) {}
65
+ li.postfix:nth-child(2n + 1) {}
66
+ li.postfix:nth-child(-n + 3) {}
67
+ "
68
+ `);
69
+ });
70
+
71
+ it(`understands nth-last-child syntax`, function () {
72
+ const css = `
73
+ li:nth-last-child(2) {}
74
+ li:nth-last-child(odd) {}
75
+ `;
76
+
77
+ const postfix = 'postfix';
78
+ const fileName = 'foo.css';
79
+ const rewritten = rewriteCss(css, postfix, fileName);
80
+
81
+ expect(rewritten).toMatchInlineSnapshot(`
82
+ "/* foo.css */
83
+
84
+ li.postfix:nth-last-child(2) {}
85
+ li.postfix:nth-last-child(odd) {}
86
+ "
87
+ `);
88
+ });
89
+
90
+ it(`understands nth-last-of-type syntax`, function () {
91
+ const css = `
92
+ li:nth-last-of-type(2) {}
93
+ li:nth-last-of-type(even) {}
94
+ `;
95
+
96
+ const postfix = 'postfix';
97
+ const fileName = 'foo.css';
98
+ const rewritten = rewriteCss(css, postfix, fileName);
99
+
100
+ expect(rewritten).toMatchInlineSnapshot(`
101
+ "/* foo.css */
102
+
103
+ li.postfix:nth-last-of-type(2) {}
104
+ li.postfix:nth-last-of-type(even) {}
105
+ "
106
+ `);
107
+ });
108
+
109
+ it(`scopes the selector list in nth-child's "of S" argument`, function () {
110
+ const css = `
111
+ li:nth-child(2 of .highlighted) {}
112
+ li:nth-last-child(odd of li) {}
113
+ li:nth-child(2 of .highlighted, .selected) {}
114
+ li:nth-child(2 of .highlighted:hover) {}
115
+ `;
116
+
117
+ const postfix = 'postfix';
118
+ const fileName = 'foo.css';
119
+ const rewritten = rewriteCss(css, postfix, fileName);
120
+
121
+ expect(rewritten).toMatchInlineSnapshot(`
122
+ "/* foo.css */
123
+
124
+ li.postfix:nth-child(2 of .highlighted_postfix) {}
125
+ li.postfix:nth-last-child(odd of li.postfix) {}
126
+ li.postfix:nth-child(2 of .highlighted_postfix, .selected_postfix) {}
127
+ li.postfix:nth-child(2 of .highlighted_postfix:hover) {}
128
+ "
129
+ `);
130
+ });
131
+
48
132
  describe('@container', () => {
49
133
  it('works', () => {
50
134
  const css = `
@@ -154,3 +154,111 @@ describe('concat in a class attribute', () => {
154
154
  ).to.equal('<div class={{if x (concat "a_pfx" " " "b_pfx") "c"}}></div>');
155
155
  });
156
156
  });
157
+
158
+ /**
159
+ * A `concat` nested inside an `if`/`unless` branch survives as a call (the
160
+ * branch has to stay one value), so a param fused onto a nested `if`/`unless`
161
+ * -- "a" and (if x "-on" "-off") share no boundary -- is never a whole class
162
+ * name on its own; the boundary-aware rename above has nothing to postfix.
163
+ * But every class the branch could actually produce is still known ahead of
164
+ * time, so enumerating them lets the fused run collapse into an `if`/`unless`
165
+ * over the renamed classes instead, with nothing left for `concat` to join.
166
+ */
167
+ describe('a concat nested in an if/unless branch, fused onto a nested condition', () => {
168
+ const classesWithVariants = new Set(['a', 'b', 'a-on', 'a-off']);
169
+ const rewriteWithVariants = (hbs) =>
170
+ rewriteHbs(hbs, classesWithVariants, new Set(), postfix);
171
+
172
+ it('distributes over a nested if, replacing the concat with an if over the renamed classes', () => {
173
+ expect(
174
+ rewriteWithVariants(
175
+ '<div class={{if outer (concat "a" (if x "-on" "-off")) "global-thing"}}></div>',
176
+ ),
177
+ ).to.equal(
178
+ '<div class={{if outer (if x "a-on_pfx" "a-off_pfx") "global-thing"}}></div>',
179
+ );
180
+ });
181
+
182
+ it('distributes over a nested unless, inverting the branches it reaches through', () => {
183
+ // unless resolves to its second param when falsy and its third when
184
+ // truthy -- the opposite of if -- so the if built in its place has to
185
+ // reach each renamed class through the opposite branch.
186
+ expect(
187
+ rewriteWithVariants(
188
+ '<div class={{if outer (concat "a" (unless x "-on" "-off")) "global-thing"}}></div>',
189
+ ),
190
+ ).to.equal(
191
+ '<div class={{if outer (if x "a-off_pfx" "a-on_pfx") "global-thing"}}></div>',
192
+ );
193
+ });
194
+
195
+ it('leaves the concat as a call when a fused param is a genuine runtime value', () => {
196
+ // this.suffix has no statically-known value to enumerate, so the classes
197
+ // the concat could produce aren't knowable ahead of time.
198
+ expect(
199
+ rewriteWithVariants(
200
+ '<div class={{if outer (concat "a" this.suffix) "global-thing"}}></div>',
201
+ ),
202
+ ).to.equal(
203
+ '<div class={{if outer (concat "a" this.suffix) "global-thing"}}></div>',
204
+ );
205
+ });
206
+
207
+ it('leaves the concat as a call when a fused param is an opaque helper call', () => {
208
+ expect(
209
+ rewriteWithVariants(
210
+ '<div class={{if outer (concat "a" (someHelper "z")) "global-thing"}}></div>',
211
+ ),
212
+ ).to.equal(
213
+ '<div class={{if outer (concat "a" (someHelper "z")) "global-thing"}}></div>',
214
+ );
215
+ });
216
+
217
+ it('distributes over two independent conditions', () => {
218
+ const classesWithBothVariants = new Set(['a-on', 'a-off', 'b-on', 'b-off']);
219
+
220
+ expect(
221
+ rewriteHbs(
222
+ '<div class={{if outer (concat (if p "a" "b") (if q "-on" "-off")) "global-thing"}}></div>',
223
+ classesWithBothVariants,
224
+ new Set(),
225
+ postfix,
226
+ ),
227
+ ).to.equal(
228
+ '<div class={{if outer (if p (if q "a-on_pfx" "a-off_pfx") (if q "b-on_pfx" "b-off_pfx")) "global-thing"}}></div>',
229
+ );
230
+ });
231
+
232
+ it('leaves the concat as a call once a third independent condition would be needed', () => {
233
+ // Every independent condition doubles the leaves a full enumeration has
234
+ // to cover; the budget caps how many may combine so a long run of fused
235
+ // conditions doesn't blow up the rewrite.
236
+ expect(
237
+ rewriteWithVariants(
238
+ '<div class={{if outer (concat (if p "a" "b") "-" (if q "x" "y") "-" (if r "1" "2")) "global-thing"}}></div>',
239
+ ),
240
+ ).to.equal(
241
+ '<div class={{if outer (concat (if p "a" "b") "-" (if q "x" "y") "-" (if r "1" "2")) "global-thing"}}></div>',
242
+ );
243
+ });
244
+
245
+ it('leaves the concat as a call when none of the classes it could produce are in the CSS', () => {
246
+ expect(
247
+ rewrite(
248
+ '<div class={{if outer (concat "z" (if x "-on" "-off")) "global-thing"}}></div>',
249
+ ),
250
+ ).to.equal(
251
+ '<div class={{if outer (concat "z" (if x "-on" "-off")) "global-thing"}}></div>',
252
+ );
253
+ });
254
+
255
+ it('skips a shadowed concat, leaving it and its nested if alone', () => {
256
+ expect(
257
+ rewriteWithVariants(
258
+ '{{#let x as |concat|}}<div class={{if outer (concat "a" (if x "-on" "-off")) "global-thing"}}></div>{{/let}}',
259
+ ),
260
+ ).to.equal(
261
+ '{{#let x as |concat|}}<div class={{if outer (concat "a" (if x "-on" "-off")) "global-thing"}}></div>{{/let}}',
262
+ );
263
+ });
264
+ });
@@ -93,6 +93,65 @@ function isWholeClassName(params, index) {
93
93
  return boundedLeft && boundedRight;
94
94
  }
95
95
 
96
+ /**
97
+ * A collapsed `concat`/`if`/`unless` run becomes a StringLiteral or a
98
+ * SubExpression (`(if ...)`), which are the right shape for a helper param
99
+ * but not for an attribute value -- there they need to be a TextNode or
100
+ * MustacheStatement instead.
101
+ *
102
+ * "a-suffix" -> the TextNode a-suffix
103
+ * (if C "a" "b") -> {{if C "a" "b"}}
104
+ */
105
+ function toAttributeValue(node) {
106
+ if (node.type === 'StringLiteral') return recast.builders.text(node.value);
107
+
108
+ if (node.type === 'SubExpression') {
109
+ return recast.builders.mustache(node.path, node.params, node.hash);
110
+ }
111
+
112
+ return node;
113
+ }
114
+
115
+ /**
116
+ * How many independent `if`/`unless` conditions may be combined when
117
+ * expanding a concat run into a decision tree. Each one roughly doubles the
118
+ * leaves, so this bounds the blowup rather than the leaf count directly.
119
+ */
120
+ const MAX_CONDITION_DEPTH = 2;
121
+
122
+ const EMPTY_VALUE = { kind: 'literal', value: '' };
123
+
124
+ /**
125
+ * Concatenate two statically-known values. Two literals just join; a literal
126
+ * against a choice distributes into both of the choice's branches, and two
127
+ * choices nest, doubling the leaves.
128
+ *
129
+ * "a" + "b" -> "ab"
130
+ * "a" + (C ? "x" : "y") -> C ? "ax" : "ay"
131
+ * (C ? "x" : "y") + (D ? "1" : "2") -> C ? (D ? "x1" : "x2") : (D ? "y1" : "y2")
132
+ */
133
+ function concatValueExprs(left, right) {
134
+ if (left.kind === 'literal' && right.kind === 'literal') {
135
+ return { kind: 'literal', value: left.value + right.value };
136
+ }
137
+
138
+ if (left.kind === 'choice') {
139
+ return {
140
+ kind: 'choice',
141
+ condition: left.condition,
142
+ whenTrue: concatValueExprs(left.whenTrue, right),
143
+ whenFalse: concatValueExprs(left.whenFalse, right),
144
+ };
145
+ }
146
+
147
+ return {
148
+ kind: 'choice',
149
+ condition: right.condition,
150
+ whenTrue: concatValueExprs(left, right.whenTrue),
151
+ whenFalse: concatValueExprs(left, right.whenFalse),
152
+ };
153
+ }
154
+
96
155
  export function templatePlugin({ classes, tags, attributes, postfix }) {
97
156
  let stack = [];
98
157
  // scoped-class is a global we allow in hbs
@@ -131,17 +190,152 @@ export function templatePlugin({ classes, tags, attributes, postfix }) {
131
190
  return CLASS_BUILDING_HELPERS.has(name) && !isShadowed(name);
132
191
  }
133
192
 
193
+ /**
194
+ * The statically-known values a class-building expression can produce,
195
+ * shaped as the decision tree of the `if`/`unless` conditions involved.
196
+ * `null` means the value isn't pinned down at build time -- either it's a
197
+ * genuine runtime value, or pinning it down would need more conditions
198
+ * than `budget` allows.
199
+ *
200
+ * "a" -> { kind: 'literal', value: 'a' }
201
+ * (if C "a" "b") -> { kind: 'choice', condition: C, whenTrue: ..., whenFalse: ... }
202
+ * (if C "a") -> the same, with whenFalse the empty string
203
+ * this.x -> null, an arbitrary runtime value
204
+ * (someHelper "a") -> null, its contract is opaque
205
+ *
206
+ * `budget.remaining` is shared across one whole enumeration attempt (it is
207
+ * not reset per branch), since sibling conditions multiply leaves exactly
208
+ * as nested ones do.
209
+ */
210
+ function toValueExpr(node, budget) {
211
+ if (node.type === 'StringLiteral') {
212
+ return { kind: 'literal', value: node.value };
213
+ }
214
+
215
+ if (node.type !== 'MustacheStatement' && node.type !== 'SubExpression') {
216
+ return null;
217
+ }
218
+
219
+ const name = getValue(node.path);
220
+
221
+ if (!isClassBuilding(name)) return null;
222
+
223
+ if (name === 'concat') {
224
+ let combined = EMPTY_VALUE;
225
+
226
+ for (const param of node.params ?? []) {
227
+ const next = toValueExpr(param, budget);
228
+
229
+ if (!next) return null;
230
+
231
+ combined = concatValueExprs(combined, next);
232
+ }
233
+
234
+ return combined;
235
+ }
236
+
237
+ if (budget.remaining <= 0) return null;
238
+
239
+ budget.remaining -= 1;
240
+
241
+ const [condition, thenBranch, elseBranch] = node.params ?? [];
242
+ const thenExpr = thenBranch ? toValueExpr(thenBranch, budget) : EMPTY_VALUE;
243
+ const elseExpr = elseBranch ? toValueExpr(elseBranch, budget) : EMPTY_VALUE;
244
+
245
+ if (!thenExpr || !elseExpr) return null;
246
+
247
+ return name === 'unless'
248
+ ? { kind: 'choice', condition, whenTrue: elseExpr, whenFalse: thenExpr }
249
+ : { kind: 'choice', condition, whenTrue: thenExpr, whenFalse: elseExpr };
250
+ }
251
+
252
+ /**
253
+ * Rename every leaf of a value tree in one pass, reporting alongside it
254
+ * whether any leaf's value actually changed -- the tree is rebuilt either
255
+ * way, so this is the only point that can tell without walking it twice.
256
+ */
257
+ function renameValueExpr(expr) {
258
+ if (expr.kind === 'literal') {
259
+ const value = renameClass(expr.value, postfix, classes);
260
+
261
+ return {
262
+ expr: { kind: 'literal', value },
263
+ changed: value !== expr.value,
264
+ };
265
+ }
266
+
267
+ const whenTrue = renameValueExpr(expr.whenTrue);
268
+ const whenFalse = renameValueExpr(expr.whenFalse);
269
+
270
+ return {
271
+ expr: {
272
+ kind: 'choice',
273
+ condition: expr.condition,
274
+ whenTrue: whenTrue.expr,
275
+ whenFalse: whenFalse.expr,
276
+ },
277
+ changed: whenTrue.changed || whenFalse.changed,
278
+ };
279
+ }
280
+
281
+ /**
282
+ * Turn a value tree into the AST it describes -- a literal for a plain
283
+ * leaf, or nested `if`s reproducing the conditions. A condition reused
284
+ * across sibling branches (two independent `if`s in one concat) appears
285
+ * more than once in the output, exactly as it would if written by hand.
286
+ */
287
+ function buildValueExprNode(expr) {
288
+ if (expr.kind === 'literal') {
289
+ return recast.builders.literal('StringLiteral', expr.value);
290
+ }
291
+
292
+ return recast.builders.sexpr(recast.builders.path('if'), [
293
+ expr.condition,
294
+ buildValueExprNode(expr.whenTrue),
295
+ buildValueExprNode(expr.whenFalse),
296
+ ]);
297
+ }
298
+
299
+ /**
300
+ * `concat` fuses its params into one string, so a literal is only a class
301
+ * name of its own when a boundary separates it from its neighbours (see
302
+ * isWholeClassName). When every param's value is statically known and they
303
+ * all fuse, the call produces one of a fixed set of class names, so
304
+ * `concat` has nothing left to join -- the caller replaces the whole call
305
+ * with the literal (no conditions involved) or the `if` expression (one
306
+ * per condition) that reaches each renamed leaf.
307
+ *
308
+ * {{concat "a" " " "b"}} -> two classes, rename each, concat stays
309
+ * {{concat "a" "-suffix"}} -> one class; the call is replaced by it
310
+ * {{concat "a" (if C "-on" "-off")}} -> replaced by {{if C "a-on_pfx" "a-off_pfx"}}
311
+ * {{concat "a" this.x}} -> "a" fuses with an unknown value, so leave it
312
+ *
313
+ * Returns the replacement when the call collapses; otherwise renames
314
+ * whole-class-name params in place and returns nothing.
315
+ */
134
316
  function renameConcatParams(node) {
135
317
  const params = node.params ?? [];
318
+ const fuses = params.some(
319
+ (param, index) => !isWholeClassName(params, index),
320
+ );
321
+
322
+ if (fuses) {
323
+ const expr = toValueExpr(node, { remaining: MAX_CONDITION_DEPTH });
324
+ const renamed = expr && renameValueExpr(expr);
325
+
326
+ // Collapsing is only worth the churn if it renamed something -- and
327
+ // only possible at all if every leaf's value was statically known.
328
+ if (renamed?.changed) {
329
+ return buildValueExprNode(renamed.expr);
330
+ }
331
+
332
+ return;
333
+ }
136
334
 
137
335
  for (const [index, param] of params.entries()) {
138
336
  if (!isWholeClassName(params, index)) continue;
139
337
 
140
- if (param.type === 'StringLiteral') {
141
- param.value = renameClass(param.value, postfix, classes);
142
- } else {
143
- renameLiteralClasses(param);
144
- }
338
+ params[index] = renameLiteralClasses(param);
145
339
  }
146
340
  }
147
341
 
@@ -223,13 +417,17 @@ export function templatePlugin({ classes, tags, attributes, postfix }) {
223
417
  }
224
418
 
225
419
  /**
226
- * Rename the string literals whose value reaches the class attribute.
420
+ * Rename the string literals whose value reaches the class attribute, and
421
+ * return the node that should stand in this one's place. That's the same
422
+ * node for everything but a `concat` call that collapses to a single
423
+ * literal or `if` expression -- see renameConcatParams.
227
424
  *
228
425
  * {{if x "a" "b"}} -> both branches
229
426
  * {{if (checkAlphabet "a" "b") "a" "b"}} -> the branches, not the condition
230
427
  * {{if x (concat "a" " " "b") "c"}} -> concat's own whole-class-name params too
231
428
  * {{if x (someHelper "a") "b"}} -> someHelper is opaque, left alone
232
429
  * {{concat "a" " " "b"}} -> every param that's a whole class name
430
+ * {{concat "a" "-suffix"}} -> replaced by the renamed literal
233
431
  * {{this.fooClass}} -> resolved at runtime, nothing to rename
234
432
  *
235
433
  * The shadowing checked at each depth is the one in effect at the element
@@ -238,23 +436,30 @@ export function templatePlugin({ classes, tags, attributes, postfix }) {
238
436
  * rebound part-way down a class attribute's value.
239
437
  */
240
438
  function renameLiteralClasses(node) {
439
+ if (node.type === 'StringLiteral') {
440
+ node.value = renameClass(node.value, postfix, classes);
441
+
442
+ return node;
443
+ }
444
+
241
445
  const name = getValue(node.path);
242
446
 
243
- if (!isClassBuilding(name)) return;
447
+ if (!isClassBuilding(name)) return node;
244
448
 
245
449
  if (name === 'concat') {
246
- renameConcatParams(node);
247
-
248
- return;
450
+ return renameConcatParams(node) ?? node;
249
451
  }
250
452
 
251
- for (let param of node.params.slice(1)) {
252
- if (param.type === 'StringLiteral') {
253
- param.value = renameClass(param.value, postfix, classes);
254
- } else {
255
- renameLiteralClasses(param);
256
- }
453
+ for (const [index, param] of (node.params ?? []).entries()) {
454
+ // `name` is 'if' or 'unless' here ('concat' returned above), so
455
+ // params[0] is the condition itself -- e.g. `x` in `(if x "a" "b")` --
456
+ // not one of the branches, so it's never a class name to rename.
457
+ if (index === 0) continue;
458
+
459
+ node.params[index] = renameLiteralClasses(param);
257
460
  }
461
+
462
+ return node;
258
463
  }
259
464
 
260
465
  return {
@@ -278,20 +483,26 @@ export function templatePlugin({ classes, tags, attributes, postfix }) {
278
483
 
279
484
  node.value.chars = renamedClass;
280
485
  } else if (node.value.type === 'ConcatStatement') {
281
- for (let part of node.value.parts) {
486
+ for (const [index, part] of node.value.parts.entries()) {
282
487
  if (part.type === 'TextNode' && part.chars) {
283
488
  const renamedClass = renameClass(part.chars, postfix, classes);
284
489
 
285
490
  part.chars = renamedClass;
286
491
  } else if (part.type === 'MustacheStatement') {
287
- renameLiteralClasses(part);
492
+ node.value.parts[index] = toAttributeValue(
493
+ renameLiteralClasses(part),
494
+ );
288
495
  }
289
496
  }
290
497
  } else if (node.value.type === 'MustacheStatement') {
291
498
  // Glimmer parses a quoted attribute value as a ConcatStatement and
292
499
  // an unquoted one as a bare MustacheStatement, so `class={{if x
293
500
  // "a" "b"}}` lands here rather than in the branch above.
294
- renameLiteralClasses(node.value);
501
+ const replaced = renameLiteralClasses(node.value);
502
+
503
+ if (replaced.type === 'StringLiteral') node.quoteType = '"';
504
+
505
+ node.value = toAttributeValue(replaced);
295
506
  }
296
507
  }
297
508
  },