@ripple-ts/prettier-plugin 0.2.207 → 0.2.210

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/src/index.test.js CHANGED
@@ -2116,6 +2116,49 @@ function test() {
2116
2116
  expect(result).toBeWithNewline(expected);
2117
2117
  });
2118
2118
 
2119
+ it('should preserve comments before closing tag in elements', async () => {
2120
+ const expected = `component App() {
2121
+ <div id="second-top-block">
2122
+ if (true) {
2123
+ <div>{'b is true'}</div>
2124
+ }
2125
+ // <div>
2126
+ // <div />
2127
+ // </div>
2128
+ // <div id="sibling-block">{'Sibling'}</div>
2129
+ </div>
2130
+ }`;
2131
+
2132
+ const result = await format(expected, { singleQuote: true });
2133
+ expect(result).toBeWithNewline(expected);
2134
+ });
2135
+
2136
+ it('should preserve trailing comments after last child element before closing tag', async () => {
2137
+ const expected = `component App() {
2138
+ <div>
2139
+ <span>{'first'}</span>
2140
+ <span>{'second'}</span>
2141
+ // trailing comment 1
2142
+ // trailing comment 2
2143
+ </div>
2144
+ }`;
2145
+
2146
+ const result = await format(expected, { singleQuote: true });
2147
+ expect(result).toBeWithNewline(expected);
2148
+ });
2149
+
2150
+ it('should preserve block comments before closing tag in elements', async () => {
2151
+ const expected = `component App() {
2152
+ <div>
2153
+ <span>{'child'}</span>
2154
+ /* block comment */
2155
+ </div>
2156
+ }`;
2157
+
2158
+ const result = await format(expected, { singleQuote: true });
2159
+ expect(result).toBeWithNewline(expected);
2160
+ });
2161
+
2119
2162
  it('should preserve trailing comments in function parameters', async () => {
2120
2163
  const expected = `function test(
2121
2164
  // comment in params
@@ -2235,6 +2278,34 @@ const obj2 = #{
2235
2278
  expect(result).toBeWithNewline(expected);
2236
2279
  });
2237
2280
 
2281
+ it('should not add an extra blank line before a comment inside element children', async () => {
2282
+ const expected = `component App() {
2283
+ <div id="second-top-block">
2284
+ <div>
2285
+ let x = 1;
2286
+ // comment
2287
+ <div>{'Test'}</div>
2288
+ </div>
2289
+ </div>
2290
+ }`;
2291
+
2292
+ const result = await format(expected, { singleQuote: true });
2293
+ expect(result).toBeWithNewline(expected);
2294
+ });
2295
+
2296
+ it('should preserve an existing blank line before a comment inside element children', async () => {
2297
+ const expected = `component App() {
2298
+ <div>
2299
+ let x = 1;
2300
+
2301
+ // comment
2302
+ <div>{'Test'}</div>
2303
+ </div>
2304
+ }`;
2305
+
2306
+ const result = await format(expected, { singleQuote: true });
2307
+ expect(result).toBeWithNewline(expected);
2308
+ });
2238
2309
  it('should preserve comment if the whole component code is commented out', async () => {
2239
2310
  const expected = `export component Test() {
2240
2311
  // thing
@@ -2887,6 +2958,64 @@ const items = [] as unknown[];`;
2887
2958
  expect(result).toBeWithNewline(expected);
2888
2959
  });
2889
2960
 
2961
+ it('should format TSDeclareFunction (function overload signatures)', async () => {
2962
+ const input = `export function test(arg: string): string;
2963
+ export function test(arg: number): string;
2964
+ export function test(arg: string | number): string {
2965
+ return String(arg);
2966
+ }`;
2967
+ const expected = `export function test(arg: string): string;
2968
+ export function test(arg: number): string;
2969
+ export function test(arg: string | number): string {
2970
+ return String(arg);
2971
+ }`;
2972
+ const result = await format(input);
2973
+ expect(result).toBeWithNewline(expected);
2974
+ });
2975
+
2976
+ it('should preserve declare modifier on ambient function declarations', async () => {
2977
+ const input = `declare function doSomething(x: string): void;
2978
+ declare function processData<T>(data: T): Promise<T>;`;
2979
+ const expected = `declare function doSomething(x: string): void;
2980
+ declare function processData<T>(data: T): Promise<T>;`;
2981
+ const result = await format(input);
2982
+ expect(result).toBeWithNewline(expected);
2983
+ });
2984
+
2985
+ it('should preserve generics on method shorthand in object literals', async () => {
2986
+ const input = `function getBuilder() {
2987
+ return {
2988
+ build<T>(): T {
2989
+ return 'test' as unknown as T;
2990
+ },
2991
+ };
2992
+ }`;
2993
+ const expected = `function getBuilder() {
2994
+ return {
2995
+ build<T>(): T {
2996
+ return 'test' as unknown as T;
2997
+ },
2998
+ };
2999
+ }`;
3000
+ const result = await format(input, { singleQuote: true });
3001
+ expect(result).toBeWithNewline(expected);
3002
+ });
3003
+
3004
+ it('should preserve multiple generics on method shorthand', async () => {
3005
+ const input = `const obj = {
3006
+ method<V, T, U>(): { build: () => V; data: T; key: U } {
3007
+ return null as any;
3008
+ },
3009
+ };`;
3010
+ const expected = `const obj = {
3011
+ method<V, T, U>(): { build: () => V; data: T; key: U } {
3012
+ return null as any;
3013
+ },
3014
+ };`;
3015
+ const result = await format(input, { singleQuote: true });
3016
+ expect(result).toBeWithNewline(expected);
3017
+ });
3018
+
2890
3019
  it('should retain templated declarations', async () => {
2891
3020
  const expected = `function Wrapper() {
2892
3021
  return {
@@ -3983,6 +4112,49 @@ component Polygon() {
3983
4112
  const result = await format(expected, { singleQuote: true, printWidth: 100 });
3984
4113
  expect(result).toBeWithNewline(expected);
3985
4114
  });
4115
+
4116
+ it('should preserve blank line between commented out block and following element', async () => {
4117
+ const expected = `component App() {
4118
+ <div id="second-top-block">
4119
+ <div>
4120
+ <div />
4121
+ </div>
4122
+ <div id="sibling-block">{"Sibling"}</div>
4123
+ </div>
4124
+
4125
+ // if (show) {
4126
+ // <div id="third-top-block">{"Top Scope - Show is true"}</div>
4127
+ // }
4128
+
4129
+ <button onClick={() => (@b = !@b)}>{"Toggle b"}</button>
4130
+ }`;
4131
+
4132
+ const result = await format(expected, { printWidth: 100 });
4133
+ expect(result).toBeWithNewline(expected);
4134
+ });
4135
+
4136
+ it('should preserve blank line after multi-line comment block followed by element in component body', async () => {
4137
+ const expected = `component App() {
4138
+ <div>
4139
+ <div>
4140
+ let x = 1;
4141
+
4142
+ // inner comment
4143
+ <div />
4144
+ </div>
4145
+ <div>{"Sibling"}</div>
4146
+ </div>
4147
+
4148
+ // if (show) {
4149
+ // <div>{"Top Scope - Show is true"}</div>
4150
+ // }
4151
+
4152
+ <button onClick={() => (@b = !@b)}>{"Toggle b"}</button>
4153
+ }`;
4154
+
4155
+ const result = await format(expected, { printWidth: 100 });
4156
+ expect(result).toBeWithNewline(expected);
4157
+ });
3986
4158
  });
3987
4159
 
3988
4160
  describe('Arrays with printWidth constraints', () => {
@@ -4519,5 +4691,345 @@ component App() {
4519
4691
  expect(result).toBeWithNewline(expected);
4520
4692
  });
4521
4693
  });
4694
+
4695
+ describe('if statement formatting', () => {
4696
+ it('should format chained if-else statements with non-block bodies on separate lines', async () => {
4697
+ const input = `component Test() {
4698
+ <button
4699
+ onClick={() => {
4700
+ if (@status === 'a') @status = 'b'; else if (@status === 'b') @status = 'c'; else @status =
4701
+ 'a';
4702
+ }}
4703
+ >
4704
+ {'Click'}
4705
+ </button>
4706
+ }`;
4707
+ const expected = `component Test() {
4708
+ <button
4709
+ onClick={() => {
4710
+ if (@status === 'a') @status = 'b';
4711
+ else if (@status === 'b') @status = 'c';
4712
+ else @status = 'a';
4713
+ }}
4714
+ >
4715
+ {'Click'}
4716
+ </button>
4717
+ }`;
4718
+
4719
+ const result = await format(input, { singleQuote: true });
4720
+ expect(result).toBeWithNewline(expected);
4721
+ });
4722
+
4723
+ it('should format simple if statement with non-block body', async () => {
4724
+ const input = `component Test() {
4725
+ let x = 0;
4726
+ if (x === 0) x = 1;
4727
+ <div>{x}</div>
4728
+ }`;
4729
+ const expected = `component Test() {
4730
+ let x = 0;
4731
+ if (x === 0) x = 1;
4732
+ <div>{x}</div>
4733
+ }`;
4734
+
4735
+ const result = await format(input, { singleQuote: true });
4736
+ expect(result).toBeWithNewline(expected);
4737
+ });
4738
+
4739
+ it('should format if-else with non-block bodies', async () => {
4740
+ const input = `component Test() {
4741
+ let x = 0;
4742
+ if (x === 0) x = 1; else x = 2;
4743
+ <div>{x}</div>
4744
+ }`;
4745
+ const expected = `component Test() {
4746
+ let x = 0;
4747
+ if (x === 0) x = 1;
4748
+ else x = 2;
4749
+ <div>{x}</div>
4750
+ }`;
4751
+
4752
+ const result = await format(input, { singleQuote: true });
4753
+ expect(result).toBeWithNewline(expected);
4754
+ });
4755
+
4756
+ it('should format nested if statements with non-block bodies', async () => {
4757
+ const input = `component Test() {
4758
+ let x = 0;
4759
+ if (x === 0) if (x === 1) x = 2; else x = 3;
4760
+ <div>{x}</div>
4761
+ }`;
4762
+ const expected = `component Test() {
4763
+ let x = 0;
4764
+ if (x === 0)
4765
+ if (x === 1) x = 2;
4766
+ else x = 3;
4767
+ <div>{x}</div>
4768
+ }`;
4769
+
4770
+ const result = await format(input, { singleQuote: true });
4771
+ expect(result).toBeWithNewline(expected);
4772
+ });
4773
+ });
4774
+
4775
+ it('should not move comments before if statement into the test condition', async () => {
4776
+ const input = `component App() {
4777
+ <div id="second-top-block">
4778
+ // <div>
4779
+ if (true) {
4780
+ <div>{'b is true'}</div>
4781
+ }
4782
+ // <div>
4783
+ // <div>
4784
+ // if (@b) {
4785
+ // <span>nested</span>
4786
+ // }
4787
+ // </div>
4788
+ // </div>
4789
+ // <div />
4790
+ // </div>
4791
+ // <div id="sibling-block">{'Sibling'}</div>
4792
+ </div>
4793
+ }`;
4794
+ const expected = `component App() {
4795
+ <div id="second-top-block">
4796
+ // <div>
4797
+ if (true) {
4798
+ <div>{'b is true'}</div>
4799
+ }
4800
+ // <div>
4801
+ // <div>
4802
+ // if (@b) {
4803
+ // <span>nested</span>
4804
+ // }
4805
+ // </div>
4806
+ // </div>
4807
+ // <div />
4808
+ // </div>
4809
+ // <div id="sibling-block">{'Sibling'}</div>
4810
+ </div>
4811
+ }`;
4812
+
4813
+ const result = await format(input, { singleQuote: true });
4814
+ expect(result).toBeWithNewline(expected);
4815
+ });
4816
+
4817
+ it('should not move comments before while statement into the test condition', async () => {
4818
+ const input = `function test() {
4819
+ let i = 0;
4820
+ // comment before while
4821
+ while (i < 10) {
4822
+ i++;
4823
+ }
4824
+ }`;
4825
+ const expected = `function test() {
4826
+ let i = 0;
4827
+ // comment before while
4828
+ while (i < 10) {
4829
+ i++;
4830
+ }
4831
+ }`;
4832
+
4833
+ const result = await format(input, { singleQuote: true });
4834
+ expect(result).toBeWithNewline(expected);
4835
+ });
4836
+
4837
+ it('should not move comments before for-of statement into the right expression', async () => {
4838
+ const input = `function test() {
4839
+ // comment before for-of
4840
+ for (const item of items) {
4841
+ console.log(item);
4842
+ }
4843
+ }`;
4844
+ const expected = `function test() {
4845
+ // comment before for-of
4846
+ for (const item of items) {
4847
+ console.log(item);
4848
+ }
4849
+ }`;
4850
+
4851
+ const result = await format(input, { singleQuote: true });
4852
+ expect(result).toBeWithNewline(expected);
4853
+ });
4854
+
4855
+ it('should not move comments before switch statement into the discriminant', async () => {
4856
+ const input = `function test() {
4857
+ let x = 1;
4858
+ // comment before switch
4859
+ switch (x) {
4860
+ case 1:
4861
+ console.log('one');
4862
+ }
4863
+ }`;
4864
+ const expected = `function test() {
4865
+ let x = 1;
4866
+ // comment before switch
4867
+ switch (x) {
4868
+ case 1:
4869
+ console.log('one');
4870
+ }
4871
+ }`;
4872
+
4873
+ const result = await format(input, { singleQuote: true });
4874
+ expect(result).toBeWithNewline(expected);
4875
+ });
4876
+
4877
+ it('should handle multiple comments before if statement', async () => {
4878
+ const input = `function test() {
4879
+ // comment 1
4880
+ // comment 2
4881
+ if (true) {
4882
+ console.log('test');
4883
+ }
4884
+ }`;
4885
+ const expected = `function test() {
4886
+ // comment 1
4887
+ // comment 2
4888
+ if (true) {
4889
+ console.log('test');
4890
+ }
4891
+ }`;
4892
+
4893
+ const result = await format(input, { singleQuote: true });
4894
+ expect(result).toBeWithNewline(expected);
4895
+ });
4896
+
4897
+ it('should handle comments before try/catch blocks', async () => {
4898
+ const input = `function test() {
4899
+ // comment before try
4900
+ try {
4901
+ doSomething();
4902
+ } catch (e) {
4903
+ console.error(e);
4904
+ }
4905
+ }`;
4906
+ const expected = `function test() {
4907
+ // comment before try
4908
+ try {
4909
+ doSomething();
4910
+ } catch (e) {
4911
+ console.error(e);
4912
+ }
4913
+ }`;
4914
+
4915
+ const result = await format(input, { singleQuote: true });
4916
+ expect(result).toBeWithNewline(expected);
4917
+ });
4918
+
4919
+ it('should handle comments before try/catch/finally blocks', async () => {
4920
+ const input = `function test() {
4921
+ // comment before try
4922
+ try {
4923
+ doSomething();
4924
+ } catch (e) {
4925
+ console.error(e);
4926
+ } finally {
4927
+ cleanup();
4928
+ }
4929
+ }`;
4930
+ const expected = `function test() {
4931
+ // comment before try
4932
+ try {
4933
+ doSomething();
4934
+ } catch (e) {
4935
+ console.error(e);
4936
+ } finally {
4937
+ cleanup();
4938
+ }
4939
+ }`;
4940
+
4941
+ const result = await format(input, { singleQuote: true });
4942
+ expect(result).toBeWithNewline(expected);
4943
+ });
4944
+
4945
+ it('should handle comments inside try/catch blocks', async () => {
4946
+ const input = `function test() {
4947
+ try {
4948
+ // comment inside try
4949
+ doSomething();
4950
+ } catch (e) {
4951
+ // comment inside catch
4952
+ console.error(e);
4953
+ }
4954
+ }`;
4955
+ const expected = `function test() {
4956
+ try {
4957
+ // comment inside try
4958
+ doSomething();
4959
+ } catch (e) {
4960
+ // comment inside catch
4961
+ console.error(e);
4962
+ }
4963
+ }`;
4964
+
4965
+ const result = await format(input, { singleQuote: true });
4966
+ expect(result).toBeWithNewline(expected);
4967
+ });
4968
+
4969
+ it('should handle block comments with try/catch', async () => {
4970
+ const input = `function test() {
4971
+ /* block comment before try */
4972
+ try {
4973
+ doSomething();
4974
+ } catch (e) {
4975
+ /* block comment in catch */
4976
+ console.error(e);
4977
+ }
4978
+ }`;
4979
+ const expected = `function test() {
4980
+ /* block comment before try */
4981
+ try {
4982
+ doSomething();
4983
+ } catch (e) {
4984
+ /* block comment in catch */
4985
+ console.error(e);
4986
+ }
4987
+ }`;
4988
+
4989
+ const result = await format(input, { singleQuote: true });
4990
+ expect(result).toBeWithNewline(expected);
4991
+ });
4992
+
4993
+ it('should handle comments before try block in Ripple component', async () => {
4994
+ const input = `component App() {
4995
+ <div id="second-top-block">
4996
+ // <div>
4997
+ try {
4998
+ <div>{'b is true'}</div>
4999
+ } catch (e) {}
5000
+ // <div>
5001
+ // <div>
5002
+ // if (@b) {
5003
+ // return;
5004
+ // }
5005
+ // </div>
5006
+ // </div>
5007
+ // <div />
5008
+ // </div>
5009
+ // <div id="sibling-block">{'Sibling'}</div>
5010
+ </div>
5011
+ }`;
5012
+ const expected = `component App() {
5013
+ <div id="second-top-block">
5014
+ // <div>
5015
+ try {
5016
+ <div>{'b is true'}</div>
5017
+ } catch (e) {}
5018
+ // <div>
5019
+ // <div>
5020
+ // if (@b) {
5021
+ // return;
5022
+ // }
5023
+ // </div>
5024
+ // </div>
5025
+ // <div />
5026
+ // </div>
5027
+ // <div id="sibling-block">{'Sibling'}</div>
5028
+ </div>
5029
+ }`;
5030
+
5031
+ const result = await format(input, { singleQuote: true });
5032
+ expect(result).toBeWithNewline(expected);
5033
+ });
4522
5034
  });
4523
5035
  });