|
| 1 | +import { expect, test, vi } from 'vitest'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { should_ignore } from './utils.js'; |
| 4 | + |
| 5 | +// Mock the colors module to avoid issues in tests |
| 6 | +vi.mock('kleur', () => ({ |
| 7 | + default: { |
| 8 | + bold: () => ({ red: (/** @type {string} */ str) => str }) |
| 9 | + } |
| 10 | +})); |
| 11 | + |
| 12 | +// We need to test the warning_preprocessor functionality |
| 13 | +// Since it's not exported, we'll recreate the relevant parts for testing |
| 14 | +const options_regex = /(export\s+const\s+(prerender|csr|ssr|trailingSlash))\s*=/s; |
| 15 | + |
| 16 | +/** |
| 17 | + * @param {string} content |
| 18 | + * @param {string} filename |
| 19 | + */ |
| 20 | +function should_warn_for_content(content, filename) { |
| 21 | + const basename = path.basename(filename); |
| 22 | + if (basename.startsWith('+page.') || basename.startsWith('+layout.')) { |
| 23 | + const match = content.match(options_regex); |
| 24 | + return match && match.index !== undefined && !should_ignore(content, match.index); |
| 25 | + } |
| 26 | + return false; |
| 27 | +} |
| 28 | + |
| 29 | +test.each([ |
| 30 | + [ |
| 31 | + 'single-line comment with export const trailingSlash', |
| 32 | + '// export const trailingSlash = "always"', |
| 33 | + '+page.svelte', |
| 34 | + false |
| 35 | + ], |
| 36 | + [ |
| 37 | + 'multi-line comment with export const trailingSlash', |
| 38 | + '/* export const trailingSlash = "always" */', |
| 39 | + '+page.svelte', |
| 40 | + false |
| 41 | + ], |
| 42 | + [ |
| 43 | + 'HTML comment with export const trailingSlash', |
| 44 | + '<!-- export const trailingSlash = "always" -->', |
| 45 | + '+page.svelte', |
| 46 | + false |
| 47 | + ], |
| 48 | + [ |
| 49 | + 'single-line comment with export const ssr', |
| 50 | + '// export const ssr = false', |
| 51 | + '+page.svelte', |
| 52 | + false |
| 53 | + ], |
| 54 | + [ |
| 55 | + 'valid export const trailingSlash', |
| 56 | + 'export const trailingSlash = "always"', |
| 57 | + '+page.svelte', |
| 58 | + true |
| 59 | + ], |
| 60 | + ['valid export const ssr', 'export const ssr = false', '+page.svelte', true], |
| 61 | + [ |
| 62 | + 'valid export const with spacing', |
| 63 | + 'export const trailingSlash = "always"', |
| 64 | + '+page.svelte', |
| 65 | + true |
| 66 | + ], |
| 67 | + [ |
| 68 | + 'valid export on non-page file should not warn', |
| 69 | + 'export const trailingSlash = "always"', |
| 70 | + 'regular.svelte', |
| 71 | + false |
| 72 | + ], |
| 73 | + [ |
| 74 | + 'comment followed by valid export', |
| 75 | + '// This is a comment\nexport const trailingSlash = "always"', |
| 76 | + '+page.svelte', |
| 77 | + true // Should still warn for the valid export |
| 78 | + ], |
| 79 | + [ |
| 80 | + 'valid export with trailing comment', |
| 81 | + 'export const trailingSlash = "always" // with comment after', |
| 82 | + '+page.svelte', |
| 83 | + true |
| 84 | + ], |
| 85 | + [ |
| 86 | + 'comment with */ inside - not actually nested', |
| 87 | + '/* comment with */ inside */ export const trailingSlash = "always"', |
| 88 | + '+page.svelte', |
| 89 | + true // This should warn because the export is not in a comment |
| 90 | + ], |
| 91 | + [ |
| 92 | + 'multiple line comment spanning lines', |
| 93 | + '/*\n * multi-line comment\n * export const trailingSlash = "always"\n */', |
| 94 | + '+page.svelte', |
| 95 | + false |
| 96 | + ], |
| 97 | + [ |
| 98 | + 'multiple single-line comments', |
| 99 | + '// first comment\n// export const trailingSlash = "always"\n// third comment', |
| 100 | + '+page.svelte', |
| 101 | + false |
| 102 | + ], |
| 103 | + [ |
| 104 | + 'HTML comment spanning multiple lines', |
| 105 | + '<!--\n export const trailingSlash = "always"\n-->', |
| 106 | + '+page.svelte', |
| 107 | + false |
| 108 | + ], |
| 109 | + [ |
| 110 | + 'edge case: comment delimiters in strings', |
| 111 | + '"/*"; export const trailingSlash = "always"; "*/"', |
| 112 | + '+page.svelte', |
| 113 | + true // Should warn because the export is not actually in a comment |
| 114 | + ], |
| 115 | + [ |
| 116 | + 'escaped quotes in strings', |
| 117 | + '"comment with \\"/*\\" inside"; export const trailingSlash = "always"', |
| 118 | + '+page.svelte', |
| 119 | + true |
| 120 | + ], |
| 121 | + [ |
| 122 | + 'single quotes with comment delimiters', |
| 123 | + "'/*'; export const trailingSlash = 'always'; '*/'", |
| 124 | + '+page.svelte', |
| 125 | + true |
| 126 | + ], |
| 127 | + [ |
| 128 | + 'template literal with comment delimiters', |
| 129 | + '`/*`; export const trailingSlash = "always"; `*/`', |
| 130 | + '+page.svelte', |
| 131 | + true |
| 132 | + ], |
| 133 | + [ |
| 134 | + 'real comment after string with comment delimiter', |
| 135 | + '"fake /*"; /* real comment with export const trailingSlash = "always" */', |
| 136 | + '+page.svelte', |
| 137 | + false |
| 138 | + ], |
| 139 | + [ |
| 140 | + 'nested comment-like structures in strings', |
| 141 | + '"/* /* nested */ */"; export const trailingSlash = "always"', |
| 142 | + '+page.svelte', |
| 143 | + true |
| 144 | + ], |
| 145 | + ['page option match inside string', '"export const trailingSlash = true"', '+page.svelte', false], |
| 146 | + [ |
| 147 | + 'page option match inside template literal', |
| 148 | + '`${42}export const trailingSlash = true`', |
| 149 | + '+page.svelte', |
| 150 | + false |
| 151 | + ] |
| 152 | +])('warning behavior: %s', (_description, content, filename, should_warn) => { |
| 153 | + const result = should_warn_for_content(content, filename); |
| 154 | + expect(result).toBe(should_warn); |
| 155 | +}); |
0 commit comments