Skip to content

Commit af1f419

Browse files
authored
Fixed bug that rules not work when using jsonc-eslint-parser as vue-eslint-parser fallback parser (#32)
* Fixed bug that rules not work when using jsonc-eslint-parser as vue-eslint-parser fallback parser * fix * fix
1 parent fcdca99 commit af1f419

34 files changed

+117
-2
lines changed

lib/utils/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Rule } from "eslint"
33
import type { AST } from "jsonc-eslint-parser"
44
import * as jsoncESLintParser from "jsonc-eslint-parser"
55
import type { AST as V } from "vue-eslint-parser"
6+
import path from "path"
67

78
/**
89
* Define the rule.
@@ -27,7 +28,8 @@ export function createRule(
2728
create(context: Rule.RuleContext): any {
2829
if (
2930
typeof context.parserServices.defineCustomBlocksVisitor ===
30-
"function"
31+
"function" &&
32+
path.extname(context.getFilename()) === ".vue"
3133
) {
3234
return context.parserServices.defineCustomBlocksVisitor(
3335
context,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"root": true,
3+
"plugins": [
4+
"jsonc"
5+
],
6+
"parser": "vue-eslint-parser",
7+
"parserOptions": {
8+
"parser": "jsonc-eslint-parser",
9+
"sourceType": "module",
10+
"ecmaVersion": 2015
11+
},
12+
"rules": {
13+
"jsonc/indent": "error"
14+
}
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "test-for-eslint-plugin-jsonc-with-vue",
3+
"private": true,
4+
"version": "1.0.0",
5+
"description": "",
6+
"main": "index.js",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "",
11+
"license": "MIT",
12+
"devDependencies": {
13+
"eslint": "^7.17.0",
14+
"vue-eslint-parser": "^7.3.0",
15+
"eslint-plugin-jsonc": "file:../../../"
16+
}
17+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"a": "b"
3+
}

tests-integrations/lib/eslint-plugin-markdown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe("Integration with eslint-plugin-markdown", () => {
2323
process.chdir(originalCwd)
2424
})
2525

26-
it("should lint without errors", () => {
26+
it("should lint errors", () => {
2727
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports -- test */
2828
// @ts-ignore
2929
const eslint = require(ESLINT)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import cp from "child_process"
2+
import assert from "assert"
3+
import path from "path"
4+
import plugin from "../../lib/index"
5+
6+
// -----------------------------------------------------------------------------
7+
// Tests
8+
// -----------------------------------------------------------------------------
9+
10+
const TEST_CWD = path.join(__dirname, "../fixtures/vue-eslint-parser-option")
11+
12+
const ESLINT = path.join(TEST_CWD, `./node_modules/eslint`)
13+
14+
describe("Integration with vue-eslint-parser with option", () => {
15+
let originalCwd: string
16+
17+
before(() => {
18+
originalCwd = process.cwd()
19+
process.chdir(TEST_CWD)
20+
cp.execSync("npm i", { stdio: "inherit" })
21+
})
22+
after(() => {
23+
process.chdir(originalCwd)
24+
})
25+
26+
it("should lint errors", () => {
27+
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports -- test */
28+
// @ts-ignore
29+
const eslint = require(ESLINT)
30+
/* eslint-enable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports -- test */
31+
const engine = new eslint.CLIEngine({
32+
cwd: TEST_CWD,
33+
extensions: [".js", ".json", ".yaml"],
34+
})
35+
engine.addPlugin("eslint-plugin-jsonc", plugin)
36+
const r = engine.executeOnFiles(["./test.json"])
37+
38+
assert.strictEqual(r.results.length, 1)
39+
assert.strictEqual(r.results[0].messages.length, 1)
40+
assert.strictEqual(
41+
r.results[0].messages[0].message,
42+
"Expected indentation of 4 spaces but found 0.",
43+
)
44+
})
45+
})

tests/lib/rules/array-bracket-newline.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ tester.run("array-bracket-newline", rule as any, {
2525
],
2626
},
2727
{
28+
filename: "test.vue",
2829
code: `<i18n>[1,\n2]</i18n><custom-block lang="json">[\n1\n]</custom-block>`,
2930
output: `<i18n>[\n1,\n2\n]</i18n><custom-block lang="json">[1]</custom-block>`,
3031
parser: require.resolve("vue-eslint-parser"),

tests/lib/rules/array-bracket-spacing.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tester.run("array-bracket-spacing", rule as any, {
1717
],
1818
},
1919
{
20+
filename: "test.vue",
2021
code: `<i18n>[ 1, 2 ]</i18n><custom-block lang="jsonc">[ 1 ]</custom-block>`,
2122
output: `<i18n>[1, 2]</i18n><custom-block lang="jsonc">[1]</custom-block>`,
2223
parser: require.resolve("vue-eslint-parser"),

tests/lib/rules/array-element-newline.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ tester.run("array-element-newline", rule as any, {
2323
],
2424
},
2525
{
26+
filename: "test.vue",
2627
code: `<i18n>[1, 2, 3]</i18n><custom-block lang="json5">[1, 2, 3]</custom-block>`,
2728
output: `<i18n>[1,
2829
2,

0 commit comments

Comments
 (0)