@tsrx/prettier-plugin 0.3.131 → 0.3.132

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/index.test.js +291 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsrx/prettier-plugin",
3
- "version": "0.3.131",
3
+ "version": "0.3.132",
4
4
  "description": "TSRX plugin for Prettier",
5
5
  "type": "module",
6
6
  "module": "src/index.js",
@@ -27,7 +27,7 @@
27
27
  "prettier": "^3.9.6"
28
28
  },
29
29
  "dependencies": {
30
- "@tsrx/core": "0.1.65"
30
+ "@tsrx/core": "0.1.66"
31
31
  },
32
32
  "files": [
33
33
  "src/"
package/src/index.test.js CHANGED
@@ -7562,4 +7562,295 @@ class A {}
7562
7562
  class B {}`);
7563
7563
  });
7564
7564
  });
7565
+
7566
+ describe('scoped <style> blocks with apply', () => {
7567
+ /**
7568
+ * Assert the input is already formatted and comes back byte-identical.
7569
+ * `format` also checks the second pass is a fixpoint, so every case here
7570
+ * doubles as an idempotence check.
7571
+ * @param {string} source
7572
+ */
7573
+ const expectUnchanged = async (source) => {
7574
+ const result = await format(source);
7575
+ expect(result).toBeWithNewline(source);
7576
+ };
7577
+
7578
+ it('formats a body-less <style apply={theme} /> inside a fragment', async () => {
7579
+ const input = `export function App(){return <><style apply={theme} /><div>{"hi"}</div></>}`;
7580
+ const expected = `export function App() {
7581
+ return <>
7582
+ <style apply={theme} />
7583
+ <div>{"hi"}</div>
7584
+ </>;
7585
+ }`;
7586
+ const result = await format(input);
7587
+ expect(result).toBeWithNewline(expected);
7588
+ });
7589
+
7590
+ it('formats a fragment holding <style apply={theme} /> and the output node in a @{} body', async () => {
7591
+ const input = `export function App()@{<><style apply={theme} /><div>{"hi"}</div></>}`;
7592
+ const expected = `export function App() @{
7593
+ <>
7594
+ <style apply={theme} />
7595
+ <div>{"hi"}</div>
7596
+ </>
7597
+ }`;
7598
+ const result = await format(input);
7599
+ expect(result).toBeWithNewline(expected);
7600
+ });
7601
+
7602
+ it('does not expand a body-less block into <style></style>', async () => {
7603
+ // A lone block as the output of a @{} body parses (the analyzer
7604
+ // reports it); the formatter keeps the self-closing form.
7605
+ const result = await format(`export function Only()@{<style apply={theme} />}`);
7606
+ expect(result).toBeWithNewline(`export function Only() @{
7607
+ <style apply={theme} />
7608
+ }`);
7609
+ expect(result).not.toContain('</style>');
7610
+ });
7611
+
7612
+ it('keeps an explicitly empty <style apply={theme}></style> as authored', async () => {
7613
+ await expectUnchanged(`export function App() @{
7614
+ <>
7615
+ <style apply={theme}></style>
7616
+ <div>{"hi"}</div>
7617
+ </>
7618
+ }`);
7619
+ });
7620
+
7621
+ it('formats <style apply={[a, b]}> with a CSS body', async () => {
7622
+ const input = `export function App()@{<><style apply={[a,b]}>div{color:red}</style><div>{"hi"}</div></>}`;
7623
+ const expected = `export function App() @{
7624
+ <>
7625
+ <style apply={[a, b]}>
7626
+ div {
7627
+ color: red;
7628
+ }
7629
+ </style>
7630
+ <div>{"hi"}</div>
7631
+ </>
7632
+ }`;
7633
+ const result = await format(input);
7634
+ expect(result).toBeWithNewline(expected);
7635
+ });
7636
+
7637
+ it('breaks a long apply list across lines like any other attribute', async () => {
7638
+ const input = `export function App()@{<><style apply={[someVeryLongThemeName, anotherVeryLongThemeName, yetAnotherVeryLongThemeName]} /><div>{"hi"}</div></>}`;
7639
+ const expected = `export function App() @{
7640
+ <>
7641
+ <style
7642
+ apply={[
7643
+ someVeryLongThemeName,
7644
+ anotherVeryLongThemeName,
7645
+ yetAnotherVeryLongThemeName,
7646
+ ]}
7647
+ />
7648
+ <div>{"hi"}</div>
7649
+ </>
7650
+ }`;
7651
+ const result = await format(input);
7652
+ expect(result).toBeWithNewline(expected);
7653
+ });
7654
+
7655
+ it('preserves other attributes such as ref alongside apply', async () => {
7656
+ await expectUnchanged(`export function App() @{
7657
+ <>
7658
+ <style ref={x} apply={theme} />
7659
+ <div>{"hi"}</div>
7660
+ </>
7661
+ }`);
7662
+ });
7663
+
7664
+ it('formats multiple <style> blocks in one fragment of a @{} body', async () => {
7665
+ const input = `export function App()@{<><style>div{color:red}</style><style apply={theme} /><div>{"hi"}</div></>}`;
7666
+ const expected = `export function App() @{
7667
+ <>
7668
+ <style>
7669
+ div {
7670
+ color: red;
7671
+ }
7672
+ </style>
7673
+ <style apply={theme} />
7674
+ <div>{"hi"}</div>
7675
+ </>
7676
+ }`;
7677
+ const result = await format(input);
7678
+ expect(result).toBeWithNewline(expected);
7679
+ });
7680
+
7681
+ it('formats multiple <style> blocks in one fragment', async () => {
7682
+ const input = `export function App(){return <><style apply={a} /><style apply={b}>p{margin:0}</style><p>{"x"}</p></>}`;
7683
+ const expected = `export function App() {
7684
+ return <>
7685
+ <style apply={a} />
7686
+ <style apply={b}>
7687
+ p {
7688
+ margin: 0;
7689
+ }
7690
+ </style>
7691
+ <p>{"x"}</p>
7692
+ </>;
7693
+ }`;
7694
+ const result = await format(input);
7695
+ expect(result).toBeWithNewline(expected);
7696
+ });
7697
+
7698
+ it('preserves authored blank lines between style blocks and their siblings', async () => {
7699
+ const input = `export function App()@{<>
7700
+ <style apply={theme} />
7701
+
7702
+ <style>div{color:red}</style>
7703
+
7704
+
7705
+ <div>{"hi"}</div></>}`;
7706
+ const expected = `export function App() @{
7707
+ <>
7708
+ <style apply={theme} />
7709
+
7710
+ <style>
7711
+ div {
7712
+ color: red;
7713
+ }
7714
+ </style>
7715
+
7716
+ <div>{"hi"}</div>
7717
+ </>
7718
+ }`;
7719
+ const result = await format(input);
7720
+ expect(result).toBeWithNewline(expected);
7721
+ });
7722
+
7723
+ it('keeps a blank line between setup statements and the fragment holding a style block', async () => {
7724
+ await expectUnchanged(`export function App() @{
7725
+ const x = 1;
7726
+
7727
+ <>
7728
+ <style apply={theme} />
7729
+ <div>{"hi"}</div>
7730
+ </>
7731
+ }`);
7732
+ });
7733
+
7734
+ it('keeps a leading comment on a style block', async () => {
7735
+ await expectUnchanged(`export function App() @{
7736
+ <>
7737
+ // theme
7738
+ <style apply={theme} />
7739
+ <div>{"hi"}</div>
7740
+ </>
7741
+ }`);
7742
+ });
7743
+
7744
+ it('formats a style block inside a fragment of a nested @{} block', async () => {
7745
+ const input = `export function App()@{<div>@{<><style apply={inner} /><span>{"x"}</span></>}</div>}`;
7746
+ const expected = `export function App() @{
7747
+ <div>@{
7748
+ <>
7749
+ <style apply={inner} />
7750
+ <span>{"x"}</span>
7751
+ </>
7752
+ }</div>
7753
+ }`;
7754
+ const result = await format(input);
7755
+ expect(result).toBeWithNewline(expected);
7756
+ });
7757
+
7758
+ it('formats style blocks in fragments of @if and @else bodies', async () => {
7759
+ const input = `export function App()@{<div>@if(cond){<><style apply={a} /><span>{"x"}</span></>}@else{<><style apply={b} /><em>{"y"}</em></>}</div>}`;
7760
+ const expected = `export function App() @{
7761
+ <div>
7762
+ @if (cond) {
7763
+ <>
7764
+ <style apply={a} />
7765
+ <span>{"x"}</span>
7766
+ </>
7767
+ } @else {
7768
+ <>
7769
+ <style apply={b} />
7770
+ <em>{"y"}</em>
7771
+ </>
7772
+ }
7773
+ </div>
7774
+ }`;
7775
+ const result = await format(input);
7776
+ expect(result).toBeWithNewline(expected);
7777
+ });
7778
+
7779
+ it('formats style blocks in fragments of @for bodies', async () => {
7780
+ const input = `export function App()@{<div>@for(const item of items){<><style apply={a} /><span>{item}</span></>}</div>}`;
7781
+ const expected = `export function App() @{
7782
+ <div>
7783
+ @for (const item of items) {
7784
+ <>
7785
+ <style apply={a} />
7786
+ <span>{item}</span>
7787
+ </>
7788
+ }
7789
+ </div>
7790
+ }`;
7791
+ const result = await format(input);
7792
+ expect(result).toBeWithNewline(expected);
7793
+ });
7794
+
7795
+ it('formats style blocks in fragments of @switch case bodies', async () => {
7796
+ const input = `export function App()@{<div>@switch(v){@case 1: {<><style apply={a} /><span>{"x"}</span></>}@default: {<><style apply={b} /><em>{"y"}</em></>}}</div>}`;
7797
+ const expected = `export function App() @{
7798
+ <div>
7799
+ @switch (v) {
7800
+ @case 1: {
7801
+ <>
7802
+ <style apply={a} />
7803
+ <span>{"x"}</span>
7804
+ </>
7805
+ }
7806
+ @default: {
7807
+ <>
7808
+ <style apply={b} />
7809
+ <em>{"y"}</em>
7810
+ </>
7811
+ }
7812
+ }
7813
+ </div>
7814
+ }`;
7815
+ const result = await format(input);
7816
+ expect(result).toBeWithNewline(expected);
7817
+ });
7818
+
7819
+ it('formats style blocks in fragments of @try and @catch bodies', async () => {
7820
+ const input = `export function App()@{<div>@try{<><style apply={a} /><span>{"x"}</span></>}@catch(e){<><style apply={b} /><em>{"err"}</em></>}</div>}`;
7821
+ const expected = `export function App() @{
7822
+ <div>
7823
+ @try {
7824
+ <>
7825
+ <style apply={a} />
7826
+ <span>{"x"}</span>
7827
+ </>
7828
+ } @catch (e) {
7829
+ <>
7830
+ <style apply={b} />
7831
+ <em>{"err"}</em>
7832
+ </>
7833
+ }
7834
+ </div>
7835
+ }`;
7836
+ const result = await format(input);
7837
+ expect(result).toBeWithNewline(expected);
7838
+ });
7839
+
7840
+ it('formats a module-scope body-less bundle export', async () => {
7841
+ const result = await format(`export const bundle = <style apply={[a,b]} />;`);
7842
+ expect(result).toBeWithNewline(`export const bundle = <style apply={[a, b]} />;`);
7843
+ });
7844
+
7845
+ it('formats a module-scope assigned block with a CSS body', async () => {
7846
+ const input = `const theme = <style apply={base}>div{color:red}</style>;`;
7847
+ const expected = `const theme = <style apply={base}>
7848
+ div {
7849
+ color: red;
7850
+ }
7851
+ </style>;`;
7852
+ const result = await format(input);
7853
+ expect(result).toBeWithNewline(expected);
7854
+ });
7855
+ });
7565
7856
  });