From 9f193cc3c0a4fc246b983c376da0aa3cf0919e8d Mon Sep 17 00:00:00 2001 From: Marco Ferrari Date: Fri, 16 Aug 2024 17:15:05 +0200 Subject: [PATCH] feat: format JSONC and JSON5 with prettier (#6041) --- README.md | 4 +++- lib/functions/buildFileList.sh | 1 + lib/functions/linterCommands.sh | 1 + lib/globals/languages.sh | 4 +++- lib/globals/linterCommandsOptions.sh | 2 ++ ...pected-summary-test-linters-expect-failure-slim.md | 1 + ...ed-summary-test-linters-expect-failure-standard.md | 1 + ...pected-summary-test-linters-expect-success-slim.md | 1 + ...ed-summary-test-linters-expect-success-standard.md | 1 + .../expected-summary-test-linters-fix-mode-slim.md | 1 + ...expected-summary-test-linters-fix-mode-standard.md | 1 + test/linters/jsonc_prettier/json_prettier_bad_1.json5 | 11 +++++++++++ .../linters/jsonc_prettier/json_prettier_good_1.json5 | 11 +++++++++++ test/testUtils.sh | 1 + 14 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 test/linters/jsonc_prettier/json_prettier_bad_1.json5 create mode 100644 test/linters/jsonc_prettier/json_prettier_good_1.json5 diff --git a/README.md b/README.md index 10adb6d2..bbbc7c5b 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ Super-linter supports the following tools: | **Java** | [checkstyle](https://checkstyle.org) | [google-java-format](https://github.com/google/google-java-format) | | **JavaScript** | [ESLint](https://eslint.org/), [standard js](https://standardjs.com/) | [Prettier](https://prettier.io/) | | **JSON** | [eslint-plugin-jsonc (configured for JSON)](https://www.npmjs.com/package/eslint-plugin-jsonc) (default), [eslint-plugin-json](https://www.npmjs.com/package/eslint-plugin-json) | [Prettier](https://prettier.io/) | -| **JSONC**, **JSON5** | [eslint-plugin-jsonc](https://www.npmjs.com/package/eslint-plugin-jsonc) | | +| **JSONC**, **JSON5** | [eslint-plugin-jsonc](https://www.npmjs.com/package/eslint-plugin-jsonc) | [Prettier](https://prettier.io/) | | **JSX**, **TSX** | [eslint-plugin-jsx-a11y](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y), [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) | [Prettier](https://prettier.io/) | | **Kubernetes** | [kubeconform](https://github.com/yannh/kubeconform), [Checkov](https://www.checkov.io/) | See YAML formatters | | **Kotlin** | [ktlint](https://github.com/pinterest/ktlint) | | @@ -235,6 +235,7 @@ You can configure Super-linter using the following environment variables: | **FIX_JSON_PRETTIER** | `true` | Flag to enable or disable the formatting of JSON files with Prettier. | | **FIX_JSON** | `false` | Option to enable fix mode for `JSON`. | | **FIX_JSONC** | `false` | Option to enable fix mode for `JSONC`. | +| **FIX_JSONC_PRETTIER** | `true` | Flag to enable or disable the formatting of JSONC and JSON5 files with Prettier. | | **FIX_JSX_PRETTIER** | `true` | Flag to enable or disable the formatting of JSX files with Prettier. | | **FIX_JSX** | `false` | Option to enable fix mode for `JSX`. | | **FIX_MARKDOWN_PRETTIER** | `true` | Flag to enable or disable the formatting of Markdown files with Prettier. | @@ -352,6 +353,7 @@ You can configure Super-linter using the following environment variables: | **VALIDATE_JSON** | `true` | Flag to enable or disable the linting process of the JSON language. | | **VALIDATE_JSON_PRETTIER** | `true` | Flag to enable or disable checking the formatting of JSON files with Prettier. | | **VALIDATE_JSONC** | `true` | Flag to enable or disable the linting process of the JSONC and JSON5 languages. | +| **VALIDATE_JSONC_PRETTIER** | `true` | Flag to enable or disable checking the formatting of JSONC and JSON5 files with Prettier. | | **VALIDATE_JSX** | `true` | Flag to enable or disable the linting process for jsx files (Utilizing: ESLint) | | **VALIDATE_JSX_PRETTIER** | `true` | Flag to enable or disable checking the formatting of JSX files with Prettier. | | **VALIDATE_KOTLIN** | `true` | Flag to enable or disable the linting process of the Kotlin language. | diff --git a/lib/functions/buildFileList.sh b/lib/functions/buildFileList.sh index 86126e47..7d044190 100755 --- a/lib/functions/buildFileList.sh +++ b/lib/functions/buildFileList.sh @@ -413,6 +413,7 @@ BuildFileArrays() { echo "${FILE}" >>"${FILE_ARRAYS_DIRECTORY_PATH}/file-array-JAVASCRIPT_PRETTIER" elif [ "$FILE_TYPE" == "jsonc" ] || [ "$FILE_TYPE" == "json5" ]; then echo "${FILE}" >>"${FILE_ARRAYS_DIRECTORY_PATH}/file-array-JSONC" + echo "${FILE}" >>"${FILE_ARRAYS_DIRECTORY_PATH}/file-array-JSONC_PRETTIER" elif [ "${FILE_TYPE}" == "json" ]; then echo "${FILE}" >>"${FILE_ARRAYS_DIRECTORY_PATH}/file-array-JSON" echo "${FILE}" >>"${FILE_ARRAYS_DIRECTORY_PATH}/file-array-JSON_PRETTIER" diff --git a/lib/functions/linterCommands.sh b/lib/functions/linterCommands.sh index b25a3328..64c9e29d 100755 --- a/lib/functions/linterCommands.sh +++ b/lib/functions/linterCommands.sh @@ -91,6 +91,7 @@ fi LINTER_COMMANDS_ARRAY_JSON=(eslint -c "${JAVASCRIPT_ES_LINTER_RULES}" --ext '.json') LINTER_COMMANDS_ARRAY_JSON_PRETTIER=("${PRETTIER_COMMAND[@]}") LINTER_COMMANDS_ARRAY_JSONC=(eslint -c "${JAVASCRIPT_ES_LINTER_RULES}" --ext '.json5,.jsonc') +LINTER_COMMANDS_ARRAY_JSONC_PRETTIER=("${PRETTIER_COMMAND[@]}") LINTER_COMMANDS_ARRAY_JSX=(eslint -c "${JSX_LINTER_RULES}") LINTER_COMMANDS_ARRAY_JSX_PRETTIER=("${PRETTIER_COMMAND[@]}") LINTER_COMMANDS_ARRAY_KOTLIN=(ktlint "{/}") diff --git a/lib/globals/languages.sh b/lib/globals/languages.sh index 0b1691d3..1ea94d60 100755 --- a/lib/globals/languages.sh +++ b/lib/globals/languages.sh @@ -21,7 +21,9 @@ LANGUAGE_ARRAY=('ANSIBLE' 'ARM' 'BASH' 'BASH_EXEC' 'CHECKOV' 'CLANG_FORMAT' 'JAVA' 'JAVASCRIPT_ES' 'JAVASCRIPT_PRETTIER' 'JAVASCRIPT_STANDARD' 'JSCPD' 'JSON' "JSON_PRETTIER" - 'JSONC' 'JSX' + 'JSONC' + "JSONC_PRETTIER" + 'JSX' "JSX_PRETTIER" 'KUBERNETES_KUBECONFORM' 'KOTLIN' 'LATEX' 'LUA' 'MARKDOWN' "MARKDOWN_PRETTIER" diff --git a/lib/globals/linterCommandsOptions.sh b/lib/globals/linterCommandsOptions.sh index 2b5e0d1c..87785689 100755 --- a/lib/globals/linterCommandsOptions.sh +++ b/lib/globals/linterCommandsOptions.sh @@ -22,6 +22,7 @@ GRAPHQL_PRETTIER_CHECK_ONLY_MODE_OPTIONS=("${PRETTIER_CHECK_ONLY_MODE_OPTIONS[@] HTML_PRETTIER_CHECK_ONLY_MODE_OPTIONS=("${PRETTIER_CHECK_ONLY_MODE_OPTIONS[@]}") JAVASCRIPT_PRETTIER_CHECK_ONLY_MODE_OPTIONS=("${PRETTIER_CHECK_ONLY_MODE_OPTIONS[@]}") JSON_PRETTIER_CHECK_ONLY_MODE_OPTIONS=("${PRETTIER_CHECK_ONLY_MODE_OPTIONS[@]}") +JSONC_PRETTIER_CHECK_ONLY_MODE_OPTIONS=("${PRETTIER_CHECK_ONLY_MODE_OPTIONS[@]}") JSX_PRETTIER_CHECK_ONLY_MODE_OPTIONS=("${PRETTIER_CHECK_ONLY_MODE_OPTIONS[@]}") MARKDOWN_PRETTIER_CHECK_ONLY_MODE_OPTIONS=("${PRETTIER_CHECK_ONLY_MODE_OPTIONS[@]}") PYTHON_BLACK_CHECK_ONLY_MODE_OPTIONS=(--diff --check) @@ -63,6 +64,7 @@ JAVASCRIPT_STANDARD_FIX_MODE_OPTIONS=("${STANDARD_FIX_MODE_OPTIONS[@]}") JSON_FIX_MODE_OPTIONS=("${ESLINT_FIX_MODE_OPTIONS[@]}") JSON_PRETTIER_FIX_MODE_OPTIONS=("${PRETTIER_FIX_MODE_OPTIONS[@]}") JSONC_FIX_MODE_OPTIONS=("${ESLINT_FIX_MODE_OPTIONS[@]}") +JSONC_PRETTIER_FIX_MODE_OPTIONS=("${PRETTIER_FIX_MODE_OPTIONS[@]}") JSX_FIX_MODE_OPTIONS=("${ESLINT_FIX_MODE_OPTIONS[@]}") JSX_PRETTIER_FIX_MODE_OPTIONS=("${PRETTIER_FIX_MODE_OPTIONS[@]}") MARKDOWN_FIX_MODE_OPTIONS=(--fix) diff --git a/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-failure-slim.md b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-failure-slim.md index d7ec3d59..d70edf99 100644 --- a/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-failure-slim.md +++ b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-failure-slim.md @@ -37,6 +37,7 @@ | JSON | Fail ❌ | | JSON_PRETTIER | Fail ❌ | | JSONC | Fail ❌ | +| JSONC_PRETTIER | Fail ❌ | | JSX | Fail ❌ | | JSX_PRETTIER | Fail ❌ | | KUBERNETES_KUBECONFORM | Fail ❌ | diff --git a/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-failure-standard.md b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-failure-standard.md index d2372770..887c9d75 100644 --- a/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-failure-standard.md +++ b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-failure-standard.md @@ -42,6 +42,7 @@ | JSON | Fail ❌ | | JSON_PRETTIER | Fail ❌ | | JSONC | Fail ❌ | +| JSONC_PRETTIER | Fail ❌ | | JSX | Fail ❌ | | JSX_PRETTIER | Fail ❌ | | KUBERNETES_KUBECONFORM | Fail ❌ | diff --git a/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-success-slim.md b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-success-slim.md index 3776997d..bf182907 100644 --- a/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-success-slim.md +++ b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-success-slim.md @@ -37,6 +37,7 @@ | JSON | Pass ✅ | | JSON_PRETTIER | Pass ✅ | | JSONC | Pass ✅ | +| JSONC_PRETTIER | Pass ✅ | | JSX | Pass ✅ | | JSX_PRETTIER | Pass ✅ | | KUBERNETES_KUBECONFORM | Pass ✅ | diff --git a/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-success-standard.md b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-success-standard.md index 714390e2..cacf58fd 100644 --- a/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-success-standard.md +++ b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-success-standard.md @@ -42,6 +42,7 @@ | JSON | Pass ✅ | | JSON_PRETTIER | Pass ✅ | | JSONC | Pass ✅ | +| JSONC_PRETTIER | Pass ✅ | | JSX | Pass ✅ | | JSX_PRETTIER | Pass ✅ | | KUBERNETES_KUBECONFORM | Pass ✅ | diff --git a/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-fix-mode-slim.md b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-fix-mode-slim.md index 5e82a5b8..81f42f5f 100644 --- a/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-fix-mode-slim.md +++ b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-fix-mode-slim.md @@ -20,6 +20,7 @@ | JSON | Fail ❌ | | JSON_PRETTIER | Pass ✅ | | JSONC | Pass ✅ | +| JSONC_PRETTIER | Pass ✅ | | JSX | Fail ❌ | | JSX_PRETTIER | Pass ✅ | | MARKDOWN | Pass ✅ | diff --git a/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-fix-mode-standard.md b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-fix-mode-standard.md index ba5ff15d..cc2f4c34 100644 --- a/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-fix-mode-standard.md +++ b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-fix-mode-standard.md @@ -24,6 +24,7 @@ | JSON | Fail ❌ | | JSON_PRETTIER | Pass ✅ | | JSONC | Pass ✅ | +| JSONC_PRETTIER | Pass ✅ | | JSX | Fail ❌ | | JSX_PRETTIER | Pass ✅ | | MARKDOWN | Pass ✅ | diff --git a/test/linters/jsonc_prettier/json_prettier_bad_1.json5 b/test/linters/jsonc_prettier/json_prettier_bad_1.json5 new file mode 100644 index 00000000..b72f80bd --- /dev/null +++ b/test/linters/jsonc_prettier/json_prettier_bad_1.json5 @@ -0,0 +1,11 @@ +{ + "arrow_spacing": { + "level": "ignore" + }, + // tests... + "braces_spacing": { + "level": "ignore", + "spaces": 0, + "empty_object_spaces": 0 + } +} diff --git a/test/linters/jsonc_prettier/json_prettier_good_1.json5 b/test/linters/jsonc_prettier/json_prettier_good_1.json5 new file mode 100644 index 00000000..1a5be704 --- /dev/null +++ b/test/linters/jsonc_prettier/json_prettier_good_1.json5 @@ -0,0 +1,11 @@ +{ + arrow_spacing: { + level: "ignore", + }, + // tests... + braces_spacing: { + level: "ignore", + spaces: 0, + empty_object_spaces: 0, + }, +} diff --git a/test/testUtils.sh b/test/testUtils.sh index fe5634b6..cfa11330 100755 --- a/test/testUtils.sh +++ b/test/testUtils.sh @@ -43,6 +43,7 @@ LANGUAGES_WITH_FIX_MODE=( "JSON" "JSON_PRETTIER" "JSONC" + "JSONC_PRETTIER" "JSX" "JSX_PRETTIER" "MARKDOWN"