|
| 1 | +import assert from 'assert'; |
| 2 | +import semver from 'semver'; |
| 3 | +import plugin from '../../../src/index'; |
| 4 | +import { LegacyESLint, ESLint } from '../../utils/eslint-compat'; |
| 5 | + |
| 6 | +describe('`base` config', () => { |
| 7 | + it('legacy `base` config should work. ', async () => { |
| 8 | + const code = `<script>const a = 1, b = 2;</script> |
| 9 | +<!-- eslint-disable-next-line svelte/no-at-html-tags --> |
| 10 | +{@html a+b} |
| 11 | +{@html a+b}`; |
| 12 | + |
| 13 | + const linter = new LegacyESLint({ |
| 14 | + plugins: { |
| 15 | + svelte: plugin as never |
| 16 | + }, |
| 17 | + baseConfig: { |
| 18 | + parserOptions: { |
| 19 | + ecmaVersion: 2020 |
| 20 | + }, |
| 21 | + extends: ['plugin:svelte/base'], |
| 22 | + rules: { |
| 23 | + 'svelte/no-at-html-tags': 'error' |
| 24 | + } |
| 25 | + }, |
| 26 | + useEslintrc: false |
| 27 | + }); |
| 28 | + const result = await linter.lintText(code, { filePath: 'test.svelte' }); |
| 29 | + const messages = result[0].messages; |
| 30 | + |
| 31 | + assert.deepStrictEqual( |
| 32 | + messages.map((m) => ({ ruleId: m.ruleId, line: m.line, message: m.message })), |
| 33 | + [ |
| 34 | + { |
| 35 | + ruleId: 'svelte/no-at-html-tags', |
| 36 | + message: '`{@html}` can lead to XSS attack.', |
| 37 | + line: 4 |
| 38 | + } |
| 39 | + ] |
| 40 | + ); |
| 41 | + }); |
| 42 | + it('`base` config should work. ', async () => { |
| 43 | + if (semver.satisfies(ESLint.version, '<8.0.0')) return; |
| 44 | + const code = `<script>const a = 1, b = 2;</script> |
| 45 | +<!-- eslint-disable-next-line svelte/no-at-html-tags --> |
| 46 | +{@html a+b} |
| 47 | +{@html a+b}`; |
| 48 | + const linter = new ESLint({ |
| 49 | + overrideConfigFile: true as never, |
| 50 | + overrideConfig: [ |
| 51 | + ...plugin.configs['flat/base'], |
| 52 | + { |
| 53 | + rules: { |
| 54 | + 'svelte/no-at-html-tags': 'error' |
| 55 | + } |
| 56 | + } |
| 57 | + ] as never |
| 58 | + }); |
| 59 | + const result = await linter.lintText(code, { filePath: 'test.svelte' }); |
| 60 | + const messages = result[0].messages; |
| 61 | + |
| 62 | + assert.deepStrictEqual( |
| 63 | + messages.map((m) => ({ ruleId: m.ruleId, line: m.line, message: m.message })), |
| 64 | + [ |
| 65 | + { |
| 66 | + ruleId: 'svelte/no-at-html-tags', |
| 67 | + message: '`{@html}` can lead to XSS attack.', |
| 68 | + line: 4 |
| 69 | + } |
| 70 | + ] |
| 71 | + ); |
| 72 | + |
| 73 | + const resultWithJs = await linter.lintText(';', { filePath: 'test.js' }); |
| 74 | + const messagesWithJs = resultWithJs[0].messages; |
| 75 | + |
| 76 | + assert.deepStrictEqual( |
| 77 | + messagesWithJs.map((m) => ({ |
| 78 | + ruleId: m.ruleId, |
| 79 | + line: m.line, |
| 80 | + message: m.message |
| 81 | + })), |
| 82 | + [] |
| 83 | + ); |
| 84 | + }); |
| 85 | +}); |
0 commit comments