forked from antfu/eslint-config
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f8cea6
commit b4d496f
Showing
13 changed files
with
183 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
module.exports = { | ||
extends: [ | ||
'@ow3/eslint-config-vue', | ||
'@unocss', | ||
], | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const CLASS_FIELDS = ['class', 'classname'] | ||
export const AST_NODES_WITH_QUOTES = ['Literal', 'VLiteral'] |
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,3 @@ | ||
import { fileURLToPath } from 'node:url' | ||
|
||
export const distDir = fileURLToPath(new URL('../dist', import.meta.url)) |
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// thank you to @unocss/eslint-plugin | ||
// https://github.com/unocss/unocss/blob/main/packages/eslint-plugin/src/rules/order.ts | ||
import { join } from 'node:path' | ||
import { ESLintUtils } from '@typescript-eslint/utils' | ||
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 { AST_NODES_WITH_QUOTES, CLASS_FIELDS } from '../constants' | ||
|
||
const sortClasses = createSyncFn<(classes: string) => Promise<string>>(join(distDir, 'worker-sort.cjs')) | ||
|
||
export default ESLintUtils.RuleCreator(name => name)({ | ||
name: 'order', | ||
meta: { | ||
type: 'layout', | ||
fixable: 'code', | ||
docs: { | ||
description: 'Order of utilities in class attribute', | ||
recommended: 'warn', | ||
}, | ||
messages: { | ||
'invalid-order': 'Utility classes are not ordered', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
function checkLiteral(node: TSESTree.Literal) { | ||
if (typeof node.value !== 'string' || !node.value.trim()) | ||
return | ||
const input = node.value | ||
const sorted = sortClasses(input).trim() | ||
if (sorted !== input) { | ||
context.report({ | ||
node, | ||
messageId: 'invalid-order', | ||
fix(fixer) { | ||
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) | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
const scriptVisitor: RuleListener = { | ||
JSXAttribute(node) { | ||
if (typeof node.name.name === 'string' && CLASS_FIELDS.includes(node.name.name.toLowerCase()) && node.value) { | ||
if (node.value.type === 'Literal') | ||
checkLiteral(node.value) | ||
} | ||
}, | ||
SvelteAttribute(node: any) { | ||
if (node.key.name === 'class') { | ||
if (node.value?.[0].type === 'SvelteLiteral') | ||
checkLiteral(node.value[0]) | ||
} | ||
}, | ||
} | ||
|
||
const templateBodyVisitor: RuleListener = { | ||
VAttribute(node: any) { | ||
if (node.key.name === 'class') { | ||
if (node.value.type === 'VLiteral') | ||
checkLiteral(node.value) | ||
} | ||
}, | ||
} | ||
|
||
// @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) | ||
} | ||
}, | ||
}) |
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,45 @@ | ||
// thank you to https://github.com/unocss/unocss/blob/main/packages/shared-integration/src/sort-rules.ts | ||
import type { UnoGenerator } from '@unocss/core' | ||
import { collapseVariantGroup, notNull, parseVariantGroup } from '@unocss/core' | ||
|
||
export async function sortRules(rules: string, uno: UnoGenerator) { | ||
const unknown: string[] = [] | ||
|
||
// enable details for variant handlers | ||
if (!uno.config.details) | ||
uno.config.details = true | ||
|
||
// const hasAttributify = !!uno.config.presets.find(i => i.name === '@unocss/preset-attributify') | ||
// const hasVariantGroup = !!uno.config.transformers?.find(i => i.name === '@unocss/transformer-variant-group') | ||
|
||
const expandedResult = parseVariantGroup(rules) // todo read separators from config | ||
rules = expandedResult.expanded | ||
|
||
const result = await Promise.all(rules.split(/\s+/g) | ||
.map(async (i) => { | ||
const token = await uno.parseToken(i) | ||
if (token == null) { | ||
unknown.push(i) | ||
return undefined | ||
} | ||
const variantRank = (token[0][5]?.variantHandlers?.length || 0) * 100_000 | ||
const order = token[0][0] + variantRank | ||
return [order, i] as const | ||
})) | ||
|
||
let sorted = result | ||
.filter(notNull) | ||
.sort((a, b) => { | ||
let result = a[0] - b[0] | ||
if (result === 0) | ||
result = a[1].localeCompare(b[1]) | ||
return result | ||
}) | ||
.map(i => i[1]) | ||
.join(' ') | ||
|
||
if (expandedResult?.prefixes.length) | ||
sorted = collapseVariantGroup(sorted, expandedResult.prefixes) | ||
|
||
return [...unknown, sorted].join(' ').trim() | ||
} |
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,20 @@ | ||
import { loadConfig } from '@unocss/config' | ||
import type { UnoGenerator } from '@unocss/core' | ||
import { createGenerator } from '@unocss/core' | ||
import { runAsWorker } from 'synckit' | ||
import { sortRules } from './sort-rules' | ||
|
||
async function getGenerator() { | ||
const { config, sources } = await loadConfig() | ||
if (!sources.length) | ||
throw new Error('[@ow3/eslint-plugin] No `uno.config.ts` file found. Please create a bug report.') | ||
return createGenerator(config) | ||
} | ||
|
||
let promise: Promise<UnoGenerator<any>> | undefined | ||
|
||
runAsWorker(async (classes: string) => { | ||
promise = promise || getGenerator() | ||
const uno = await promise | ||
return await sortRules(classes, uno) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.