Skip to content

Commit

Permalink
Update ESLint
Browse files Browse the repository at this point in the history
  • Loading branch information
0Miles committed Nov 11, 2023
1 parent 802fd0b commit 124f2d9
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 18 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ packages/extractor/**/*master.css.*
packages/extractor.vite/tests/vite/index.html
packages/extractor.vite/tests/vite/master.css.*
packages/extractor.webpack/tests/webpack/src/index.html
packages/extractor.webpack/tests/webpack/src/master.css.*
packages/extractor.webpack/tests/webpack/src/master.css.*

**/*/cache
11 changes: 8 additions & 3 deletions packages/css/src/functions/reorder-for-readable-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import { MasterCSS } from '../core'
/**
* Sorts classes in a consistent order
* @param classes
* @param config
* @param options
* @returns consistent classes
*/
export default function reorderForReadableClasses(classes: string[], config?: Config) {
export default function reorderForReadableClasses(classes: string[], options?: { css?: MasterCSS, config?: Config }) {
if (!classes.length) return
const css = new MasterCSS(config)
let css: MasterCSS
if (options?.css) {
css = options?.css
} else {
css = new MasterCSS(options?.config)
}
for (const eachClass of classes) {
css.add(eachClass)
}
Expand Down
4 changes: 3 additions & 1 deletion packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"dependencies": {
"@master/css": "workspace:^",
"@master/css-validator": "workspace:^",
"explore-config": "^2.5.19"
"explore-config": "^2.5.19",
"synckit": "^0.8.5"
},
"devDependencies": {
"@angular-eslint/template-parser": "^16.2.0",
Expand All @@ -52,6 +53,7 @@
"eslint-plugin-markdown": "^3.0.1",
"eslint-plugin-mdx": "^2.2.0",
"svelte-eslint-parser": "^0.33.1",
"ts-node": "^10.9.1",
"vue-eslint-parser": "^9.3.1"
}
}
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/dirs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const distDir = __dirname
6 changes: 6 additions & 0 deletions packages/eslint-plugin/src/rules/_.ts
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.js')) as typeof run
8 changes: 2 additions & 6 deletions packages/eslint-plugin/src/rules/class-collision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as astUtil from '../utils/ast'
import areDeclarationsEqual from '../utils/are-declarations-equal'
import defineVisitors from '../utils/define-visitors'
import resolveContext from '../utils/resolve-context'
import { createValidRules } from '@master/css-validator'
import { Rule } from 'eslint'
import { syncAction } from './_'

export default {
meta: {
Expand Down Expand Up @@ -50,11 +50,7 @@ export default {
const sourceCodeLines = sourceCode.lines
const nodeStartLine = node.loc.start.line
const nodeEndLine = node.loc.end.line
const ruleOfClass = {}
classNames
.forEach(eachClassName => {
ruleOfClass[eachClassName] = createValidRules(eachClassName, { config })[0]
})
const ruleOfClass = syncAction('ruleOfClass', classNames, config)

for (let i = 0; i < classNames.length; i++) {
const className = classNames[i]
Expand Down
5 changes: 2 additions & 3 deletions packages/eslint-plugin/src/rules/class-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import * as astUtil from '../utils/ast'
import defineVisitors from '../utils/define-visitors'
import resolveContext from '../utils/resolve-context'
import { reorderForReadableClasses } from '@master/css'
import { Rule } from 'eslint'
import { syncAction } from './_'

export default {
meta: {
Expand Down Expand Up @@ -143,8 +143,7 @@ export default {
return
}

let orderedClassNames = reorderForReadableClasses(classNames, config)
.filter(eachOrderedClassName => classNames.includes(eachOrderedClassName))
let orderedClassNames = syncAction('sort', classNames, config)

orderedClassNames = classNames.filter(x => !orderedClassNames.includes(x))
.concat(orderedClassNames)
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/class-validation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as astUtil from '../utils/ast'
import defineVisitors from '../utils/define-visitors'
import resolveContext from '../utils/resolve-context'
import { validate } from '@master/css-validator'
import { Rule } from 'eslint'
import { syncAction } from './_'

export default {
meta: {
Expand Down Expand Up @@ -51,7 +51,7 @@ export default {
const nodeStartLine = node.loc.start.line
const nodeEndLine = node.loc.end.line
for (const className of classNames) {
const { isMasterCSS, errors } = validate(className, { config })
const { isMasterCSS, errors } = syncAction('validate', className, config)
if (errors.length > 0) {
for (const error of errors) {
if (isMasterCSS) {
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin/src/utils/resolve-context.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import settings from '../settings'
import exploreConfig from 'explore-config'

export default function resolveContext(context) {
const resolvedSettings = Object.assign(settings, context.settings?.['@master/css'])
const config = resolvedSettings?.config
return {
settings: resolvedSettings,
options: context.options[0] || {},
config: typeof config === 'object' ? config : exploreConfig(resolvedSettings?.config || '')
config: resolvedSettings?.config
}
}
62 changes: 62 additions & 0 deletions packages/eslint-plugin/src/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import process from 'node:process'
import exploreConfig from 'explore-config'
import { runAsWorker } from 'synckit'
import MasterCSS, { Config, reorderForReadableClasses } from '@master/css'
import { validate, createValidRules } from '@master/css-validator'
import { Rule } from '@master/css'

let masterCSS: MasterCSS
let masterConfig: string | Config

// bypass icon rules in ESLint
process.env.ESLINT ||= 'true'

function _getMasterCSS(config: string | Config) {
console.log('new master css')
return new MasterCSS(typeof config === 'object' ? config : exploreConfig(config || ''))
}

export function getMasterCSS(config: string | Config) {
if (!masterCSS || masterConfig !== config) {
masterCSS = _getMasterCSS(config)
masterConfig = config
}
return masterCSS
}

function actionSort(classNames: string[], config: string | Config) {
return reorderForReadableClasses(classNames, { css: getMasterCSS(config) })
.filter((eachOrderedClassName: string) => classNames.includes(eachOrderedClassName))
}

function actionValidate(className: string, config: string | Config) {
return validate(className, { css: getMasterCSS(config) })
}

function actionRuleOfClass(classNames: string[], config: string | Config) {
const ruleOfClass = {}
classNames
.forEach(eachClassName => {
ruleOfClass[eachClassName] = createValidRules(eachClassName, { css: getMasterCSS(config) })[0]
})
return ruleOfClass
}

export function run(action: 'sort', classNames: string[], config: string | Config): string[]
export function run(action: 'validate', className: string, config: string | Config): {isMasterCSS: boolean;errors: SyntaxError[]}
export function run(action: 'ruleOfClass', className: string, config: string | Config): Rule
export function run(action: string, ...args: any[]): any {
switch (action) {
case 'sort':
// @ts-expect-error cast
return actionSort(...args)
case 'validate':
// @ts-expect-error cast
return actionValidate(...args)
case 'ruleOfClass':
// @ts-expect-error cast
return actionRuleOfClass(...args)
}
}

runAsWorker(run as any)
86 changes: 86 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 124f2d9

Please sign in to comment.