Skip to content

Commit

Permalink
migrate node config
Browse files Browse the repository at this point in the history
Signed-off-by: Kangrui Johann Ye <[email protected]>
  • Loading branch information
strawberry-choco committed Oct 7, 2024
1 parent da2c071 commit ea6ff80
Show file tree
Hide file tree
Showing 15 changed files with 298 additions and 152 deletions.
1 change: 0 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export default tseslint.config(
// until we are done with migrating everything to v9
ignores: [
'packages/eslint-plugin-angular/src/configs/format-template.ts',
'packages/eslint-plugin-node/*',
'packages/eslint-plugin-react/*',
'packages/eslint-plugin-typescript/src/configs/index.ts',
'packages/eslint-plugin-typescript/src/configs/recommended/*',
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"author": "Cloudflight GmbH",
"license": "Apache-2.0",
"devDependencies": {
"@eslint/compat": "1.1.1",
"@eslint/compat": "1.2.0",
"@eslint/js": "9.11.1",
"@types/eslint": "9.6.1",
"@types/eslint__js": "8.42.3",
Expand All @@ -44,12 +44,12 @@
"eslint-plugin-import": "2.29.1",
"eslint-plugin-json-es": "1.6.0",
"eslint-plugin-jsx-a11y": "6.8.0",
"eslint-plugin-no-unsanitized": "4.0.2",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-n": "17.10.3",
"eslint-plugin-no-unsanitized": "4.1.2",
"eslint-plugin-react": "7.34.1",
"eslint-plugin-react-hooks": "4.6.2",
"eslint-plugin-rxjs": "5.0.3",
"eslint-plugin-security": "3.0.0",
"eslint-plugin-security": "3.0.1",
"eslint-plugin-vue": "9.28.0",
"husky": "9.1.6",
"jest": "29.7.0",
Expand Down
40 changes: 24 additions & 16 deletions packages/eslint-plugin-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ You can find the directory of all rules including their reasoning [here](src/con
The following dependencies are required:

```
"@cloudflight/eslint-plugin-typescript": ">=0.26.0",
"eslint": ">=9.0.0 < 10.0.0"
```

Expand All @@ -25,19 +24,28 @@ In your `package.json` add the following:
}
```

The plugin provides 1 configuration:

- @cloudflight/node/recommended
- Contains rules for Node files

Now open your `.eslintrc.js` and add one of the configurations:

```
require('@rushstack/eslint-patch/modern-module-resolution');
module.exports = {
...
extends: ['plugin:@cloudflight/node/recommended'],
...
};
Now open your `eslint.config.mts` and add one of the configurations:

```ts
import { cloudflightNodeConfig } from '@cloudflight/eslint-plugin-node';
import { includeIgnoreFile } from '@eslint/compat';
import { dirname, normalize, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const gitignorePath = normalize(resolve(__dirname, '.gitignore'));

export default [
includeIgnoreFile(gitignorePath),
...cloudflightNodeConfig,
{
languageOptions: {
parserOptions: {
project: ['tsconfig.*?.json'],
tsconfigRootDir: import.meta.dirname,
},
},
},
];
```
13 changes: 7 additions & 6 deletions packages/eslint-plugin-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@
"publishConfig": {
"access": "public"
},
"private": true,
"scripts": {
"build-packagex": "tsc"
"build-package": "tsc"
},
"dependencies": {
"eslint-plugin-no-unsanitized": "^4.0.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-security": "^3.0.0"
"@cloudflight/eslint-plugin-typescript": "workspace:*",
"eslint-plugin-n": "17.10.3",
"eslint-plugin-no-unsanitized": "^4.1.2",
"eslint-plugin-security": "^3.0.1"
},
"peerDependencies": {
"@cloudflight/eslint-plugin-typescript": "workspace:*",
"eslint": ">=9.0.0 < 10.0.0"
},
"devDependencies": {
"@types/eslint-plugin-no-unsanitized": "workspace:*",
"@types/eslint-plugin-security": "workspace:*",
"typescript": "5.5.4"
}
}
7 changes: 0 additions & 7 deletions packages/eslint-plugin-node/src/configs/index.ts

This file was deleted.

29 changes: 29 additions & 0 deletions packages/eslint-plugin-node/src/configs/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {TSESLint} from '@typescript-eslint/utils';

export const nodeRules: TSESLint.Linter.RulesRecord = {
// require all requires be top-level
'n/global-require': 'error',

// allow typescript files
'n/no-missing-import': [
'error',
{
tryExtensions: ['.js', '.ts', '.json', '.node'],
},
],

// disallow use of new operator with the require function
'n/no-new-require': 'error',

// disallow string concatenation with __dirname and __filename
'n/no-path-concat': 'error',

// disallow use of synchronous methods (off by default)
'n/no-sync': 'error',

// we use typescript, which transpiles es-syntax to cjs-syntax
'n/no-unsupported-features/es-syntax': 'off',

// we use typescript, so node-builtins are typed and checked via TS
'n/no-unsupported-features/node-builtins': 'off',
};

This file was deleted.

27 changes: 27 additions & 0 deletions packages/eslint-plugin-node/src/configs/security.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {TSESLint} from '@typescript-eslint/utils';
import pluginSecurity from 'eslint-plugin-security';

const recommendedRules = pluginSecurity.configs.recommended.rules;
const updatedRules = Object.entries<TSESLint.Linter.RuleEntry>(recommendedRules)
.map(([key, value]) => [key, warningEntryToError(value)]);

// yes, we know this works correctly
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
export const securityRules = Object.fromEntries(updatedRules) as TSESLint.Linter.RulesRecord;

function warningEntryToError(value: TSESLint.Linter.RuleEntry): TSESLint.Linter.RuleEntry {
const level = Array.isArray(value) ? value[0] : value;
const updatedLevel = warningLevelToError(level);

return Array.isArray(value) ? [updatedLevel, value.slice(1)] : updatedLevel;
}

function warningLevelToError(level: TSESLint.Linter.RuleLevel): TSESLint.Linter.RuleLevel {
if (level === 'warn') {
return 'error';
} else if (level === 1) {
return 'error';
}

return level;
}
25 changes: 23 additions & 2 deletions packages/eslint-plugin-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
export {configs} from './configs';
export {rules} from './rules';
import tseslint from 'typescript-eslint';
import { cloudflightTypescriptConfig } from '@cloudflight/eslint-plugin-typescript';
import nounsanitized from 'eslint-plugin-no-unsanitized';
import pluginSecurity from 'eslint-plugin-security';
import pluginNode from 'eslint-plugin-n';
import {securityRules} from './configs/security';
import {nodeRules} from './configs/node';

export const cloudflightNodeConfig = tseslint.config(
{
files: ['**/*.{js,mjs,cjs,ts,mts,cts}'],
extends: [
...cloudflightTypescriptConfig,
pluginNode.configs['flat/recommended'],
nounsanitized.configs.recommended,
pluginSecurity.configs.recommended,
],
rules: {
...nodeRules,
...securityRules,
}
},
);
3 changes: 0 additions & 3 deletions packages/eslint-plugin-node/src/rules/index.ts

This file was deleted.

9 changes: 9 additions & 0 deletions packages/types-eslint-plugin-no-unsanitized/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Linter } from 'eslint';

declare const nounsanitized: {
readonly configs: {
readonly recommended: { readonly rules: Readonly<Linter.RulesRecord> };
};
};

export = nounsanitized;
18 changes: 18 additions & 0 deletions packages/types-eslint-plugin-no-unsanitized/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@types/eslint-plugin-no-unsanitized",
"version": "0.1.0",
"description": "TypeScript definitions for eslint-plugin-no-unsanitized",
"homepage": "https://github.com/cloudflightio/cloudflight-eslint-plugin",
"bugs": {
"url": "https://github.com/cloudflightio/cloudflight-eslint-plugin/issues"
},
"private": true,
"author": "Cloudflight GmbH",
"license": "Apache-2.0",
"main": "",
"types": "index.d.ts",
"dependencies": {
"@types/eslint": "*"
},
"typeScriptVersion": "4.5"
}
9 changes: 9 additions & 0 deletions packages/types-eslint-plugin-security/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Linter } from 'eslint';

declare const pluginSecurity: {
readonly configs: {
readonly recommended: { readonly rules: Readonly<Linter.RulesRecord> };
};
};

export = pluginSecurity;
18 changes: 18 additions & 0 deletions packages/types-eslint-plugin-security/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@types/eslint-plugin-security",
"version": "0.1.0",
"description": "TypeScript definitions for eslint-plugin-security",
"homepage": "https://github.com/cloudflightio/cloudflight-eslint-plugin",
"bugs": {
"url": "https://github.com/cloudflightio/cloudflight-eslint-plugin/issues"
},
"private": true,
"author": "Cloudflight GmbH",
"license": "Apache-2.0",
"main": "",
"types": "index.d.ts",
"dependencies": {
"@types/eslint": "*"
},
"typeScriptVersion": "4.5"
}
Loading

0 comments on commit ea6ff80

Please sign in to comment.