Skip to content

Commit

Permalink
feat: support svelte out of box
Browse files Browse the repository at this point in the history
close #472, close #482
  • Loading branch information
JounQin committed Jun 30, 2022
1 parent 928a60f commit 9128f7b
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 163 deletions.
26 changes: 20 additions & 6 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,26 @@ module.exports = {

let inferParserToBabel = parserBlocklist.includes(inferredParser);

if (
switch (inferredParser) {
// it could be processed by `@graphql-eslint/eslint-plugin` or `eslint-plugin-graphql`
inferredParser === 'graphql' &&
// for `eslint-plugin-graphql`, see https://github.com/apollographql/eslint-plugin-graphql/blob/master/src/index.js#L416
source.startsWith('ESLintPluginGraphQLFile`')
) {
inferParserToBabel = true;
case 'graphql': {
if (
// for `eslint-plugin-graphql`, see https://github.com/apollographql/eslint-plugin-graphql/blob/master/src/index.js#L416
source.startsWith('ESLintPluginGraphQLFile`')
) {
inferParserToBabel = true;
}
break;
}
// it could be processed by `@ota-meshi/eslint-plugin-svelte`, `eslint-plugin-svelte` or `eslint-plugin-svelte3`
case 'svelte': {
// The `source` would be modified by `eslint-plugin-svelte3`
if (!context.parserPath.includes('svelte-eslint-parser')) {
// We do not support `eslint-plugin-svelte3`,
// the users should run `prettier` on `.svelte` files manually
return;
}
}
}

if (inferParserToBabel) {
Expand All @@ -223,6 +236,7 @@ module.exports = {
'html',
'mdx',
'angular',
'svelte',
];
if (parserBlocklist.includes(inferredParser)) {
return;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
"@changesets/changelog-github": "^0.4.5",
"@changesets/cli": "^2.23.0",
"@graphql-eslint/eslint-plugin": "^2.5.0",
"@ota-meshi/eslint-plugin-svelte": "^0.34.1",
"@typescript-eslint/parser": "^5.29.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.5.0",
"eslint-mdx": "^1.17.0",
"eslint-plugin-eslint-plugin": "^4.3.0",
Expand All @@ -60,6 +60,7 @@
"mocha": "^9.2.2",
"patch-package": "^6.4.7",
"prettier": "^2.7.1",
"svelte": "^3.48.0",
"vue-eslint-parser": "^8.3.0"
},
"resolutions": {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/@ota-meshi/eslint-plugin-svelte/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let name = 'world';
</script>

<h1>Hello {name}!</h1>
5 changes: 5 additions & 0 deletions test/fixtures/eslint-plugin-svelte3/App.named-blocks.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let name = 'world';
</script>

<h1>Hello {name}!</h1>
5 changes: 5 additions & 0 deletions test/fixtures/eslint-plugin-svelte3/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let name = 'world';
</script>

<h1>Hello {name}!</h1>
70 changes: 64 additions & 6 deletions test/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ const eslint = new ESLint({
'mdx/code-block': true,
},
},
{
files: '**/eslint-plugin-svelte3/*.svelte',
plugins: ['svelte3'],
processor: 'svelte3/svelte3',
},
{
files: '**/eslint-plugin-svelte3/*.named-blocks.svelte',
settings: {
'svelte3/named-blocks': true,
},
},
{
files: '**/@ota-meshi/eslint-plugin-svelte/*.svelte',
extends: ['plugin:@ota-meshi/svelte/recommended'],
},
],
},
useEslintrc: false,
Expand Down Expand Up @@ -158,8 +173,8 @@ atGraphqlEslintRuleTester.run('@graphql-eslint/eslint-plugin', rule, {
],
});

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

runFixture('mdx', [
runFixture('*.mdx', [
[
{
column: 33,
Expand Down Expand Up @@ -245,6 +260,43 @@ runFixture('mdx', [
],
]);

runFixture('@ota-meshi/eslint-plugin-svelte/*.svelte', [
[
{
column: 5,
endColumn: 13,
endLine: 2,
fix: {
range: [13, 21],
text: 'name =',
},
line: 2,
message: 'Replace `·name·=·` with `name·=`',
messageId: 'replace',
nodeType: null,
ruleId: 'prettier/prettier',
severity: 2,
},
{
column: 4,
endColumn: 7,
endLine: 5,
fix: {
range: [45, 48],
text: '>',
},
line: 5,
message: 'Replace `·>·` with `>`',
messageId: 'replace',
nodeType: null,
ruleId: 'prettier/prettier',
severity: 2,
},
],
]);

runFixture('eslint-plugin-svelte3/*.svelte', [[], []]);

// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -288,12 +340,18 @@ function getPrettierRcJsFilename(dir, file = 'dummy.js') {
return path.resolve(__dirname, `./prettierrc/${dir}/${file}`);
}

async function runFixture(name, asserts) {
/**
*
* @param {string} pattern
* @param {import('eslint').Linter.LintMessage[]} asserts
* @returns {Promise<void>}
*/
async function runFixture(pattern, asserts) {
try {
const results = await eslint.lintFiles(`test/fixtures/${name}.*`);
const results = await eslint.lintFiles([`test/fixtures/${pattern}`]);
return assert.deepStrictEqual(
asserts,
results.map(({ messages }) => messages),
asserts,
);
} catch (err) {
console.error(err);
Expand Down
Loading

0 comments on commit 9128f7b

Please sign in to comment.