Skip to content

Commit

Permalink
Update ESLint
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed Oct 28, 2023
1 parent 8189af7 commit 35c10af
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
5 changes: 1 addition & 4 deletions examples/eslint/collision.html
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
<div class="m:10 m:20 m:30 m:40@sm m:50@sm">
...
</div>

<div class="m:10 m:20 m:30 m:40@sm m:50@sm">...</div>
3 changes: 1 addition & 2 deletions examples/eslint/order.html
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<div class="font:12 font:24@sm m:32 block font:32@md mb:48">...</div>

<div class="font:12 font:24@sm m:32 block font:32@md mb:48">...</div>
2 changes: 1 addition & 1 deletion examples/eslint/unknow.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<button class="btn">...</button>
<button class="btn">...</button>
2 changes: 1 addition & 1 deletion examples/eslint/validation.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div class="width:">...</div>
<div class="width:">...</div>
15 changes: 9 additions & 6 deletions packages/eslint-plugin/src/utils/are-declarations-equal.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
export default function areDeclarationsEqual(aDeclarations, bDeclarations) {
// 获取对象A和B的所有属性名
const aKeys = Object.keys(aDeclarations);
const bKeys = Object.keys(bDeclarations);
const aKeys = Object.keys(aDeclarations)
const bKeys = Object.keys(bDeclarations)

// 如果属性数量不相等,返回false
if (aKeys.length !== bKeys.length) {
return false;
return false
}

// 检查对象A的属性是否都存在于对象B中
for (const key of aKeys) {
if (!bDeclarations.hasOwnProperty(key)) {
return false;
if (!Object.prototype.hasOwnProperty.call(bDeclarations, key)) {
return false
}
if (aDeclarations[key] !== bDeclarations[key]) {
return false
}
}

// 如果所有属性都匹配,返回true
return true;
return true
}
19 changes: 11 additions & 8 deletions packages/eslint-plugin/src/utils/ast.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* eslint-disable no-case-declarations */
import { Rule } from 'eslint'

const separatorRegEx = /([\t\n\f\r ]+)/

export function extractRangeFromNode(node) {
Expand Down Expand Up @@ -36,19 +39,19 @@ export function extractClassnamesFromValue(classStr) {
if (typeof classStr !== 'string') {
return { classNames: [], whitespaces: [], headSpace: false, tailSpace: false }
}
let parts = classStr.split(separatorRegEx)
const parts = classStr.split(separatorRegEx)
if (parts[0] === '') {
parts.shift()
}
if (parts[parts.length - 1] === '') {
parts.pop()
}
let headSpace = separatorRegEx.test(parts[0])
let tailSpace = separatorRegEx.test(parts[parts.length - 1])
const headSpace = separatorRegEx.test(parts[0])
const tailSpace = separatorRegEx.test(parts[parts.length - 1])
const isClass = (_, i) => (headSpace ? i % 2 !== 0 : i % 2 === 0)
const isNotClass = (_, i) => (headSpace ? i % 2 === 0 : i % 2 !== 0)
let classNames = parts.filter(isClass)
let whitespaces = parts.filter(isNotClass)
const classNames = parts.filter(isClass)
const whitespaces = parts.filter(isNotClass)
return { classNames: classNames, whitespaces: whitespaces, headSpace: headSpace, tailSpace: tailSpace }
}

Expand All @@ -64,7 +67,7 @@ export function extractClassnamesFromValue(classStr) {
* @param {Array} ignoredKeys Optional, set object keys which should not be parsed e.g. for `cva`
* @returns {void}
*/
export function parseNodeRecursive(rootNode, childNode, cb, skipConditional = false, isolate = false, ignoredKeys = [], context = null) {
export function parseNodeRecursive(rootNode, childNode, cb, skipConditional = false, isolate = false, ignoredKeys = [], context: Rule.RuleContext = null) {
// TODO allow vue non litteral
let originalClassNamesValue
let classNames
Expand Down Expand Up @@ -161,7 +164,7 @@ export function parseNodeRecursive(rootNode, childNode, cb, skipConditional = fa
originalClassNamesValue = childNode.value.raw
start = childNode.range[0]
end = childNode.range[1]
const txt = context?.getSourceCode()?.getText(childNode) ?? ''
const txt = context?.sourceCode.getText(childNode) ?? ''
prefix = getTemplateElementPrefix(txt, originalClassNamesValue)
suffix = getTemplateElementSuffix(txt, originalClassNamesValue)
break
Expand Down Expand Up @@ -195,7 +198,7 @@ export function getTemplateElementSuffix(text, raw) {
export function getTemplateElementBody(text, prefix, suffix) {
let arr = text.split(prefix)
arr.shift()
let body = arr.join(prefix)
const body = arr.join(prefix)
arr = body.split(suffix)
arr.pop()
return arr.join(suffix)
Expand Down

0 comments on commit 35c10af

Please sign in to comment.