Skip to content

Commit 4514424

Browse files
nzakasfasttimeamareshsm
authored
build: Enable JSON linting (#18681)
* build: Enable JSON linting * Update eslint.config.js Co-authored-by: Francesco Trotta <[email protected]> * Update eslint.config.js Co-authored-by: Amaresh S M <[email protected]> * Apply feedback * Fix internal paths strings --------- Co-authored-by: Francesco Trotta <[email protected]> Co-authored-by: Amaresh S M <[email protected]>
1 parent a7016a5 commit 4514424

3 files changed

Lines changed: 50 additions & 30 deletions

File tree

Makefile.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ const NODE = "node ", // intentional extra space
7373

7474
// Files
7575
RULE_FILES = glob.sync("lib/rules/*.js").filter(filePath => path.basename(filePath) !== "index.js"),
76-
JSON_FILES = find("conf/").filter(fileType("json")),
7776
MARKDOWNLINT_IGNORE_INSTANCE = ignore().add(fs.readFileSync(path.join(__dirname, ".markdownlintignore"), "utf-8")),
7877
MARKDOWN_FILES_ARRAY = MARKDOWNLINT_IGNORE_INSTANCE.filter(find("docs/").concat(ls(".")).filter(fileType("md"))),
7978
TEST_FILES = "\"tests/{bin,conf,lib,tools}/**/*.js\"",
@@ -93,18 +92,6 @@ const NODE = "node ", // intentional extra space
9392
// Helpers
9493
//------------------------------------------------------------------------------
9594

96-
/**
97-
* Simple JSON file validation that relies on ES JSON parser.
98-
* @param {string} filePath Path to JSON.
99-
* @throws Error If file contents is invalid JSON.
100-
* @returns {undefined}
101-
*/
102-
function validateJsonFile(filePath) {
103-
const contents = fs.readFileSync(filePath, "utf8");
104-
105-
JSON.parse(contents);
106-
}
107-
10895
/**
10996
* Generates a function that matches files with a particular extension.
11097
* @param {string} extension The file extension (i.e. "js")
@@ -572,9 +559,6 @@ target.lint = function([fix = false] = []) {
572559
errors++;
573560
}
574561

575-
echo("Validating JSON Files");
576-
JSON_FILES.forEach(validateJsonFile);
577-
578562
echo("Validating Markdown Files");
579563
lastReturn = lintMarkdown(MARKDOWN_FILES_ARRAY);
580564
if (lastReturn.code !== 0) {

eslint.config.js

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,27 @@ const eslintPluginTestsRecommendedConfig = require("eslint-plugin-eslint-plugin/
1616
const globals = require("globals");
1717
const eslintConfigESLintCJS = require("eslint-config-eslint/cjs");
1818
const eslintConfigESLintFormatting = require("eslint-config-eslint/formatting");
19+
const json = require("@eslint/json");
1920

2021
//-----------------------------------------------------------------------------
2122
// Helpers
2223
//-----------------------------------------------------------------------------
2324

24-
const INTERNAL_FILES = {
25+
const INTERNAL_PATHS = {
2526
CLI_ENGINE_PATTERN: "lib/cli-engine/**/*",
2627
LINTER_PATTERN: "lib/linter/**/*",
2728
RULE_TESTER_PATTERN: "lib/rule-tester/**/*",
2829
RULES_PATTERN: "lib/rules/**/*",
2930
SOURCE_CODE_PATTERN: "lib/source-code/**/*"
3031
};
3132

33+
// same paths but with `.js` at the end
34+
const INTERNAL_FILES = Object.fromEntries(
35+
Object.entries(INTERNAL_PATHS).map(([key, value]) => [key, `${value}.js`])
36+
);
37+
38+
const ALL_JS_FILES = "**/*.js";
39+
3240
/**
3341
* Resolve an absolute path or glob pattern.
3442
* @param {string} pathOrPattern the path or glob pattern.
@@ -44,7 +52,7 @@ function resolveAbsolutePath(pathOrPattern) {
4452
* @returns {Object[]} The array of `no-restricted-require` entries.
4553
*/
4654
function createInternalFilesPatterns(pattern = null) {
47-
return Object.values(INTERNAL_FILES)
55+
return Object.values(INTERNAL_PATHS)
4856
.filter(p => p !== pattern)
4957
.map(p => ({
5058
name: [
@@ -59,8 +67,16 @@ function createInternalFilesPatterns(pattern = null) {
5967
}
6068

6169
module.exports = [
62-
...eslintConfigESLintCJS,
63-
eslintConfigESLintFormatting,
70+
...eslintConfigESLintCJS.map(config => ({
71+
...config,
72+
name: `eslint/${config.name}`,
73+
files: [ALL_JS_FILES]
74+
})),
75+
{
76+
...eslintConfigESLintFormatting,
77+
name: "eslint/formatting",
78+
files: [ALL_JS_FILES]
79+
},
6480
{
6581
name: "eslint/global-ignores",
6682
ignores: [
@@ -74,11 +90,13 @@ module.exports = [
7490
"tests/fixtures/**",
7591
"tests/performance/**",
7692
"tmp/**",
77-
"**/test.js"
93+
"**/test.js",
94+
".vscode"
7895
]
7996
},
8097
{
8198
name: "eslint/internal-rules",
99+
files: [ALL_JS_FILES],
82100
plugins: {
83101
"internal-rules": internalPlugin
84102
},
@@ -99,7 +117,7 @@ module.exports = [
99117
},
100118
{
101119
name: "eslint/rules",
102-
files: ["lib/rules/*", "tools/internal-rules/*"],
120+
files: ["lib/rules/*.js", "tools/internal-rules/*.js"],
103121
ignores: ["**/index.js"],
104122
...eslintPluginRulesRecommendedConfig,
105123
rules: {
@@ -113,15 +131,15 @@ module.exports = [
113131
},
114132
{
115133
name: "eslint/core-rules",
116-
files: ["lib/rules/*"],
134+
files: ["lib/rules/*.js"],
117135
ignores: ["**/index.js"],
118136
rules: {
119137
"eslint-plugin/require-meta-docs-url": ["error", { pattern: "https://eslint.org/docs/latest/rules/{{name}}" }]
120138
}
121139
},
122140
{
123141
name: "eslint/rules-tests",
124-
files: ["tests/lib/rules/*", "tests/tools/internal-rules/*"],
142+
files: ["tests/lib/rules/*.js", "tests/tools/internal-rules/*.js"],
125143
...eslintPluginTestsRecommendedConfig,
126144
rules: {
127145
...eslintPluginTestsRecommendedConfig.rules,
@@ -156,10 +174,27 @@ module.exports = [
156174
}
157175
},
158176

177+
// JSON files
178+
{
179+
name: "eslint/json",
180+
files: ["**/*.json", ".c8rc"],
181+
ignores: ["**/package-lock.json"],
182+
language: "json/json",
183+
...json.configs.recommended
184+
},
185+
186+
// JSONC files
187+
{
188+
name: "eslint/jsonc",
189+
files: ["knip.jsonc"],
190+
language: "json/jsonc",
191+
...json.configs.recommended
192+
},
193+
159194
// Restrict relative path imports
160195
{
161196
name: "eslint/lib",
162-
files: ["lib/*"],
197+
files: ["lib/*.js"],
163198
ignores: ["lib/unsupported-api.js"],
164199
rules: {
165200
"n/no-restricted-require": ["error", [
@@ -172,7 +207,7 @@ module.exports = [
172207
files: [INTERNAL_FILES.CLI_ENGINE_PATTERN],
173208
rules: {
174209
"n/no-restricted-require": ["error", [
175-
...createInternalFilesPatterns(INTERNAL_FILES.CLI_ENGINE_PATTERN)
210+
...createInternalFilesPatterns(INTERNAL_PATHS.CLI_ENGINE_PATTERN)
176211
]]
177212
}
178213
},
@@ -181,7 +216,7 @@ module.exports = [
181216
files: [INTERNAL_FILES.LINTER_PATTERN],
182217
rules: {
183218
"n/no-restricted-require": ["error", [
184-
...createInternalFilesPatterns(INTERNAL_FILES.LINTER_PATTERN),
219+
...createInternalFilesPatterns(INTERNAL_PATHS.LINTER_PATTERN),
185220
"fs",
186221
resolveAbsolutePath("lib/cli-engine/index.js"),
187222
resolveAbsolutePath("lib/rule-tester/index.js")
@@ -193,7 +228,7 @@ module.exports = [
193228
files: [INTERNAL_FILES.RULES_PATTERN],
194229
rules: {
195230
"n/no-restricted-require": ["error", [
196-
...createInternalFilesPatterns(INTERNAL_FILES.RULES_PATTERN),
231+
...createInternalFilesPatterns(INTERNAL_PATHS.RULES_PATTERN),
197232
"fs",
198233
resolveAbsolutePath("lib/cli-engine/index.js"),
199234
resolveAbsolutePath("lib/linter/index.js"),
@@ -204,7 +239,7 @@ module.exports = [
204239
},
205240
{
206241
name: "eslint/shared",
207-
files: ["lib/shared/**/*"],
242+
files: ["lib/shared/**/*.js"],
208243
rules: {
209244
"n/no-restricted-require": ["error", [
210245
...createInternalFilesPatterns(),
@@ -220,7 +255,7 @@ module.exports = [
220255
files: [INTERNAL_FILES.SOURCE_CODE_PATTERN],
221256
rules: {
222257
"n/no-restricted-require": ["error", [
223-
...createInternalFilesPatterns(INTERNAL_FILES.SOURCE_CODE_PATTERN),
258+
...createInternalFilesPatterns(INTERNAL_PATHS.SOURCE_CODE_PATTERN),
224259
"fs",
225260
resolveAbsolutePath("lib/cli-engine/index.js"),
226261
resolveAbsolutePath("lib/linter/index.js"),

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
"@babel/core": "^7.4.3",
106106
"@babel/preset-env": "^7.4.3",
107107
"@eslint/core": "^0.1.0",
108+
"@eslint/json": "^0.1.0",
108109
"@types/estree": "^1.0.5",
109110
"@types/node": "^20.11.5",
110111
"@wdio/browser-runner": "^8.38.3",

0 commit comments

Comments
 (0)