Skip to content

Commit 7bd70b6

Browse files
authored
feat: support svelte out of box (#483)
1 parent 928a60f commit 7bd70b6

File tree

8 files changed

+196
-165
lines changed

8 files changed

+196
-165
lines changed

.changeset/odd-eagles-breathe.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'eslint-plugin-prettier': minor
3+
---
4+
5+
feat: support svelte out of box
6+
7+
close #472, close #482
8+
9+
We recommend to use [`eslint-plugin-svelte`](https://github.com/ota-meshi/eslint-plugin-svelte) instead of [`eslint-plugin-svelte3`](https://github.com/sveltejs/eslint-plugin-svelte3).

eslint-plugin-prettier.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,33 @@ module.exports = {
187187
// by default.
188188
// Related ESLint plugins are:
189189
// 1. `eslint-plugin-graphql` (replacement: `@graphql-eslint/eslint-plugin`)
190-
// 2. `eslint-plugin-markdown@1` (replacement: `eslint-plugin-markdown@2+`)
191-
// 3. `eslint-plugin-html`
190+
// 2. `eslint-plugin-html`
191+
// 3. `eslint-plugin-markdown@1` (replacement: `eslint-plugin-markdown@2+`)
192+
// 4. `eslint-plugin-svelte3` (replacement: `eslint-plugin-svelte@2+`)
192193
const parserBlocklist = [null, 'markdown', 'html'];
193194

194195
let inferParserToBabel = parserBlocklist.includes(inferredParser);
195196

196-
if (
197+
switch (inferredParser) {
197198
// it could be processed by `@graphql-eslint/eslint-plugin` or `eslint-plugin-graphql`
198-
inferredParser === 'graphql' &&
199-
// for `eslint-plugin-graphql`, see https://github.com/apollographql/eslint-plugin-graphql/blob/master/src/index.js#L416
200-
source.startsWith('ESLintPluginGraphQLFile`')
201-
) {
202-
inferParserToBabel = true;
199+
case 'graphql': {
200+
if (
201+
// for `eslint-plugin-graphql`, see https://github.com/apollographql/eslint-plugin-graphql/blob/master/src/index.js#L416
202+
source.startsWith('ESLintPluginGraphQLFile`')
203+
) {
204+
inferParserToBabel = true;
205+
}
206+
break;
207+
}
208+
// it could be processed by `@ota-meshi/eslint-plugin-svelte`, `eslint-plugin-svelte` or `eslint-plugin-svelte3`
209+
case 'svelte': {
210+
// The `source` would be modified by `eslint-plugin-svelte3`
211+
if (!context.parserPath.includes('svelte-eslint-parser')) {
212+
// We do not support `eslint-plugin-svelte3`,
213+
// the users should run `prettier` on `.svelte` files manually
214+
return;
215+
}
216+
}
203217
}
204218

205219
if (inferParserToBabel) {
@@ -223,6 +237,7 @@ module.exports = {
223237
'html',
224238
'mdx',
225239
'angular',
240+
'svelte',
226241
];
227242
if (parserBlocklist.includes(inferredParser)) {
228243
return;

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
"@changesets/changelog-github": "^0.4.5",
4949
"@changesets/cli": "^2.23.0",
5050
"@graphql-eslint/eslint-plugin": "^2.5.0",
51+
"@ota-meshi/eslint-plugin-svelte": "^0.34.1",
5152
"@typescript-eslint/parser": "^5.29.0",
52-
"eslint": "^7.32.0",
5353
"eslint-config-prettier": "^8.5.0",
5454
"eslint-mdx": "^1.17.0",
5555
"eslint-plugin-eslint-plugin": "^4.3.0",
@@ -60,6 +60,7 @@
6060
"mocha": "^9.2.2",
6161
"patch-package": "^6.4.7",
6262
"prettier": "^2.7.1",
63+
"svelte": "^3.48.0",
6364
"vue-eslint-parser": "^8.3.0"
6465
},
6566
"resolutions": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let name = 'world';
3+
</script>
4+
5+
<h1>Hello {name}!</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let name = 'world';
3+
</script>
4+
5+
<h1>Hello {name}!</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let name = 'world';
3+
</script>
4+
5+
<h1>Hello {name}!</h1>

test/prettier.js

+64-6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ const eslint = new ESLint({
4646
'mdx/code-block': true,
4747
},
4848
},
49+
{
50+
files: '**/eslint-plugin-svelte3/*.svelte',
51+
plugins: ['svelte3'],
52+
processor: 'svelte3/svelte3',
53+
},
54+
{
55+
files: '**/eslint-plugin-svelte3/*.named-blocks.svelte',
56+
settings: {
57+
'svelte3/named-blocks': true,
58+
},
59+
},
60+
{
61+
files: '**/@ota-meshi/eslint-plugin-svelte/*.svelte',
62+
extends: ['plugin:@ota-meshi/svelte/recommended'],
63+
},
4964
],
5065
},
5166
useEslintrc: false,
@@ -158,8 +173,8 @@ atGraphqlEslintRuleTester.run('@graphql-eslint/eslint-plugin', rule, {
158173
],
159174
});
160175

161-
// eslint-plugin-graphql handles literal graphql files by tranforming graphql
162-
// code with a processor, instead of using a parser. Unfortunatly we cant
176+
// eslint-plugin-graphql handles literal graphql files by transforming graphql
177+
// code with a processor, instead of using a parser. Unfortunately we cant
163178
// specify custom processors in a RuleTester, so instead we have write test code
164179
// that is the result of eslint-plugin-graphql's processing - this is the
165180
// ESLintPluginGraphQLFile tagged template literal. See
@@ -210,7 +225,7 @@ mdxRuleTester.run('eslint-plugin-mdx', rule, {
210225
],
211226
});
212227

213-
runFixture('mdx', [
228+
runFixture('*.mdx', [
214229
[
215230
{
216231
column: 33,
@@ -245,6 +260,43 @@ runFixture('mdx', [
245260
],
246261
]);
247262

263+
runFixture('@ota-meshi/eslint-plugin-svelte/*.svelte', [
264+
[
265+
{
266+
column: 5,
267+
endColumn: 13,
268+
endLine: 2,
269+
fix: {
270+
range: [13, 21],
271+
text: 'name =',
272+
},
273+
line: 2,
274+
message: 'Replace `·name·=·` with `name·=`',
275+
messageId: 'replace',
276+
nodeType: null,
277+
ruleId: 'prettier/prettier',
278+
severity: 2,
279+
},
280+
{
281+
column: 4,
282+
endColumn: 7,
283+
endLine: 5,
284+
fix: {
285+
range: [45, 48],
286+
text: '>',
287+
},
288+
line: 5,
289+
message: 'Replace `·>·` with `>`',
290+
messageId: 'replace',
291+
nodeType: null,
292+
ruleId: 'prettier/prettier',
293+
severity: 2,
294+
},
295+
],
296+
]);
297+
298+
runFixture('eslint-plugin-svelte3/*.svelte', [[], []]);
299+
248300
// ------------------------------------------------------------------------------
249301
// Helpers
250302
// ------------------------------------------------------------------------------
@@ -288,12 +340,18 @@ function getPrettierRcJsFilename(dir, file = 'dummy.js') {
288340
return path.resolve(__dirname, `./prettierrc/${dir}/${file}`);
289341
}
290342

291-
async function runFixture(name, asserts) {
343+
/**
344+
*
345+
* @param {string} pattern
346+
* @param {import('eslint').Linter.LintMessage[]} asserts
347+
* @returns {Promise<void>}
348+
*/
349+
async function runFixture(pattern, asserts) {
292350
try {
293-
const results = await eslint.lintFiles(`test/fixtures/${name}.*`);
351+
const results = await eslint.lintFiles([`test/fixtures/${pattern}`]);
294352
return assert.deepStrictEqual(
295-
asserts,
296353
results.map(({ messages }) => messages),
354+
asserts,
297355
);
298356
} catch (err) {
299357
console.error(err);

0 commit comments

Comments
 (0)