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 4, 2024
1 parent 8f77e07 commit 7c5a549
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 90 deletions.
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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-node/src/configs/node.ts',
'packages/eslint-plugin-react/*',
'packages/eslint-plugin-typescript/src/configs/index.ts',
'packages/eslint-plugin-typescript/src/configs/recommended/*',
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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-no-unsanitized": "4.1.2",
"eslint-plugin-node": "11.1.0",
"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,
},
},
},
];
```
12 changes: 6 additions & 6 deletions packages/eslint-plugin-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@
"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-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
'node/global-require': 'error',

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

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

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

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

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

// we use typescript, so node-builtins are typed and checked via TS
'node/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;
}
24 changes: 22 additions & 2 deletions packages/eslint-plugin-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
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 {securityRules} from './configs/security';

export const cloudflightNodeConfig = tseslint.config(
...cloudflightTypescriptConfig,
{
files: ['**/*.{js,mjs,cjs,ts,mts,cts}'],
extends: [
...cloudflightTypescriptConfig,
nounsanitized.configs.recommended,
pluginSecurity.configs.recommended,
],
rules: {
// todo: eslint-plugin-node has not been migrated to support v9 yet
// ...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"
}
49 changes: 33 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -531,12 +531,13 @@ __metadata:
version: 0.0.0-use.local
resolution: "@cloudflight/eslint-plugin-node@workspace:packages/eslint-plugin-node"
dependencies:
eslint-plugin-no-unsanitized: "npm:^4.0.2"
eslint-plugin-node: "npm:^11.1.0"
eslint-plugin-security: "npm:^3.0.0"
"@cloudflight/eslint-plugin-typescript": "workspace:*"
"@types/eslint-plugin-no-unsanitized": "workspace:*"
"@types/eslint-plugin-security": "workspace:*"
eslint-plugin-no-unsanitized: "npm:^4.1.2"
eslint-plugin-security: "npm:^3.0.1"
typescript: "npm:5.5.4"
peerDependencies:
"@cloudflight/eslint-plugin-typescript": "workspace:*"
eslint: ">=9.0.0 < 10.0.0"
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -606,12 +607,12 @@ __metadata:
eslint-plugin-import: "npm:2.29.1"
eslint-plugin-json-es: "npm:1.6.0"
eslint-plugin-jsx-a11y: "npm:6.8.0"
eslint-plugin-no-unsanitized: "npm:4.0.2"
eslint-plugin-no-unsanitized: "npm:4.1.2"
eslint-plugin-node: "npm:11.1.0"
eslint-plugin-react: "npm:7.34.1"
eslint-plugin-react-hooks: "npm:4.6.2"
eslint-plugin-rxjs: "npm:5.0.3"
eslint-plugin-security: "npm:3.0.0"
eslint-plugin-security: "npm:3.0.1"
eslint-plugin-vue: "npm:9.28.0"
husky: "npm:9.1.6"
jest: "npm:29.7.0"
Expand Down Expand Up @@ -1196,6 +1197,22 @@ __metadata:
languageName: node
linkType: hard

"@types/eslint-plugin-no-unsanitized@workspace:*, @types/eslint-plugin-no-unsanitized@workspace:packages/types-eslint-plugin-no-unsanitized":
version: 0.0.0-use.local
resolution: "@types/eslint-plugin-no-unsanitized@workspace:packages/types-eslint-plugin-no-unsanitized"
dependencies:
"@types/eslint": "npm:*"
languageName: unknown
linkType: soft

"@types/eslint-plugin-security@workspace:*, @types/eslint-plugin-security@workspace:packages/types-eslint-plugin-security":
version: 0.0.0-use.local
resolution: "@types/eslint-plugin-security@workspace:packages/types-eslint-plugin-security"
dependencies:
"@types/eslint": "npm:*"
languageName: unknown
linkType: soft

"@types/eslint@npm:*, @types/eslint@npm:9.6.1":
version: 9.6.1
resolution: "@types/eslint@npm:9.6.1"
Expand Down Expand Up @@ -2934,16 +2951,16 @@ __metadata:
languageName: node
linkType: hard

"eslint-plugin-no-unsanitized@npm:4.0.2, eslint-plugin-no-unsanitized@npm:^4.0.2":
version: 4.0.2
resolution: "eslint-plugin-no-unsanitized@npm:4.0.2"
"eslint-plugin-no-unsanitized@npm:4.1.2, eslint-plugin-no-unsanitized@npm:^4.1.2":
version: 4.1.2
resolution: "eslint-plugin-no-unsanitized@npm:4.1.2"
peerDependencies:
eslint: ^6 || ^7 || ^8
checksum: 10c0/6187b731e8130297975becaded7b5d7060795c13a4ffb756f5014f288c1d0f536d59e3ef1a572b41f8249bf80dd13391a50d5718ca4ac8348f374bf0d9f70fdf
eslint: ^8 || ^9
checksum: 10c0/c62bbfc02e8a73adc7ff9fdbbf8131a2a843eaebc6e8eb5f311c4db8a641bb46535bd066052857c7daea6c6d2a6c38bec66e06ad5704319083a1e2c467e71a4a
languageName: node
linkType: hard

"eslint-plugin-node@npm:11.1.0, eslint-plugin-node@npm:^11.1.0":
"eslint-plugin-node@npm:11.1.0":
version: 11.1.0
resolution: "eslint-plugin-node@npm:11.1.0"
dependencies:
Expand Down Expand Up @@ -3016,12 +3033,12 @@ __metadata:
languageName: node
linkType: hard

"eslint-plugin-security@npm:3.0.0, eslint-plugin-security@npm:^3.0.0":
version: 3.0.0
resolution: "eslint-plugin-security@npm:3.0.0"
"eslint-plugin-security@npm:3.0.1, eslint-plugin-security@npm:^3.0.1":
version: 3.0.1
resolution: "eslint-plugin-security@npm:3.0.1"
dependencies:
safe-regex: "npm:^2.1.1"
checksum: 10c0/b7fe64fa912224bb2f5a2d112708320dada5a2b04a19e24384f2bc543fa10363955f93ed8eec70cc1af5319cb15932b1db402809167f301c31555022b2fd5638
checksum: 10c0/6b85feabe389b73e0a5961abfeac79214d61699249c990c9a66628a5e45870b49f1ab0be63223dfd75c4046ff9632f42a963790616f66e2c6d59b6c24643c5e0
languageName: node
linkType: hard

Expand Down

0 comments on commit 7c5a549

Please sign in to comment.