Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Rule } from "eslint"
import type { AST } from "jsonc-eslint-parser"
import * as jsoncESLintParser from "jsonc-eslint-parser"
import type { AST as V } from "vue-eslint-parser"
import path from "path"

/**
* Define the rule.
Expand All @@ -27,7 +28,8 @@ export function createRule(
create(context: Rule.RuleContext): any {
if (
typeof context.parserServices.defineCustomBlocksVisitor ===
"function"
"function" &&
path.extname(context.getFilename()) === ".vue"
) {
return context.parserServices.defineCustomBlocksVisitor(
context,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"root": true,
"plugins": [
"jsonc"
],
"parser": "vue-eslint-parser",
"parserOptions": {
"parser": "jsonc-eslint-parser",
"sourceType": "module",
"ecmaVersion": 2015
},
"rules": {
"jsonc/indent": "error"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
17 changes: 17 additions & 0 deletions tests-integrations/fixtures/vue-eslint-parser-option/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "test-for-eslint-plugin-jsonc-with-vue",
"private": true,
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"devDependencies": {
"eslint": "^7.17.0",
"vue-eslint-parser": "^7.3.0",
"eslint-plugin-jsonc": "file:../../../"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"a": "b"
}
2 changes: 1 addition & 1 deletion tests-integrations/lib/eslint-plugin-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("Integration with eslint-plugin-markdown", () => {
process.chdir(originalCwd)
})

it("should lint without errors", () => {
it("should lint errors", () => {
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports -- test */
// @ts-ignore
const eslint = require(ESLINT)
Expand Down
45 changes: 45 additions & 0 deletions tests-integrations/lib/vue-eslint-parser-option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import cp from "child_process"
import assert from "assert"
import path from "path"
import plugin from "../../lib/index"

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

const TEST_CWD = path.join(__dirname, "../fixtures/vue-eslint-parser-option")

const ESLINT = path.join(TEST_CWD, `./node_modules/eslint`)

describe("Integration with vue-eslint-parser with option", () => {
let originalCwd: string

before(() => {
originalCwd = process.cwd()
process.chdir(TEST_CWD)
cp.execSync("npm i", { stdio: "inherit" })
})
after(() => {
process.chdir(originalCwd)
})

it("should lint errors", () => {
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports -- test */
// @ts-ignore
const eslint = require(ESLINT)
/* eslint-enable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports -- test */
const engine = new eslint.CLIEngine({
cwd: TEST_CWD,
extensions: [".js", ".json", ".yaml"],
})
engine.addPlugin("eslint-plugin-jsonc", plugin)
const r = engine.executeOnFiles(["./test.json"])

assert.strictEqual(r.results.length, 1)
assert.strictEqual(r.results[0].messages.length, 1)
assert.strictEqual(
r.results[0].messages[0].message,
"Expected indentation of 4 spaces but found 0.",
)
})
})
1 change: 1 addition & 0 deletions tests/lib/rules/array-bracket-newline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tester.run("array-bracket-newline", rule as any, {
],
},
{
filename: "test.vue",
code: `<i18n>[1,\n2]</i18n><custom-block lang="json">[\n1\n]</custom-block>`,
output: `<i18n>[\n1,\n2\n]</i18n><custom-block lang="json">[1]</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/array-bracket-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tester.run("array-bracket-spacing", rule as any, {
],
},
{
filename: "test.vue",
code: `<i18n>[ 1, 2 ]</i18n><custom-block lang="jsonc">[ 1 ]</custom-block>`,
output: `<i18n>[1, 2]</i18n><custom-block lang="jsonc">[1]</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/array-element-newline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tester.run("array-element-newline", rule as any, {
],
},
{
filename: "test.vue",
code: `<i18n>[1, 2, 3]</i18n><custom-block lang="json5">[1, 2, 3]</custom-block>`,
output: `<i18n>[1,
2,
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/comma-dangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ tester.run("comma-dangle", rule as any, {
errors: ["Missing trailing comma.", "Unexpected trailing comma."],
},
{
filename: "test.vue",
code: `<i18n>{"key": "value",}</i18n><custom-block>{"key": "value",}</custom-block>`,
output: `<i18n>{"key": "value"}</i18n><custom-block>{"key": "value",}</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/comma-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ tester.run("comma-style", rule as any, {
],
},
{
filename: "test.vue",
code: `<i18n>{
"BAD": ["apples"
, "oranges"],
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/key-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tester.run("key-spacing", rule as any, {
],
},
{
filename: "test.vue",
code: `<custom-block lang="json">{"key" :"value"}</custom-block>`,
output: `<custom-block lang="json">{"key": "value"}</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-bigint-literals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tester.run("no-bigint-literals", rule as any, {
],
},
{
filename: "test.vue",
code: `<custom-block lang="json">42n</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
errors: ["BigInt literals are not allowed."],
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tester.run("no-comments", rule as any, {
],
},
{
filename: "test.vue",
code: `<custom-block lang="json">
// comment
"a"</custom-block>`,
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-dupe-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ tester.run("no-dupe-keys", rule as any, {
errors: ["Duplicate key 'key'."],
},
{
filename: "test.vue",
code: `<custom-block lang="json">{"key": "value", "key": "value"}</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
errors: ["Duplicate key 'key'."],
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-multi-str.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ tester.run("no-multi-str", rule as any, {
errors: 1, // FIXME: drop supports eslint6 ["Multiline support is limited to JSON5 only."],
},
{
filename: "test.vue",
code: `<custom-block lang="json">"Line 1 \\\nLine 2"</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
errors: 1, // FIXME: drop supports eslint6 ["Multiline support is limited to JSON5 only."],
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-number-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ tester.run("no-number-props", rule as any, {
errors: ["The number property keys are not allowed."],
},
{
filename: "test.vue",
code: `<custom-block lang="json">{123: 123}</custom-block>`,
output: `<custom-block lang="json">{"123": 123}</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-numeric-separators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tester.run("no-numeric-separators", rule as any, {
errors: ["Numeric separators are not allowed."],
},
{
filename: "test.vue",
code: `<custom-block lang="json">{"a": 1_23}</custom-block>`,
output: `<custom-block lang="json">{"a": 123}</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-octal-escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ tester.run("no-octal-escape", rule as any, {
errors: ["Don't use octal: '\\251'. Use '\\u....' instead."],
},
{
filename: "test.vue",
code: `<custom-block lang="json">{"BAD": "Copyright \\251"}</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
errors: ["Don't use octal: '\\251'. Use '\\u....' instead."],
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-regexp-literals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ tester.run("no-regexp-literals", rule as any, {
],
},
{
filename: "test.vue",
code: `<custom-block lang="json">/reg/</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
errors: ["RegExp literals are not allowed."],
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-sparse-arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tester.run("no-sparse-arrays", rule as any, {
errors: ["Unexpected comma in middle of array."],
},
{
filename: "test.vue",
code: `<custom-block lang="json">[,,]</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
errors: ["Unexpected comma in middle of array."],
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-template-literals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tester.run("no-template-literals", rule as any, {
errors: ["The template literals are not allowed."],
},
{
filename: "test.vue",
code: `<custom-block lang="json">{"foo":\`template\`}</custom-block>`,
output: `<custom-block lang="json">{"foo":"template"}</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-undefined-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ tester.run("no-undefined-value", rule as any, {
],
},
{
filename: "test.vue",
code: `<custom-block lang="json">undefined</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
errors: ["`undefined` is not allowed."],
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-useless-escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ tester.run("no-useless-escape", rule as any, {
errors: ["Unnecessary escape character: \\a."],
},
{
filename: "test.vue",
code: `<custom-block lang="json">"hol\\a"</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
errors: ["Unnecessary escape character: \\a."],
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/object-curly-newline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ tester.run("object-curly-newline", rule as any, {
errors: ["Unexpected line break after this opening brace."],
},
{
filename: "test.vue",
code: `<custom-block lang="json">{\n"key": "value"}</custom-block>`,
output: `<custom-block lang="json">{"key": "value"}</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/object-curly-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tester.run("object-curly-spacing", rule as any, {
],
},
{
filename: "test.vue",
code: `<custom-block lang="json">{ "key": "value" }</custom-block>`,
output: `<custom-block lang="json">{"key": "value"}</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/object-property-newline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tester.run("object-property-newline", rule as any, {
],
},
{
filename: "test.vue",
code: `<custom-block lang="json">{ "foo": "foo", "bar": "bar" }</custom-block>`,
output: `<custom-block lang="json">{ "foo": "foo",\n"bar": "bar" }</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/quote-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ tester.run("quote-props", rule as any, {
errors: ["Unquoted property 'key' found."],
},
{
filename: "test.vue",
code: `<custom-block lang="json">{key: "value"}</custom-block>`,
output: `<custom-block lang="json">{"key": "value"}</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/quotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ tester.run("quotes", rule as any, {
errors: ["Strings must use doublequote."],
},
{
filename: "test.vue",
code: `<custom-block lang="json">['element']</custom-block>`,
output: `<custom-block lang="json">["element"]</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/sort-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ tester.run("sort-keys", rule as any, {
],
},
{
filename: "test.vue",
code: `<custom-block lang="json">{a:1, A: 2, B:3, b:4}</custom-block>`,
output: `<custom-block lang="json">{ A: 2,a:1, B:3, b:4}</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/space-unary-ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ tester.run("space-unary-ops", rule as any, {
],
},
{
filename: "test.vue",
code: `<custom-block lang="json">- 1</custom-block>`,
output: `<custom-block lang="json">-1</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/valid-json-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ tester.run("valid-json-number", rule as any, {
],
},
{
filename: "test.vue",
code: `<custom-block lang="json">0x123</custom-block>`,
output: `<custom-block lang="json">291</custom-block>`,
parser: require.resolve("vue-eslint-parser"),
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/vue-custom-block/no-parsing-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ tester.run("vue-custom-block/no-parsing-error", rule as any, {
'"string"',
'["element"]',
{
filename: "test.vue",
code: `<i18n>{"key": "value"}</i18n>`,
parser: require.resolve("vue-eslint-parser"),
},
{
filename: "test.vue",
code: `<i18n lang="json5">/**/123</i18n>`,
parser: require.resolve("vue-eslint-parser"),
},
],
invalid: [
{
filename: "test.vue",
code: `<i18n>{"key" "value"}</i18n>`,
parser: require.resolve("vue-eslint-parser"),
errors: [
Expand All @@ -35,6 +38,7 @@ tester.run("vue-custom-block/no-parsing-error", rule as any, {
],
},
{
filename: "test.vue",
code: `<i18n></i18n>`,
parser: require.resolve("vue-eslint-parser"),
errors: [
Expand All @@ -46,6 +50,7 @@ tester.run("vue-custom-block/no-parsing-error", rule as any, {
],
},
{
filename: "test.vue",
code: `<i18n>/**/123</i18n>`,
parser: require.resolve("vue-eslint-parser"),
errors: [
Expand All @@ -57,6 +62,7 @@ tester.run("vue-custom-block/no-parsing-error", rule as any, {
],
},
{
filename: "test.vue",
code: `<i18n lang="json">/**/123</i18n>`,
parser: require.resolve("vue-eslint-parser"),
errors: [
Expand Down