Skip to content

Commit

Permalink
feat(eslint-plugin): add svelte support (#2417)
Browse files Browse the repository at this point in the history
  • Loading branch information
devunt committed Apr 8, 2023
1 parent 94bdb27 commit c4ebf70
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/eslint-plugin/fixtures/src/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1 class="text-3xl hover:text-red m1 font-bold underline">Hello world!</h1>
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const CLASS_FIELDS = ['class', 'classname']
export const AST_NODES_WITH_QUOTES = ['Literal', 'VLiteral']
19 changes: 14 additions & 5 deletions packages/eslint-plugin/src/rules/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createSyncFn } from 'synckit'
import type { RuleListener } from '@typescript-eslint/utils/dist/ts-eslint'
import type { TSESTree } from '@typescript-eslint/types'
import { distDir } from '../dirs'
import { CLASS_FIELDS } from '../constants'
import { AST_NODES_WITH_QUOTES, CLASS_FIELDS } from '../constants'

const sortClasses = createSyncFn<(classes: string) => Promise<string>>(join(distDir, 'worker-sort.cjs'))

Expand All @@ -25,16 +25,19 @@ export default ESLintUtils.RuleCreator(name => name)({
defaultOptions: [],
create(context) {
function checkLiteral(node: TSESTree.Literal) {
if (typeof node.value !== 'string')
if (typeof node.value !== 'string' || !node.value.trim())
return
const input = node.value as string
const sorted = sortClasses(input)
const input = node.value
const sorted = sortClasses(input).trim()
if (sorted !== input) {
context.report({
node,
messageId: 'invalid-order',
fix(fixer) {
return fixer.replaceText(node, `"${sorted.trim()}"`)
if (AST_NODES_WITH_QUOTES.includes(node.type))
return fixer.replaceTextRange([node.range[0] + 1, node.range[1] - 1], sorted)
else
return fixer.replaceText(node, sorted)
},
})
}
Expand All @@ -47,6 +50,12 @@ export default ESLintUtils.RuleCreator(name => name)({
checkLiteral(node.value)
}
},
SvelteAttribute(node: any) {
if (node.key.name === 'class') {
if (node.value?.[0].type === 'SvelteLiteral')
checkLiteral(node.value[0])
}
},
}

const templateBodyVisitor: RuleListener = {
Expand Down

0 comments on commit c4ebf70

Please sign in to comment.