-
-
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.
feat(esling): new
@unocss/blocklist
rule, #3082
- Loading branch information
Showing
12 changed files
with
160 additions
and
37 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,10 @@ | ||
{ | ||
"root": true, | ||
"extends": [ | ||
"@antfu", | ||
"plugin:@unocss/recommended" | ||
] | ||
], | ||
"rules": { | ||
"@unocss/blocklist": "error" | ||
} | ||
} |
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,6 @@ | ||
import { join } from 'node:path' | ||
import { createSyncFn } from 'synckit' | ||
import { distDir } from '../dirs' | ||
import type { run } from '../worker' | ||
|
||
export const syncAction = createSyncFn(join(distDir, 'worker.cjs')) as typeof run |
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,73 @@ | ||
import { ESLintUtils } from '@typescript-eslint/utils' | ||
import type { RuleListener } from '@typescript-eslint/utils/ts-eslint' | ||
import type { TSESTree } from '@typescript-eslint/types' | ||
import { CLASS_FIELDS } from '../constants' | ||
import { syncAction } from './_' | ||
|
||
export default ESLintUtils.RuleCreator(name => name)({ | ||
name: 'blocklist', | ||
meta: { | ||
type: 'problem', | ||
fixable: 'code', | ||
docs: { | ||
description: 'Utilities in UnoCSS blocklist', | ||
recommended: 'recommended', | ||
}, | ||
messages: { | ||
'in-blocklist': 'Utility \'{{ name }}\' is in blocklist', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const checkLiteral = (node: TSESTree.Literal) => { | ||
if (typeof node.value !== 'string' || !node.value.trim()) | ||
return | ||
const input = node.value | ||
const blocked = syncAction('blocklist', input, context.filename) | ||
blocked.forEach((i) => { | ||
context.report({ | ||
node, | ||
messageId: 'in-blocklist', | ||
data: { | ||
name: i, | ||
}, | ||
}) | ||
}) | ||
} | ||
|
||
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
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 was deleted.
Oops, something went wrong.
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,44 @@ | ||
import { loadConfig } from '@unocss/config' | ||
import type { UnoGenerator } from '@unocss/core' | ||
import { createGenerator } from '@unocss/core' | ||
import { runAsWorker } from 'synckit' | ||
import { sortRules } from '../../shared-integration/src/sort-rules' | ||
|
||
let promise: Promise<UnoGenerator<any>> | undefined | ||
|
||
async function _getGenerator() { | ||
const { config, sources } = await loadConfig() | ||
if (!sources.length) | ||
throw new Error('[@unocss/eslint-plugin] No config file found, create a `uno.config.ts` file in your project root and try again.') | ||
return createGenerator(config) | ||
} | ||
|
||
export async function getGenerator() { | ||
promise = promise || _getGenerator() | ||
return await promise | ||
} | ||
|
||
async function actionSort(classes: string) { | ||
return await sortRules(classes, await getGenerator()) | ||
} | ||
|
||
async function actionBlocklist(classes: string, id?: string) { | ||
const uno = await getGenerator() | ||
const extracted = await uno.applyExtractors(classes, id) | ||
return [...extracted.values()].filter(i => uno.isBlocked(i)) | ||
} | ||
|
||
export function run(action: 'sort', classes: string): string | ||
export function run(action: 'blocklist', classes: string, id?: string): string[] | ||
export function run(action: string, ...args: any[]): any { | ||
switch (action) { | ||
case 'sort': | ||
// @ts-expect-error cast | ||
return actionSort(...args) | ||
case 'blocklist': | ||
// @ts-expect-error cast | ||
return actionBlocklist(...args) | ||
} | ||
} | ||
|
||
runAsWorker(run as any) |