Skip to content

Commit

Permalink
fix: incompatible with ESLint v8.40 in astro/semi rule (#201)
Browse files Browse the repository at this point in the history
* fix: incompatible with ESLint v8.40 in `astro/semi` rule

* Create quiet-lobsters-compete.md

* fix
  • Loading branch information
ota-meshi authored May 8, 2023
1 parent eda1ee7 commit cafd97c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 75 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-lobsters-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-astro": patch
---

fix: incompatible with ESLint v8.40 in `astro/semi` rule
152 changes: 77 additions & 75 deletions src/rules/semi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,85 +27,87 @@ export default createRule("semi", {

return coreRule.create(
newProxy(context, {
getSourceCode() {
if (sourceCodeWrapper) {
return sourceCodeWrapper
}
const sourceCode = context.getSourceCode()
getSourceCode,
get sourceCode() {
return getSourceCode()
},
}),
)

/** Transforms token */
function transformToken(
token: AST.Token | AST.Comment,
): AST.Token | AST.Comment {
return token.value === "---"
? newProxy(token, { value: "___" })
: token
}
/** Get source code wrapper instance */
function getSourceCode() {
if (sourceCodeWrapper) {
return sourceCodeWrapper
}
const sourceCode = context.getSourceCode()

return (sourceCodeWrapper = newProxy(sourceCode, {
/* eslint-disable @typescript-eslint/unbound-method -- ignore */
getFirstToken: wrapGetTokenFunction(sourceCode.getFirstToken),
getFirstTokens: wrapGetTokensFunction(sourceCode.getFirstTokens),
getFirstTokenBetween: wrapGetTokenFunction(
sourceCode.getFirstTokenBetween,
),
getFirstTokensBetween: wrapGetTokensFunction(
sourceCode.getFirstTokensBetween,
),
getLastToken: wrapGetTokenFunction(sourceCode.getLastToken),
getLastTokens: wrapGetTokensFunction(sourceCode.getLastTokens),
getLastTokenBetween: wrapGetTokenFunction(
sourceCode.getLastTokenBetween,
),
getLastTokensBetween: wrapGetTokensFunction(
sourceCode.getLastTokensBetween,
),
getTokenBefore: wrapGetTokenFunction(sourceCode.getTokenBefore),
getTokensBefore: wrapGetTokensFunction(sourceCode.getTokensBefore),
getTokenAfter: wrapGetTokenFunction(sourceCode.getTokenAfter),
getTokensAfter: wrapGetTokensFunction(sourceCode.getTokensAfter),
getTokenByRangeStart: wrapGetTokenFunction(
sourceCode.getTokenByRangeStart,
),
getTokens: wrapGetTokensFunction(sourceCode.getTokens),
getTokensBetween: wrapGetTokensFunction(
sourceCode.getTokensBetween,
),
/* eslint-enable @typescript-eslint/unbound-method -- ignore */
}))
/** Transforms token */
function transformToken(
token: AST.Token | AST.Comment,
): AST.Token | AST.Comment {
return token.value === "---" ? newProxy(token, { value: "___" }) : token
}

/** Wrap token getter function */
function wrapGetTokenFunction<
T extends (
this: SourceCode,
...args: never[]
) => AST.Token | AST.Comment | null,
>(base: T): T {
return function (this: SourceCode, ...args) {
// eslint-disable-next-line no-invalid-this -- is valid
const token = base.apply(this, args)
if (!token) {
return token
}
return transformToken(token)
} as T
}
return (sourceCodeWrapper = newProxy(sourceCode, {
/* eslint-disable @typescript-eslint/unbound-method -- ignore */
getFirstToken: wrapGetTokenFunction(sourceCode.getFirstToken),
getFirstTokens: wrapGetTokensFunction(sourceCode.getFirstTokens),
getFirstTokenBetween: wrapGetTokenFunction(
sourceCode.getFirstTokenBetween,
),
getFirstTokensBetween: wrapGetTokensFunction(
sourceCode.getFirstTokensBetween,
),
getLastToken: wrapGetTokenFunction(sourceCode.getLastToken),
getLastTokens: wrapGetTokensFunction(sourceCode.getLastTokens),
getLastTokenBetween: wrapGetTokenFunction(
sourceCode.getLastTokenBetween,
),
getLastTokensBetween: wrapGetTokensFunction(
sourceCode.getLastTokensBetween,
),
getTokenBefore: wrapGetTokenFunction(sourceCode.getTokenBefore),
getTokensBefore: wrapGetTokensFunction(sourceCode.getTokensBefore),
getTokenAfter: wrapGetTokenFunction(sourceCode.getTokenAfter),
getTokensAfter: wrapGetTokensFunction(sourceCode.getTokensAfter),
getTokenByRangeStart: wrapGetTokenFunction(
sourceCode.getTokenByRangeStart,
),
getTokens: wrapGetTokensFunction(sourceCode.getTokens),
getTokensBetween: wrapGetTokensFunction(sourceCode.getTokensBetween),
/* eslint-enable @typescript-eslint/unbound-method -- ignore */
}))

/** Wrap tokens getter function */
function wrapGetTokensFunction<
T extends (
this: SourceCode,
...args: never[]
) => (AST.Token | AST.Comment)[],
>(base: T): T {
return function (this: SourceCode, ...args) {
// eslint-disable-next-line no-invalid-this -- is valid
const tokens = base.apply(this, args)
return tokens.map(transformToken)
} as T
/** Wrap token getter function */
function wrapGetTokenFunction<
T extends (
this: SourceCode,
...args: never[]
) => AST.Token | AST.Comment | null,
>(base: T): T {
return function (this: SourceCode, ...args) {
// eslint-disable-next-line no-invalid-this -- is valid
const token = base.apply(this, args)
if (!token) {
return token
}
},
}),
)
return transformToken(token)
} as T
}

/** Wrap tokens getter function */
function wrapGetTokensFunction<
T extends (
this: SourceCode,
...args: never[]
) => (AST.Token | AST.Comment)[],
>(base: T): T {
return function (this: SourceCode, ...args) {
// eslint-disable-next-line no-invalid-this -- is valid
const tokens = base.apply(this, args)
return tokens.map(transformToken)
} as T
}
}
},
})
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export type RuleContext = {
getScope(): Scope.Scope

getSourceCode(): SourceCode
sourceCode: SourceCode

markVariableAsUsed(name: string): boolean

Expand Down

0 comments on commit cafd97c

Please sign in to comment.