-
-
Notifications
You must be signed in to change notification settings - Fork 778
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…3506) Co-authored-by: Jacob Bowdoin <7559478+jacob-8@users.noreply.github.com>
- Loading branch information
1 parent
f3944de
commit 25a938f
Showing
9 changed files
with
477 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import orderAttributify from './rules/order-attributify' | ||
import order from './rules/order' | ||
import blocklist from './rules/blocklist' | ||
import enforceClassCompile from './rules/enforce-class-compile' | ||
|
||
export const plugin = { | ||
rules: { | ||
order, | ||
'order-attributify': orderAttributify, | ||
blocklist, | ||
'enforce-class-compile': enforceClassCompile, | ||
}, | ||
} |
125 changes: 125 additions & 0 deletions
125
packages/eslint-plugin/src/rules/enforce-class-compile.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import type { ESLintUtils } from '@typescript-eslint/utils' | ||
import type { ReportFixFunction, RuleListener } from '@typescript-eslint/utils/ts-eslint' | ||
import type { TSESTree } from '@typescript-eslint/types' | ||
import type { AST } from 'vue-eslint-parser' | ||
import { createRule } from './_' | ||
|
||
export default createRule<[{ prefix: string, enableFix: boolean }], 'missing'>({ | ||
name: 'enforce-class-compile', | ||
meta: { | ||
type: 'problem', | ||
fixable: 'code', | ||
docs: { | ||
description: 'Enforce class compilation', | ||
}, | ||
messages: { | ||
missing: 'prefix: `{{prefix}}` is missing', | ||
}, | ||
schema: [{ | ||
type: 'object', | ||
properties: { | ||
prefix: { | ||
type: 'string', | ||
}, | ||
enableFix: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}], | ||
}, | ||
defaultOptions: [{ prefix: ':uno:', enableFix: true }], | ||
create(context, [mergedOptions]) { | ||
const CLASS_COMPILE_PREFIX = `${mergedOptions.prefix} ` | ||
const ENABLE_FIX = mergedOptions.enableFix | ||
|
||
function report({ node, fix }: { node: AST.VNode | AST.ESLintNode, fix: ReportFixFunction }) { | ||
context.report({ | ||
node: node as unknown as TSESTree.Node, | ||
loc: node.loc, | ||
messageId: 'missing', | ||
data: { prefix: CLASS_COMPILE_PREFIX.trim() }, | ||
fix: (...args) => ENABLE_FIX ? fix(...args) : null, | ||
}) | ||
} | ||
|
||
const scriptVisitor: RuleListener = { | ||
JSXAttribute(_node) { | ||
// todo: add support | NEED HELP | ||
}, | ||
SvelteAttribute(_node: any) { | ||
// todo: add support | NEED HELP | ||
}, | ||
} | ||
|
||
const reportClassList = (node: AST.VNode | AST.ESLintNode, classList: string) => { | ||
if (classList.startsWith(CLASS_COMPILE_PREFIX)) | ||
return | ||
|
||
report({ | ||
node, | ||
fix(fixer) { | ||
return fixer.replaceTextRange([node.range[0] + 1, node.range[1] - 1], `${CLASS_COMPILE_PREFIX}${classList}`) | ||
}, | ||
}) | ||
} | ||
|
||
const templateBodyVisitor: RuleListener = { | ||
[`VAttribute[key.name=class]`](attr: AST.VAttribute) { | ||
const valueNode = attr.value | ||
if (!valueNode || !valueNode.value) | ||
return | ||
|
||
reportClassList(valueNode, valueNode.value) | ||
}, | ||
[`VAttribute[key.argument.name=class] VExpressionContainer Literal:not(ConditionalExpression .test Literal):not(Property .value Literal)`]( | ||
literal: AST.ESLintStringLiteral, | ||
) { | ||
if (!literal.value || typeof literal.value !== 'string') | ||
return | ||
|
||
reportClassList(literal, literal.value) | ||
}, | ||
[`VAttribute[key.argument.name=class] VExpressionContainer TemplateElement`]( | ||
templateElement: AST.ESLintTemplateElement, | ||
) { | ||
if (!templateElement.value.raw) | ||
return | ||
|
||
reportClassList(templateElement, templateElement.value.raw) | ||
}, | ||
[`VAttribute[key.argument.name=class] VExpressionContainer Property`]( | ||
property: AST.ESLintProperty, | ||
) { | ||
if (property.key.type !== 'Identifier') | ||
return | ||
|
||
const classListString = property.key.name | ||
if (classListString.startsWith(CLASS_COMPILE_PREFIX)) | ||
return | ||
|
||
report({ | ||
node: property.key, | ||
fix(fixer) { | ||
let replacePropertyKeyText = `'${CLASS_COMPILE_PREFIX}${classListString}'` | ||
|
||
if (property.shorthand) | ||
replacePropertyKeyText = `${replacePropertyKeyText}: ${classListString}` | ||
|
||
return fixer.replaceTextRange(property.key.range, replacePropertyKeyText) | ||
}, | ||
}) | ||
}, | ||
} | ||
|
||
// @ts-expect-error missing-types | ||
if (context.parserServices == null || context.parserServices.defineTemplateBodyVisitor == null) { | ||
return scriptVisitor | ||
} | ||
else { | ||
// For Vue | ||
// @ts-expect-error missing-types | ||
return context.parserServices?.defineTemplateBodyVisitor(templateBodyVisitor, scriptVisitor) | ||
} | ||
}, | ||
}) as any as ESLintUtils.RuleWithMeta<[], ''> |
Oops, something went wrong.