Skip to content

Commit 2a5a91e

Browse files
committed
Add & configure ESlint; incl. plugins
Issue 2096
1 parent f33bf9e commit 2a5a91e

File tree

3 files changed

+4615
-392
lines changed

3 files changed

+4615
-392
lines changed

eslint.config.mjs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { FlatCompat } from "@eslint/eslintrc";
2+
import pluginJs from "@eslint/js";
3+
import path from "path";
4+
import { fileURLToPath } from "url";
5+
import globals from "globals";
6+
import prettierConfig from "eslint-config-prettier";
7+
import requirejs from "eslint-plugin-requirejs";
8+
import jsdoc from "eslint-plugin-jsdoc";
9+
10+
// For compatibility with configs that don't use the new eslint flat config format:
11+
const __filename = fileURLToPath(import.meta.url);
12+
const __dirname = path.dirname(__filename);
13+
const compat = new FlatCompat({
14+
baseDirectory: __dirname,
15+
});
16+
17+
// ---------------------------------------------------------------------
18+
// ESLINT RECOMMENDED:
19+
// Use the recommended config for JS files by eslint
20+
const eslintRecommendedConfig = pluginJs.configs.recommended;
21+
const eslintRulesOverrides = {
22+
// Allow underscored names to explicitly indicate an argument is not used
23+
"no-unused-vars": ["error", { argsIgnorePattern: "^_" }],
24+
};
25+
26+
// ---------------------------------------------------------------------
27+
// AIRBNB JAVASCRIPT STYLE GUIDE:
28+
// Use the Airbnb style guide, excluding react- and node- specific rules & the
29+
// rule that disallows "use strict"
30+
const airbnbConfigs = [
31+
...compat.extends("eslint-config-airbnb-base/rules/best-practices"),
32+
...compat.extends("eslint-config-airbnb-base/rules/errors"),
33+
...compat.extends("eslint-config-airbnb-base/rules/style"),
34+
...compat.extends("eslint-config-airbnb-base/rules/variables"),
35+
...compat.extends("eslint-config-airbnb-base/rules/es6"),
36+
...compat.extends("eslint-config-airbnb-base/rules/imports"),
37+
];
38+
const airbnbRulesOverrides = {
39+
// We are using the AMD/RequireJS module pattern
40+
"import/no-amd": "off",
41+
"import/no-commonjs": "off",
42+
};
43+
44+
// ---------------------------------------------------------------------
45+
// JSDOCS:
46+
// Ensure JSDoc comments are used, and check validity
47+
const jsdocConfig = jsdoc.configs["flat/recommended"];
48+
const jsdocsRulesOverrides = {
49+
// Non-standard JSDoc tags we use to generate documentation.
50+
"jsdoc/check-tag-names": [
51+
"error",
52+
{ definedTags: ["classcategory", "screenshot"] },
53+
],
54+
// Avoiding this error would mean importing modules we don't use
55+
"jsdoc/no-undefined-types": "off",
56+
};
57+
58+
// ---------------------------------------------------------------------
59+
// REQUIREJS
60+
// Use all rules from the recommended config for RequireJS
61+
const requirejsConfig = {
62+
name: "requirejs",
63+
plugins: {
64+
requirejs,
65+
},
66+
rules: {
67+
...requirejs.configs.recommended.rules,
68+
},
69+
};
70+
71+
// ---------------------------------------------------------------------
72+
// METACATUI OVERRIDES
73+
const metacatuiConfig = {
74+
files: ["src/**/*.js"],
75+
languageOptions: {
76+
sourceType: "commonjs",
77+
ecmaVersion: 2020,
78+
globals: {
79+
...globals.browser,
80+
...globals.amd,
81+
MetacatUI: "readonly",
82+
google: "readonly",
83+
},
84+
},
85+
// Override rules that are not compatible with MetacatUI
86+
rules: {
87+
...eslintRulesOverrides,
88+
...airbnbRulesOverrides,
89+
...jsdocsRulesOverrides,
90+
},
91+
};
92+
93+
// Ignores must be a separate object to be treated as global
94+
const ignoreList = {
95+
ignores: [
96+
"src/components/",
97+
"docs/",
98+
"test/",
99+
"node_modules/",
100+
".github",
101+
"server.js",
102+
"eslint.config.mjs",
103+
],
104+
};
105+
106+
export default [
107+
ignoreList,
108+
eslintRecommendedConfig,
109+
...airbnbConfigs,
110+
jsdocConfig,
111+
requirejsConfig,
112+
metacatuiConfig,
113+
prettierConfig, // prettier must be the last config in the array
114+
];

0 commit comments

Comments
 (0)