From cafd97c515498c3ca11200091754a2bdc6eb9017 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Mon, 8 May 2023 13:52:03 +0900 Subject: [PATCH] fix: incompatible with ESLint v8.40 in `astro/semi` rule (#201) * fix: incompatible with ESLint v8.40 in `astro/semi` rule * Create quiet-lobsters-compete.md * fix --- .changeset/quiet-lobsters-compete.md | 5 + src/rules/semi.ts | 152 ++++++++++++++------------- src/types.ts | 1 + 3 files changed, 83 insertions(+), 75 deletions(-) create mode 100644 .changeset/quiet-lobsters-compete.md diff --git a/.changeset/quiet-lobsters-compete.md b/.changeset/quiet-lobsters-compete.md new file mode 100644 index 00000000..bd6b2ac5 --- /dev/null +++ b/.changeset/quiet-lobsters-compete.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-astro": patch +--- + +fix: incompatible with ESLint v8.40 in `astro/semi` rule diff --git a/src/rules/semi.ts b/src/rules/semi.ts index 58710242..beabccdd 100644 --- a/src/rules/semi.ts +++ b/src/rules/semi.ts @@ -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 + } + } }, }) diff --git a/src/types.ts b/src/types.ts index 6c24cb7f..6ce36408 100644 --- a/src/types.ts +++ b/src/types.ts @@ -132,6 +132,7 @@ export type RuleContext = { getScope(): Scope.Scope getSourceCode(): SourceCode + sourceCode: SourceCode markVariableAsUsed(name: string): boolean