From 97ae4b070bd355279b8520c8d95046779a656c6d Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Thu, 13 Jan 2022 10:34:30 +0900 Subject: [PATCH] Support for svelte v3.46.1 --- package.json | 4 +- src/rules/html-quotes.ts | 14 ++- src/rules/indent-helpers/svelte.ts | 10 +- src/rules/max-attributes-per-line.ts | 1 + src/rules/mustache-spacing.ts | 2 + src/rules/no-useless-mustaches.ts | 5 +- src/utils/ast-utils.ts | 4 +- .../html-quotes/invalid/test01-errors.json | 7 +- .../html-quotes/invalid/test01-input.svelte | 2 + .../html-quotes/invalid/test01-output.svelte | 2 + .../invalid/style-directive01-errors.json | 107 ++++++++++++++++++ .../invalid/style-directive01-input.svelte | 48 ++++++++ .../invalid/style-directive01-output.svelte | 48 ++++++++ 13 files changed, 242 insertions(+), 12 deletions(-) create mode 100644 tests/fixtures/rules/indent/invalid/style-directive01-errors.json create mode 100644 tests/fixtures/rules/indent/invalid/style-directive01-input.svelte create mode 100644 tests/fixtures/rules/indent/invalid/style-directive01-output.svelte diff --git a/package.json b/package.json index 3b7345b36..56492c957 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "debug": "^4.3.1", "eslint-utils": "^3.0.0", "sourcemap-codec": "^1.4.8", - "svelte-eslint-parser": "^0.10.0" + "svelte-eslint-parser": "^0.11.0" }, "peerDependencies": { "eslint": "^7.0.0 || ^8.0.0-0", @@ -118,7 +118,7 @@ "semver": "^7.3.5", "stylelint": "^14.0.0", "stylelint-config-standard": "^24.0.0", - "svelte": "^3.37.0", + "svelte": "^3.46.1", "svelte-adapter-ghpages": "0.0.2", "ts-node": "^10.0.0", "typescript": "^4.5.2", diff --git a/src/rules/html-quotes.ts b/src/rules/html-quotes.ts index 37952b6e3..024eb8eff 100644 --- a/src/rules/html-quotes.ts +++ b/src/rules/html-quotes.ts @@ -148,17 +148,17 @@ export default createRule("html-quotes", { } /** Verify for standard attribute */ - function verifyForValues(attr: AST.SvelteAttribute) { + function verifyForValues( + attr: AST.SvelteAttribute | AST.SvelteStyleDirective, + ) { const quoteAndRange = getAttributeValueQuoteAndRange(attr, sourceCode) verifyQuote(preferQuote, quoteAndRange) } /** Verify for dynamic attribute */ function verifyForDynamicMustacheTag( - attr: AST.SvelteAttribute, - valueNode: AST.SvelteMustacheTag & { - kind: "text" - }, + attr: AST.SvelteAttribute | AST.SvelteStyleDirective, + valueNode: AST.SvelteMustacheTagText, ) { const quoteAndRange = getAttributeValueQuoteAndRange(attr, sourceCode) const text = sourceCode.text.slice(...valueNode.range) @@ -192,7 +192,9 @@ export default createRule("html-quotes", { } return { - SvelteAttribute(node) { + "SvelteAttribute, SvelteStyleDirective"( + node: AST.SvelteAttribute | AST.SvelteStyleDirective, + ) { if ( node.value.length === 1 && node.value[0].type === "SvelteMustacheTag" diff --git a/src/rules/indent-helpers/svelte.ts b/src/rules/indent-helpers/svelte.ts index 184555439..b515bf7d0 100644 --- a/src/rules/indent-helpers/svelte.ts +++ b/src/rules/indent-helpers/svelte.ts @@ -87,6 +87,7 @@ export function defineVisitor(context: IndentContext): NodeListener { node: | AST.SvelteAttribute | AST.SvelteDirective + | AST.SvelteStyleDirective | AST.SvelteSpecialDirective, ) { const keyToken = sourceCode.getFirstToken(node) @@ -102,7 +103,11 @@ export function defineVisitor(context: IndentContext): NodeListener { ) { offsets.setOffsetToken(valueStartToken, 1, keyToken) - const values = node.type === "SvelteAttribute" ? node.value : [] + const values = + node.type === "SvelteAttribute" || + node.type === "SvelteStyleDirective" + ? node.value + : [] // process quoted let processedValues = false if (valueStartToken.type === "Punctuator") { @@ -134,6 +139,9 @@ export function defineVisitor(context: IndentContext): NodeListener { SvelteDirective(node: AST.SvelteDirective) { visitor.SvelteAttribute(node) }, + SvelteStyleDirective(node: AST.SvelteStyleDirective) { + visitor.SvelteAttribute(node) + }, SvelteSpecialDirective(node: AST.SvelteSpecialDirective) { visitor.SvelteAttribute(node) }, diff --git a/src/rules/max-attributes-per-line.ts b/src/rules/max-attributes-per-line.ts index a55eb4686..b3e426c60 100644 --- a/src/rules/max-attributes-per-line.ts +++ b/src/rules/max-attributes-per-line.ts @@ -72,6 +72,7 @@ export default createRule("max-attributes-per-line", { attribute.type === "SvelteAttribute" || attribute.type === "SvelteShorthandAttribute" || attribute.type === "SvelteDirective" || + attribute.type === "SvelteStyleDirective" || attribute.type === "SvelteSpecialDirective" ) { name = sourceCode.text.slice(...attribute.key.range!) diff --git a/src/rules/mustache-spacing.ts b/src/rules/mustache-spacing.ts index 999d970f8..759e6201a 100644 --- a/src/rules/mustache-spacing.ts +++ b/src/rules/mustache-spacing.ts @@ -193,6 +193,8 @@ export default createRule("mustache-spacing", { let option: OptionValue if (node.parent.type === "SvelteAttribute") { option = options.attributesAndProps + } else if (node.parent.type === "SvelteStyleDirective") { + option = options.directiveExpressions } else { option = options.textExpressions } diff --git a/src/rules/no-useless-mustaches.ts b/src/rules/no-useless-mustaches.ts index 80b668076..1e3c52397 100644 --- a/src/rules/no-useless-mustaches.ts +++ b/src/rules/no-useless-mustaches.ts @@ -126,7 +126,10 @@ export default createRule("no-useless-mustaches", { const unescaped = text.replace(/\\([\s\S])/g, "$1") - if (node.parent.type === "SvelteAttribute") { + if ( + node.parent.type === "SvelteAttribute" || + node.parent.type === "SvelteStyleDirective" + ) { const div = sourceCode.text.slice( node.parent.key.range[1], node.parent.value[0].range[0], diff --git a/src/utils/ast-utils.ts b/src/utils/ast-utils.ts index e95f84473..0e3cadce6 100644 --- a/src/utils/ast-utils.ts +++ b/src/utils/ast-utils.ts @@ -239,6 +239,7 @@ export function getAttributeValueQuoteAndRange( attr: | SvAST.SvelteAttribute | SvAST.SvelteDirective + | SvAST.SvelteStyleDirective | SvAST.SvelteSpecialDirective, sourceCode: SourceCode, ): QuoteAndRange | null { @@ -372,10 +373,11 @@ function getAttributeValueRangeTokens( attr: | SvAST.SvelteAttribute | SvAST.SvelteDirective + | SvAST.SvelteStyleDirective | SvAST.SvelteSpecialDirective, sourceCode: SourceCode, ) { - if (attr.type === "SvelteAttribute") { + if (attr.type === "SvelteAttribute" || attr.type === "SvelteStyleDirective") { if (!attr.value.length) { return null } diff --git a/tests/fixtures/rules/html-quotes/invalid/test01-errors.json b/tests/fixtures/rules/html-quotes/invalid/test01-errors.json index 6d85f600a..503e27eaa 100644 --- a/tests/fixtures/rules/html-quotes/invalid/test01-errors.json +++ b/tests/fixtures/rules/html-quotes/invalid/test01-errors.json @@ -21,7 +21,12 @@ }, { "message": "Unexpected to be enclosed by any quotes.", - "line": 16, + "line": 14, + "column": 20 + }, + { + "message": "Unexpected to be enclosed by any quotes.", + "line": 18, "column": 10 } ] diff --git a/tests/fixtures/rules/html-quotes/invalid/test01-input.svelte b/tests/fixtures/rules/html-quotes/invalid/test01-input.svelte index f02a10172..e7d41007f 100644 --- a/tests/fixtures/rules/html-quotes/invalid/test01-input.svelte +++ b/tests/fixtures/rules/html-quotes/invalid/test01-input.svelte @@ -10,6 +10,8 @@ + + {name} dances. diff --git a/tests/fixtures/rules/html-quotes/invalid/test01-output.svelte b/tests/fixtures/rules/html-quotes/invalid/test01-output.svelte index 580ecbd4b..dc479bfdd 100644 --- a/tests/fixtures/rules/html-quotes/invalid/test01-output.svelte +++ b/tests/fixtures/rules/html-quotes/invalid/test01-output.svelte @@ -10,6 +10,8 @@ + + {name} dances. diff --git a/tests/fixtures/rules/indent/invalid/style-directive01-errors.json b/tests/fixtures/rules/indent/invalid/style-directive01-errors.json new file mode 100644 index 000000000..54003dbfd --- /dev/null +++ b/tests/fixtures/rules/indent/invalid/style-directive01-errors.json @@ -0,0 +1,107 @@ +[ + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 8, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 13, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 14, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 15, + "column": 1 + }, + { + "message": "Expected indentation of 6 spaces but found 0 spaces.", + "line": 16, + "column": 1 + }, + { + "message": "Expected indentation of 8 spaces but found 0 spaces.", + "line": 17, + "column": 1 + }, + { + "message": "Expected indentation of 6 spaces but found 0 spaces.", + "line": 18, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 19, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 24, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 25, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 26, + "column": 1 + }, + { + "message": "Expected indentation of 6 spaces but found 0 spaces.", + "line": 27, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 28, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 33, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 34, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 35, + "column": 1 + }, + { + "message": "Expected indentation of 2 spaces but found 0 spaces.", + "line": 40, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 41, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 42, + "column": 1 + }, + { + "message": "Expected indentation of 6 spaces but found 0 spaces.", + "line": 43, + "column": 1 + }, + { + "message": "Expected indentation of 4 spaces but found 0 spaces.", + "line": 44, + "column": 1 + } +] diff --git a/tests/fixtures/rules/indent/invalid/style-directive01-input.svelte b/tests/fixtures/rules/indent/invalid/style-directive01-input.svelte new file mode 100644 index 000000000..4f49b6d4f --- /dev/null +++ b/tests/fixtures/rules/indent/invalid/style-directive01-input.svelte @@ -0,0 +1,48 @@ + + + + +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ + diff --git a/tests/fixtures/rules/indent/invalid/style-directive01-output.svelte b/tests/fixtures/rules/indent/invalid/style-directive01-output.svelte new file mode 100644 index 000000000..b0312a8bd --- /dev/null +++ b/tests/fixtures/rules/indent/invalid/style-directive01-output.svelte @@ -0,0 +1,48 @@ + + + + +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +