From 59d8e250b9d47c5fe18b73d8a1474aa72d7dc500 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Sun, 7 May 2023 08:57:00 +0900 Subject: [PATCH] Fix defineCustomBlocksVisitor was incompatible with ESLint v8.40 (#192) --- src/sfc/custom-block/index.ts | 4 +++ test/define-custom-blocks-visitor.js | 39 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/sfc/custom-block/index.ts b/src/sfc/custom-block/index.ts index bdaa1d28..c685af03 100644 --- a/src/sfc/custom-block/index.ts +++ b/src/sfc/custom-block/index.ts @@ -23,6 +23,7 @@ export type ESLintCustomBlockParser = ParserObject export type CustomBlockContext = { getSourceCode(): SourceCode + sourceCode: SourceCode parserServices: any getAncestors(): any[] getDeclaredVariables(node: any): any[] @@ -251,6 +252,9 @@ export function createCustomBlockSharedContext({ : {}), }, getSourceCode, + get sourceCode() { + return getSourceCode() + }, }, } diff --git a/test/define-custom-blocks-visitor.js b/test/define-custom-blocks-visitor.js index 31f6e942..34634020 100644 --- a/test/define-custom-blocks-visitor.js +++ b/test/define-custom-blocks-visitor.js @@ -667,5 +667,44 @@ function a(arg) { "Assignment to function parameter 'arg'.", ) }) + + it("should work sourceCode.", () => { + const code = ` + +var v = + 42 + +` + const linter = createLinter() + const rule = linter.getRules().get("space-unary-ops") + linter.defineRule("test-space-unary-ops", { + ...rule, + create(context) { + return context.parserServices.defineCustomBlocksVisitor( + context, + espree, + { + target: "js", + create(customBlockContext) { + return rule.create(customBlockContext) + }, + }, + ) + }, + }) + + const messages1 = linter.verify(code, { + ...LINTER_CONFIG, + rules: { + ...LINTER_CONFIG.rules, + "test-space-unary-ops": "error", + }, + }) + + assert.strictEqual(messages1.length, 1) + assert.strictEqual( + messages1[0].message, + "Unexpected space after unary operator '+'.", + ) + }) }) })