|
1 | 1 | import { describe, it, expect } from 'vitest';
|
2 |
| -import { runLiquidCheck } from '../../test'; |
| 2 | +import { runLiquidCheck, applySuggestions } from '../../test'; |
3 | 3 | import { ValidRenderSnippetParamTypes } from '.';
|
4 | 4 | import { MockFileSystem } from '../../test';
|
5 | 5 | import { SupportedParamTypes } from '../../liquid-doc/utils';
|
@@ -180,4 +180,154 @@ describe('Module: ValidRenderSnippetParamTypes', () => {
|
180 | 180 | expect(offenses).toHaveLength(0);
|
181 | 181 | });
|
182 | 182 | });
|
| 183 | + |
| 184 | + describe('autofix tests', () => { |
| 185 | + const makeSnippet = (type: string) => ` |
| 186 | + {% doc %} |
| 187 | + @param {${type}} param - Description |
| 188 | + {% enddoc %} |
| 189 | + <div>{{ param }}</div> |
| 190 | + `; |
| 191 | + |
| 192 | + it('should suggest replacing with default value for type or removing value', async () => { |
| 193 | + const fs = new MockFileSystem({ |
| 194 | + 'snippets/card.liquid': makeSnippet('string'), |
| 195 | + }); |
| 196 | + |
| 197 | + const sourceCode = `{% render 'card', param: 123 %}`; |
| 198 | + const offenses = await runLiquidCheck(ValidRenderSnippetParamTypes, sourceCode, undefined, { |
| 199 | + fs, |
| 200 | + }); |
| 201 | + |
| 202 | + expect(offenses).toHaveLength(1); |
| 203 | + expect(offenses[0].suggest).toHaveLength(2); |
| 204 | + expect(offenses[0].suggest?.[0]?.message).toBe("Replace with default value '''' for string"); |
| 205 | + |
| 206 | + const result = applySuggestions(sourceCode, offenses[0]); |
| 207 | + expect(result?.[0]).toEqual(`{% render 'card', param: '' %}`); |
| 208 | + |
| 209 | + const suggestions = applySuggestions(sourceCode, offenses[0]); |
| 210 | + expect(suggestions?.[1]).toEqual(`{% render 'card', param: %}`); |
| 211 | + }); |
| 212 | + |
| 213 | + it('should allow users to fix a single parameter when multiple are provided`', async () => { |
| 214 | + const fs = new MockFileSystem({ |
| 215 | + 'snippets/card.liquid': ` |
| 216 | + {% doc %} |
| 217 | + @param {string} title - The title |
| 218 | + @param {number} count - The count |
| 219 | + {% enddoc %} |
| 220 | + <div>{{ title }} {{ count }}</div> |
| 221 | + `, |
| 222 | + }); |
| 223 | + |
| 224 | + const sourceCode = `{% render 'card', title: 123, count: 5 %}`; |
| 225 | + const offenses = await runLiquidCheck(ValidRenderSnippetParamTypes, sourceCode, undefined, { |
| 226 | + fs, |
| 227 | + }); |
| 228 | + |
| 229 | + expect(offenses).toHaveLength(1); |
| 230 | + expect(offenses[0].message).toBe( |
| 231 | + "Type mismatch for parameter 'title': expected string, got number", |
| 232 | + ); |
| 233 | + |
| 234 | + const result = applySuggestions(sourceCode, offenses[0]); |
| 235 | + expect(result?.[0]).toEqual(`{% render 'card', title: '', count: 5 %}`); |
| 236 | + }); |
| 237 | + |
| 238 | + it('should handle parameters with trailing commas', async () => { |
| 239 | + const fs = new MockFileSystem({ |
| 240 | + 'snippets/card.liquid': makeSnippet('string'), |
| 241 | + }); |
| 242 | + |
| 243 | + const sourceCode = `{% render 'card', param: 123, %}`; |
| 244 | + const offenses = await runLiquidCheck(ValidRenderSnippetParamTypes, sourceCode, undefined, { |
| 245 | + fs, |
| 246 | + }); |
| 247 | + |
| 248 | + expect(offenses).toHaveLength(1); |
| 249 | + |
| 250 | + const result = applySuggestions(sourceCode, offenses[0]); |
| 251 | + expect(result?.[0]).toEqual(`{% render 'card', param: '', %}`); |
| 252 | + }); |
| 253 | + |
| 254 | + it('should handle parameters with complex spacing', async () => { |
| 255 | + const fs = new MockFileSystem({ |
| 256 | + 'snippets/card.liquid': ` |
| 257 | + {% doc %} |
| 258 | + @param {string} title - The title |
| 259 | + @param {number} count - The count |
| 260 | + {% enddoc %} |
| 261 | + `, |
| 262 | + }); |
| 263 | + |
| 264 | + const sourceCode = `{% render 'card', |
| 265 | + title: 123, |
| 266 | + count: 5 |
| 267 | + %}`; |
| 268 | + const offenses = await runLiquidCheck(ValidRenderSnippetParamTypes, sourceCode, undefined, { |
| 269 | + fs, |
| 270 | + }); |
| 271 | + |
| 272 | + expect(offenses).toHaveLength(1); |
| 273 | + |
| 274 | + const result = applySuggestions(sourceCode, offenses[0]); |
| 275 | + expect(result?.[0]).toEqual(`{% render 'card', |
| 276 | + title: '', |
| 277 | + count: 5 |
| 278 | + %}`); |
| 279 | + }); |
| 280 | + |
| 281 | + it('should handle parameter with no space after colon', async () => { |
| 282 | + const fs = new MockFileSystem({ |
| 283 | + 'snippets/card.liquid': makeSnippet('string'), |
| 284 | + }); |
| 285 | + |
| 286 | + const sourceCode = `{% render 'card', param:123 %}`; |
| 287 | + const offenses = await runLiquidCheck(ValidRenderSnippetParamTypes, sourceCode, undefined, { |
| 288 | + fs, |
| 289 | + }); |
| 290 | + |
| 291 | + expect(offenses).toHaveLength(1); |
| 292 | + |
| 293 | + const result = applySuggestions(sourceCode, offenses[0]); |
| 294 | + expect(result?.[0]).toEqual(`{% render 'card', param:'' %}`); |
| 295 | + }); |
| 296 | + |
| 297 | + it('should handle parameter with multiple spaces after colon', async () => { |
| 298 | + const fs = new MockFileSystem({ |
| 299 | + 'snippets/card.liquid': makeSnippet('string'), |
| 300 | + }); |
| 301 | + |
| 302 | + const sourceCode = `{% render 'card', param: 123 %}`; |
| 303 | + const offenses = await runLiquidCheck(ValidRenderSnippetParamTypes, sourceCode, undefined, { |
| 304 | + fs, |
| 305 | + }); |
| 306 | + |
| 307 | + expect(offenses).toHaveLength(1); |
| 308 | + |
| 309 | + const result = applySuggestions(sourceCode, offenses[0]); |
| 310 | + expect(result?.[0]).toEqual(`{% render 'card', param: '' %}`); |
| 311 | + }); |
| 312 | + |
| 313 | + it('should handle parameter with newlines', async () => { |
| 314 | + const fs = new MockFileSystem({ |
| 315 | + 'snippets/card.liquid': makeSnippet('string'), |
| 316 | + }); |
| 317 | + |
| 318 | + const sourceCode = `{% render 'card', param: |
| 319 | + 123 |
| 320 | + %}`; |
| 321 | + const offenses = await runLiquidCheck(ValidRenderSnippetParamTypes, sourceCode, undefined, { |
| 322 | + fs, |
| 323 | + }); |
| 324 | + |
| 325 | + expect(offenses).toHaveLength(1); |
| 326 | + |
| 327 | + const result = applySuggestions(sourceCode, offenses[0]); |
| 328 | + expect(result?.[0]).toEqual(`{% render 'card', param: |
| 329 | + '' |
| 330 | + %}`); |
| 331 | + }); |
| 332 | + }); |
183 | 333 | });
|
0 commit comments