ember-scoped-css 3.3.0 → 3.5.0

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.
@@ -30,6 +30,128 @@ function elementHasScopedAttribute(node, attributes) {
30
30
  );
31
31
  }
32
32
 
33
+ /**
34
+ * Helpers whose result is assembled from their own params, so a literal
35
+ * param can be a class name in its own right. The condition at index 0 of
36
+ * `if` and `unless` is excluded -- it decides which branch wins rather than
37
+ * contributing to the class string, so postfixing it (e.g. a comparand:
38
+ * `{{if (eq this.mode "a") "a" "b"}}`) would stop the comparison ever
39
+ * matching. `concat` has no such condition, but fuses its params together --
40
+ * see isWholeClassName.
41
+ *
42
+ * Any other helper is opaque: it may return a class name, but what it does
43
+ * with its arguments is unknown, so those arguments are left alone.
44
+ * `{{scopedClass "..."}}` is the way to rename a literal such a helper
45
+ * receives.
46
+ *
47
+ * Matching is by name, so a block param that shadows one of these is checked
48
+ * for -- see isShadowed.
49
+ */
50
+ const CLASS_BUILDING_HELPERS = new Set(['if', 'unless', 'concat']);
51
+
52
+ function endsAtClassBoundary(value) {
53
+ return /\s$/.test(value);
54
+ }
55
+
56
+ function startsAtClassBoundary(value) {
57
+ return /^\s/.test(value);
58
+ }
59
+
60
+ /**
61
+ * Whether the param at `index` sits in a whole-class-name position rather
62
+ * than fused onto a neighbour -- true whether that param is itself a literal
63
+ * or a class-building call, since a call is renamed by looking inside it
64
+ * (see renameConcatParams), not by postfixing its result. Postfixing a
65
+ * fused fragment would bury the postfix in the middle of the class the
66
+ * fragments build. A neighbour whose value is only known at runtime bounds
67
+ * nothing of its own, since it could begin or end with anything.
68
+ *
69
+ * {{concat "a" " " "b"}} -> "a" and "b", the space separates them
70
+ * {{concat "a" "-suffix"}} -> neither, the result is the one class a-suffix
71
+ * {{concat "a" this.x}} -> not "a", it fuses with an unknown value
72
+ * {{concat "a " this.x}} -> "a ", its own trailing space ends it
73
+ */
74
+ function isWholeClassName(params, index) {
75
+ // Only a StringLiteral has a value of its own to test for a boundary; a
76
+ // call or a path contributes no boundary and is bounded by its neighbours
77
+ // alone.
78
+ const param = params[index];
79
+ const value = param.type === 'StringLiteral' ? param.value : '';
80
+ const previous = params[index - 1];
81
+ const next = params[index + 1];
82
+
83
+ const boundedLeft =
84
+ index === 0 ||
85
+ startsAtClassBoundary(value) ||
86
+ (previous.type === 'StringLiteral' && endsAtClassBoundary(previous.value));
87
+
88
+ const boundedRight =
89
+ index === params.length - 1 ||
90
+ endsAtClassBoundary(value) ||
91
+ (next.type === 'StringLiteral' && startsAtClassBoundary(next.value));
92
+
93
+ return boundedLeft && boundedRight;
94
+ }
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
+
33
155
  export function templatePlugin({ classes, tags, attributes, postfix }) {
34
156
  let stack = [];
35
157
  // scoped-class is a global we allow in hbs
@@ -42,67 +164,395 @@ export function templatePlugin({ classes, tags, attributes, postfix }) {
42
164
  return scopedClassCandidates.some((candidate) => candidate === str);
43
165
  }
44
166
 
167
+ /**
168
+ * Stack of ElementNode ancestors that introduce a block param, e.g.
169
+ * `<Foo as |if|>` -- pushed in ElementNode's own `enter` and popped in its
170
+ * own `exit` below. A `{{#each xs as |if|}}` block has no dedicated
171
+ * visitor, so it reaches `stack` through the generic `All` visitor
172
+ * instead; ElementNode has its own visitor, so `All` never runs for it,
173
+ * and this tracks it separately.
174
+ */
175
+ let elementScope = [];
176
+
177
+ /**
178
+ * Whether `name` refers to the helper it appears to. A block param wins
179
+ * over both a helper and a keyword, so `{{#each xs as |if|}}` puts
180
+ * something else entirely behind the name for the length of the block.
181
+ */
182
+ function isShadowed(name) {
183
+ return (
184
+ stack.some((ancestor) => ancestor.blockParams?.includes(name)) ||
185
+ elementScope.some((element) => element.blockParams.includes(name))
186
+ );
187
+ }
188
+
189
+ function isClassBuilding(name) {
190
+ return CLASS_BUILDING_HELPERS.has(name) && !isShadowed(name);
191
+ }
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
+ */
316
+ function renameConcatParams(node) {
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
+ }
334
+
335
+ for (const [index, param] of params.entries()) {
336
+ if (!isWholeClassName(params, index)) continue;
337
+
338
+ params[index] = renameLiteralClasses(param);
339
+ }
340
+ }
341
+
342
+ /**
343
+ * Whether `node` is a `concat` call sitting in the class attribute's value
344
+ * itself, where the attribute joins the call's result into its own string.
345
+ * A `concat` nested any deeper -- an `if` branch, another `concat`'s
346
+ * params -- has to stay one value, so it keeps its call.
347
+ */
348
+ function isAttributeLevelConcat(node) {
349
+ return (
350
+ node.type === 'MustacheStatement' &&
351
+ getValue(node.path) === 'concat' &&
352
+ isClassBuilding('concat')
353
+ );
354
+ }
355
+
356
+ /**
357
+ * `concat(concat(x, y), z)` joins to the same string as `concat(x, y, z)`
358
+ * -- string concatenation doesn't care how its inputs were grouped -- so a
359
+ * `concat` param that is itself an unshadowed `concat` call contributes
360
+ * its own params in its place rather than surviving as a nested call.
361
+ */
362
+ function flattenConcatParams(params) {
363
+ return params.flatMap((param) =>
364
+ param.type === 'SubExpression' &&
365
+ getValue(param.path) === 'concat' &&
366
+ isClassBuilding('concat')
367
+ ? flattenConcatParams(param.params ?? [])
368
+ : [param],
369
+ );
370
+ }
371
+
372
+ /**
373
+ * `concat`'s own job -- joining its params into one string -- is exactly
374
+ * what an attribute value already does natively: adjacent literal params
375
+ * join into one `TextNode`, and each other param becomes its own part. So
376
+ * a `concat` the attribute would join anyway can be spliced into the parts
377
+ * it's equivalent to, leaving the existing per-part handling (below) to
378
+ * treat it as it would a hand-written attribute string.
379
+ *
380
+ * {{concat x y z}} -> {{x}}{{y}}{{z}}
381
+ * {{concat (if x y z) " " (if a b c)}} -> {{if x y z}} {{if a b c}}
382
+ * {{concat "a" "-suffix"}} -> the single TextNode a-suffix
383
+ * {{concat (concat "a" " " "b") "c"}} -> {{concat "a" " " "b"}}'s own
384
+ * params joined in directly, not
385
+ * wrapped as a surviving call
386
+ */
387
+ function concatParts(node) {
388
+ const parts = [];
389
+ let pendingText = '';
390
+
391
+ for (const param of flattenConcatParams(node.params ?? [])) {
392
+ if (param.type === 'StringLiteral') {
393
+ pendingText += param.value;
394
+
395
+ continue;
396
+ }
397
+
398
+ if (pendingText) {
399
+ parts.push(recast.builders.text(pendingText));
400
+ pendingText = '';
401
+ }
402
+
403
+ parts.push(
404
+ param.type === 'SubExpression'
405
+ ? recast.builders.mustache(param.path, param.params, param.hash)
406
+ : recast.builders.mustache(param),
407
+ );
408
+ }
409
+
410
+ // A ConcatStatement needs at least one part, and joining no params -- or
411
+ // only empty ones -- still gives the empty string to carry.
412
+ if (pendingText || parts.length === 0) {
413
+ parts.push(recast.builders.text(pendingText));
414
+ }
415
+
416
+ return parts;
417
+ }
418
+
419
+ /**
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.
424
+ *
425
+ * {{if x "a" "b"}} -> both branches
426
+ * {{if (checkAlphabet "a" "b") "a" "b"}} -> the branches, not the condition
427
+ * {{if x (concat "a" " " "b") "c"}} -> concat's own whole-class-name params too
428
+ * {{if x (someHelper "a") "b"}} -> someHelper is opaque, left alone
429
+ * {{concat "a" " " "b"}} -> every param that's a whole class name
430
+ * {{concat "a" "-suffix"}} -> replaced by the renamed literal
431
+ * {{this.fooClass}} -> resolved at runtime, nothing to rename
432
+ *
433
+ * The shadowing checked at each depth is the one in effect at the element
434
+ * carrying the attribute: only a block or an element can introduce a block
435
+ * param, and neither can appear inside a subexpression, so no name can be
436
+ * rebound part-way down a class attribute's value.
437
+ */
438
+ function renameLiteralClasses(node) {
439
+ if (node.type === 'StringLiteral') {
440
+ node.value = renameClass(node.value, postfix, classes);
441
+
442
+ return node;
443
+ }
444
+
445
+ const name = getValue(node.path);
446
+
447
+ if (!isClassBuilding(name)) return node;
448
+
449
+ if (name === 'concat') {
450
+ return renameConcatParams(node) ?? node;
451
+ }
452
+
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);
460
+ }
461
+
462
+ return node;
463
+ }
464
+
45
465
  return {
46
466
  AttrNode(node) {
47
467
  if (node.name === 'class') {
468
+ if (isAttributeLevelConcat(node.value)) {
469
+ node.value = recast.builders.concat(concatParts(node.value));
470
+ node.quoteType = '"';
471
+ } else if (node.value.type === 'ConcatStatement') {
472
+ // Quoting always produces a ConcatStatement, even for a value
473
+ // that's nothing but a single concat call (`class="{{concat
474
+ // ...}}"`), so the concat to splice away can be any one of the
475
+ // parts rather than the whole value.
476
+ node.value.parts = node.value.parts.flatMap((part) =>
477
+ isAttributeLevelConcat(part) ? concatParts(part) : part,
478
+ );
479
+ }
480
+
48
481
  if (node.value.type === 'TextNode' && node.value.chars) {
49
482
  const renamedClass = renameClass(node.value.chars, postfix, classes);
50
483
 
51
484
  node.value.chars = renamedClass;
52
485
  } else if (node.value.type === 'ConcatStatement') {
53
- for (let part of node.value.parts) {
486
+ for (const [index, part] of node.value.parts.entries()) {
54
487
  if (part.type === 'TextNode' && part.chars) {
55
488
  const renamedClass = renameClass(part.chars, postfix, classes);
56
489
 
57
490
  part.chars = renamedClass;
58
491
  } else if (part.type === 'MustacheStatement') {
59
- recast.traverse(part, {
60
- StringLiteral(node) {
61
- const renamedClass = renameClass(
62
- node.value,
63
- postfix,
64
- classes,
65
- );
66
-
67
- node.value = renamedClass;
68
- },
69
- });
492
+ node.value.parts[index] = toAttributeValue(
493
+ renameLiteralClasses(part),
494
+ );
70
495
  }
71
496
  }
497
+ } else if (node.value.type === 'MustacheStatement') {
498
+ // Glimmer parses a quoted attribute value as a ConcatStatement and
499
+ // an unquoted one as a bare MustacheStatement, so `class={{if x
500
+ // "a" "b"}}` lands here rather than in the branch above.
501
+ const replaced = renameLiteralClasses(node.value);
502
+
503
+ if (replaced.type === 'StringLiteral') node.quoteType = '"';
504
+
505
+ node.value = toAttributeValue(replaced);
72
506
  }
73
507
  }
74
508
  },
75
509
 
76
- ElementNode(node) {
77
- // An element is in scope if its tag matches a tag selector, or if it
78
- // carries an attribute named in a scoped attribute selector. We add the
79
- // postfix class at most once regardless of how many things matched.
80
- const shouldScope =
81
- tags.has(node.tag) || elementHasScopedAttribute(node, attributes);
510
+ ElementNode: {
511
+ enter(node) {
512
+ // Only elements that introduce a name need tracking, which also
513
+ // keeps the stack balanced when a visitor removes a node and skips
514
+ // its exit.
515
+ if (node.blockParams?.length) elementScope.push(node);
516
+ },
82
517
 
83
- if (!shouldScope) return;
518
+ // Appending runs on the way out so that AttrNode has already seen the
519
+ // class attribute in its authored shape. Wrapping `class={{if ...}}` in
520
+ // a concat first would hand AttrNode a ConcatStatement, whose every
521
+ // string literal is a class name by assumption -- including an `if`
522
+ // condition's, which must not be renamed.
523
+ exit(node) {
524
+ if (elementScope.at(-1) === node) elementScope.pop();
84
525
 
85
- // check if class attribute already exists
86
- const classAttr = node.attributes.find((attr) => attr.name === 'class');
526
+ // An element is in scope if its tag matches a tag selector, or if it
527
+ // carries an attribute named in a scoped attribute selector. We add the
528
+ // postfix class at most once regardless of how many things matched.
529
+ const shouldScope =
530
+ tags.has(node.tag) || elementHasScopedAttribute(node, attributes);
87
531
 
88
- if (!classAttr) {
89
- // push class attribute
90
- node.attributes.push(
91
- recast.builders.attr('class', recast.builders.text(postfix)),
92
- );
93
- } else if (classAttr.value.type === 'TextNode') {
94
- classAttr.value.chars += ' ' + postfix;
95
- } else if (classAttr.value.type === 'ConcatStatement') {
96
- // class="foo {{bar}}"
97
- classAttr.value.parts.push(recast.builders.text(' ' + postfix));
98
- } else {
99
- // class={{this.foo}} — wrap in a concat so we can append the text part
100
- classAttr.value = recast.builders.concat([
101
- classAttr.value,
102
- recast.builders.text(' ' + postfix),
103
- ]);
104
- classAttr.quoteType = '"';
105
- }
532
+ if (!shouldScope) return;
533
+
534
+ // check if class attribute already exists
535
+ const classAttr = node.attributes.find((attr) => attr.name === 'class');
536
+
537
+ if (!classAttr) {
538
+ // push class attribute
539
+ node.attributes.push(
540
+ recast.builders.attr('class', recast.builders.text(postfix)),
541
+ );
542
+ } else if (classAttr.value.type === 'TextNode') {
543
+ classAttr.value.chars += ' ' + postfix;
544
+ } else if (classAttr.value.type === 'ConcatStatement') {
545
+ // class="foo {{bar}}"
546
+ classAttr.value.parts.push(recast.builders.text(' ' + postfix));
547
+ } else {
548
+ // class={{this.foo}} — wrap in a concat so we can append the text part
549
+ classAttr.value = recast.builders.concat([
550
+ classAttr.value,
551
+ recast.builders.text(' ' + postfix),
552
+ ]);
553
+ classAttr.quoteType = '"';
554
+ }
555
+ },
106
556
  },
107
557
 
108
558
  All: {
@@ -0,0 +1,19 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import rewriteHbs from './rewriteHbs.js';
4
+
5
+ const postfix = 'pfx';
6
+
7
+ const classes = new Set(['a', 'b']);
8
+
9
+ function rewrite(hbs) {
10
+ return rewriteHbs(hbs, classes, new Set(), postfix);
11
+ }
12
+
13
+ describe('scopedClass in a class attribute', () => {
14
+ it('collapses to a quoted literal unconditionally, without a CSS lookup', () => {
15
+ expect(
16
+ rewrite('<div class={{scopedClass "global-thing"}}></div>'),
17
+ ).to.equal('<div class="global-thing_pfx"></div>');
18
+ });
19
+ });
@@ -0,0 +1,63 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import rewriteHbs from './rewriteHbs.js';
4
+
5
+ const postfix = 'pfx';
6
+
7
+ /**
8
+ * `a` and `b` are in the co-located CSS; `global-thing` deliberately is not, so
9
+ * tests can distinguish "renamed" from "left alone for a global class".
10
+ */
11
+ const classes = new Set(['a', 'b']);
12
+
13
+ function rewrite(hbs) {
14
+ return rewriteHbs(hbs, classes, new Set(), postfix);
15
+ }
16
+
17
+ describe('unless in a class attribute', () => {
18
+ it('renames both branches', () => {
19
+ expect(rewrite('<div class={{unless x "a" "b"}}></div>')).to.equal(
20
+ '<div class={{unless x "a_pfx" "b_pfx"}}></div>',
21
+ );
22
+ });
23
+
24
+ it('leaves a branch that is not in the CSS alone', () => {
25
+ expect(rewrite('<div class={{unless x "global-thing"}}></div>')).to.equal(
26
+ '<div class={{unless x "global-thing"}}></div>',
27
+ );
28
+ });
29
+
30
+ it('leaves the condition alone, even when it is a helper call', () => {
31
+ expect(
32
+ rewrite('<div class={{unless (checkAlphabet "a" "b") "a" "b"}}></div>'),
33
+ ).to.equal(
34
+ '<div class={{unless (checkAlphabet "a" "b") "a_pfx" "b_pfx"}}></div>',
35
+ );
36
+ });
37
+
38
+ it('skips unless when it is an element block param', () => {
39
+ expect(
40
+ rewrite('<Foo as |unless|><div class={{unless y "a" "b"}}></div></Foo>'),
41
+ ).to.equal('<Foo as |unless|><div class={{unless y "a" "b"}}></div></Foo>');
42
+ });
43
+
44
+ it('is shadowed independently of if', () => {
45
+ // if and unless share one set of class-building helper names, and the
46
+ // shadow check keys on the name rather than the set.
47
+ expect(
48
+ rewrite(
49
+ '{{#let q as |if|}}<div class={{if a "a" "b"}}></div><div class={{unless a "a" "b"}}></div>{{/let}}',
50
+ ),
51
+ ).to.equal(
52
+ '{{#let q as |if|}}<div class={{if a "a" "b"}}></div><div class={{unless a "a_pfx" "b_pfx"}}></div>{{/let}}',
53
+ );
54
+ });
55
+
56
+ it('reaches whole-class-name params of a concat branch, same as if', () => {
57
+ expect(
58
+ rewrite('<div class={{unless x (concat "a" " " "b") "c"}}></div>'),
59
+ ).to.equal(
60
+ '<div class={{unless x (concat "a_pfx" " " "b_pfx") "c"}}></div>',
61
+ );
62
+ });
63
+ });