Skip to content

Commit

Permalink
feat: add kintone-customize presets for flat config (#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
sajikix authored Jan 22, 2025
1 parent 938e12b commit 82ebf82
Show file tree
Hide file tree
Showing 15 changed files with 157 additions and 50 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,22 @@ module.exports = [
## Support rule set

- `@cybozu`
- or `@cybozu/eslint-config/flat/presets/base` for Flat Config
- This is included in the all following presets
- `@cybozu/eslint-config/presets/node`
- or `@cybozu/eslint-config/flat/presets/node` for Flat Config
- Including `eslint-plugin-n`
- `@cybozu/eslint-config/presets/typescript`
- or `@cybozu/eslint-config/flat/presets/typescript` for Flat Config
- Including `@typescript-eslint/eslint-plugin`
- `@cybozu/eslint-config/presets/react`
- or `@cybozu/eslint-config/flat/presets/react` for Flat Config
- Including `eslint-plugin-react`, `eslint-plugin-jsx-ally` and `eslint-plugin-react-hooks`
- `@cybozu/eslint-config/presets/react-typescript`
- or `@cybozu/eslint-config/flat/presets/react-typescript` for Flat Config
- Including `@cybozu/eslint-config/presets/typescript` and `@cybozu/eslint-config/presets/react`
- `@cybozu/eslint-config/presets/es5`
- or `@cybozu/eslint-config/flat/presets/es5` for Flat Config

## Prettier Support

Expand Down Expand Up @@ -164,10 +170,14 @@ module.exports = {
### Presets

- `@cybozu/eslint-config/preset/kintone-customize`
- or `@cybozu/eslint-config/flat/presets/kintone-customize` for Flat Config
- Preset for kintone customize/plugin-in development
- `@cybozu/eslint-config/preset/kintone-customize-prettier`
- or `@cybozu/eslint-config/flat/presets/kintone-customize-prettier` for Flat Config
- Preset for kintone customize/plugin-in development including `prettier` config
- `@cybozu/eslint-config/preset/kintone-customize-es5`
- or `@cybozu/eslint-config/flat/presets/kintone-customize-es5` for Flat Config
- Preset for kintone customize/plugin-in development in ES5
- `@cybozu/eslint-config/preset/kintone-customize-es5-prettier`
- or `@cybozu/eslint-config/flat/presets/kintone-customize-es5-prettier` for Flat Config
- Preset for kintone customize/plugin-in development in ES5 including `prettier` config
25 changes: 15 additions & 10 deletions flat/lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@ const importPlugin = require("eslint-plugin-import-x");
const eslint = require("@eslint/js");

/**
* @param {{ overrideGlobals?: import("eslint").ESLint.Globals} | undefined} overrides
* @return { import("eslint").Linter.FlatConfig[] }
*/
module.exports = function base() {
module.exports = function base(overrides) {
return [
eslint.configs.recommended,
{
plugins: {
"import-x": importPlugin,
},
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
globals: {
...globals.browser,
...globals.es2015,
...globals.commonjs,
},
globals: overrides?.overrideGlobals
? overrides.overrideGlobals
: {
...globals.browser,
...globals.es2024,
...globals.commonjs,
},
},
},
eslint.configs.recommended,
{
plugins: {
"import-x": importPlugin,
},
rules: {
// =======
Expand Down
2 changes: 1 addition & 1 deletion flat/lib/es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const base = require("./base");
const globals = require("globals");
const eslint = require("@eslint/js");

const baseRules = base()[1].rules;
const baseRules = base()[2].rules;

// This rule can be parsed with ES5
delete baseRules["import-x/no-duplicates"];
Expand Down
23 changes: 23 additions & 0 deletions flat/lib/kintone-es5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const globals = require("globals");
const kintoneGlobals = require("../globals/kintone");

/**
* @return { import("eslint").Linter.FlatConfig[] }
*/
module.exports = function kintoneEs5() {
return [
{
languageOptions: {
ecmaVersion: 5,
sourceType: "script",
globals: {
...globals.browser,
...kintoneGlobals,
},
},
rules: {
strict: ["error", "function"],
},
},
];
};
21 changes: 7 additions & 14 deletions flat/lib/kintone.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
const globals = require("globals");
const kintoneGlobals = require("../globals/kintone");
const base = require("../lib/base.js");

/**
* @return { import("eslint").Linter.FlatConfig[] }
*/
module.exports = function kintone() {
return [
{
languageOptions: {
ecmaVersion: 5,
sourceType: "script",
globals: {
...globals.browser,
...kintoneGlobals,
},
},
rules: {
strict: ["error", "function"],
},
return base({
overrideGlobals: {
...globals.browser,
...globals.es2024,
...kintoneGlobals,
},
];
});
};
8 changes: 4 additions & 4 deletions flat/presets/kintone-customize-es5-prettier.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const es5 = require("../lib/es5.js");
const kintone = require("../lib/kintone.js");
const prettier = require("../lib/prettier.js");
const kintoneEs5 = require("../lib/kintone-es5.js");
const kintoneGlobals = require("../globals/kintone.js");
const prettier = require("../lib/prettier.js");

/**
* @type { import("eslint").Linter.Config[] }
*/
module.exports = [
{ files: ["**/*.{js,cjs,mjs,jsx}"] },
...es5(kintoneGlobals),
...kintone(),
...es5({ overrideGlobals: kintoneGlobals }),
...kintoneEs5(),
...prettier(),
];
8 changes: 6 additions & 2 deletions flat/presets/kintone-customize-es5.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const es5 = require("../lib/es5.js");
const kintone = require("../lib/kintone.js");
const kintoneEs5 = require("../lib/kintone-es5.js");
const kintoneGlobals = require("../globals/kintone.js");

/**
* @type { import("eslint").Linter.Config[] }
*/
module.exports = [...es5(kintoneGlobals), ...kintone()];
module.exports = [
{ files: ["**/*.{js,cjs,mjs,jsx}"] },
...es5({ overrideGlobals: kintoneGlobals }),
...kintoneEs5(),
];
11 changes: 11 additions & 0 deletions flat/presets/kintone-customize-prettier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const kintone = require("../lib/kintone.js");
const prettier = require("../lib/prettier.js");

/**
* @type { import("eslint").Linter.Config[] }
*/
module.exports = [
{ files: ["**/*.{js,cjs,mjs,ts,mts,cts,jsx,tsx}"] },
...kintone(),
...prettier(),
];
9 changes: 9 additions & 0 deletions flat/presets/kintone-customize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const kintone = require("../lib/kintone.js");

/**
* @type { import("eslint").Linter.Config[] }
*/
module.exports = [
{ files: ["**/*.{js,cjs,mjs,ts,mts,cts,jsx,tsx}"] },
...kintone(),
];
6 changes: 0 additions & 6 deletions test/fixtures/base/ok.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Globals defined by jQuery
/* global $, jQuery */

const foos = [''];
foos.map((foo) => foo + foo);

Expand All @@ -10,6 +7,3 @@ async function hoge() {
return {...result};
}
hoge();

$.Deferred();
jQuery.Deferred();
26 changes: 26 additions & 0 deletions test/flat/kintone-es5-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const assert = require("assert");
const kintoneEs5 = require("../../flat/lib/kintone-es5");
const runLintWithFixturesFlat = require("../lib/runLintWithFixturesFlat");

describe("flat kintone-es5", () => {
it("should get expected errors and warnings with kintone-es5 config", async () => {
const result = await runLintWithFixturesFlat("kintone", kintoneEs5());
assert.deepStrictEqual(result, {
"error.js": {
errors: ["strict", "strict"],
},
"ok.js": {},
});
});
it("should be able to use kintone-customize-es5 as well as lib/kintone-es5", async () => {
assert.deepStrictEqual(
await runLintWithFixturesFlat("globals-kintone", kintoneEs5()),
{
"ok.js": {},
"error.js": {
errors: ["no-undef"],
},
}
);
});
});
16 changes: 4 additions & 12 deletions test/flat/kintone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,16 @@ const kintone = require("../../flat/lib/kintone");
const runLintWithFixturesFlat = require("../lib/runLintWithFixturesFlat");

describe("flat kintone", () => {
it("should get expected errors and warnings with kintone config", async () => {
const result = await runLintWithFixturesFlat("kintone", kintone());
assert.deepStrictEqual(result, {
"error.js": {
errors: ["strict", "strict"],
},
"ok.js": {},
});
});
it("should be able to use kintone-customize-es5 as well as lib/kintone", async () => {
it("should be able to use kintone-customize as well as lib/kintone", async () => {
assert.deepStrictEqual(
await runLintWithFixturesFlat("globals-kintone", kintone()),
{
"ok.js": {},
"ok.js": { warnings: ["spaced-comment"] },
"error.js": {
errors: ["no-undef"],
warnings: ["spaced-comment"],
},
},
}
);
});
});
13 changes: 13 additions & 0 deletions test/flat/preset-kintone-customize-prettier-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const assert = require("assert");
const kintoneCustomizePrettier = require("../../flat/presets/kintone-customize-prettier");
const prettier = require("../../flat/presets/prettier");
const runLintWithFixturesFlat = require("../lib/runLintWithFixturesFlat");

describe("flat preset kintone-customize-prettier", () => {
it("should be able to use kintone-customize as well as preset/prettier", async () => {
assert.deepStrictEqual(
await runLintWithFixturesFlat("prettier", prettier),
await runLintWithFixturesFlat("prettier", kintoneCustomizePrettier)
);
});
});
28 changes: 28 additions & 0 deletions test/flat/preset-kintone-customize-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const assert = require("assert");
const kintoneCustomize = require("../../flat/presets/kintone-customize");
const base = require("../../flat/presets/base");
const runLintWithFixturesFlat = require("../lib/runLintWithFixturesFlat");

describe("flat preset kintone-customize", () => {
it("should be able to use kintone-customize as well as preset/base", async () => {
assert.deepStrictEqual(
await runLintWithFixturesFlat("base", base),
await runLintWithFixturesFlat("base", kintoneCustomize)
);
});
it("should get expected errors and warninigs with kintone globals", async () => {
const result = await runLintWithFixturesFlat(
"globals-kintone",
kintoneCustomize
);
assert.deepStrictEqual(result, {
"error.js": {
errors: ["no-undef"],
warnings: ["spaced-comment"],
},
"ok.js": {
warnings: ["spaced-comment"],
},
});
});
});
1 change: 0 additions & 1 deletion test/lib/runLintWithFixturesFlat.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const runLintWithFixturesFlat = async (type, configObject) => {
});
const targetDir = path.resolve(__dirname, "..", "fixtures", type);
const lintResult = await eslint.lintFiles([targetDir]);
// console.log(JSON.stringify(lintResult, null, 2));
return lintResult.reduce((results, { filePath, messages }) => {
// strip path
const fileName = filePath.replace(`${targetDir}/`, "");
Expand Down

0 comments on commit 82ebf82

Please sign in to comment.