From dae499b19d8d6ecace087e83a0ba74ed1de3c37e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Fontenele?= Date: Mon, 9 May 2022 23:29:14 +0200 Subject: [PATCH] fix(typescript): eslint task lints projenrcts (#1832) fixes #1831 BREAKING CHANGE: EslintOptions.lintProjenRc is now a string --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. --- docs/api/API.md | 4 +++- src/javascript/eslint.ts | 14 +++++++++++--- src/typescript/typescript.ts | 15 ++++++++++----- test/typescript/typescript.test.ts | 19 +++++++++++++++++++ 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/docs/api/API.md b/docs/api/API.md index aa5e62af52a..cd6acceade6 100644 --- a/docs/api/API.md +++ b/docs/api/API.md @@ -7017,6 +7017,7 @@ new javascript.Eslint(project: NodeProject, options: EslintOptions) * **fileExtensions** (Array) File types that should be linted (e.g. [ ".js", ".ts" ]). __*Default*__: [".ts"] * **ignorePatterns** (Array) List of file patterns that should not be linted, using the same syntax as .gitignore patterns. __*Default*__: [ '*.js', '*.d.ts', 'node_modules/', '*.generated.ts', 'coverage' ] * **lintProjenRc** (boolean) Should we lint .projenrc.js. __*Default*__: true + * **lintProjenRcFile** (string) Projenrc file to lint. __*Default*__: PROJEN_RC * **prettier** (boolean) Enable prettier for code formatting. __*Default*__: false * **tsAlwaysTryTypes** (boolean) Always try to resolve types under `@types` directory even it doesn't contain any source code. __*Default*__: true * **tsconfigPath** (string) Path to `tsconfig.json` which should be used by eslint. __*Default*__: "./tsconfig.json" @@ -14972,7 +14973,8 @@ Name | Type | Description **devdirs**?🔹 | Array | Directories with source files that include tests and build tools.
__*Default*__: [] **fileExtensions**?🔹 | Array | File types that should be linted (e.g. [ ".js", ".ts" ]).
__*Default*__: [".ts"] **ignorePatterns**?🔹 | Array | List of file patterns that should not be linted, using the same syntax as .gitignore patterns.
__*Default*__: [ '*.js', '*.d.ts', 'node_modules/', '*.generated.ts', 'coverage' ] -**lintProjenRc**?🔹 | boolean | Should we lint .projenrc.js.
__*Default*__: true +**lintProjenRc**?⚠️ | boolean | Should we lint .projenrc.js.
__*Default*__: true +**lintProjenRcFile**?🔹 | string | Projenrc file to lint.
__*Default*__: PROJEN_RC **prettier**?🔹 | boolean | Enable prettier for code formatting.
__*Default*__: false **tsAlwaysTryTypes**?🔹 | boolean | Always try to resolve types under `@types` directory even it doesn't contain any source code.
__*Default*__: true **tsconfigPath**?🔹 | string | Path to `tsconfig.json` which should be used by eslint.
__*Default*__: "./tsconfig.json" diff --git a/src/javascript/eslint.ts b/src/javascript/eslint.ts index 9c777668d8f..84de4c4776b 100644 --- a/src/javascript/eslint.ts +++ b/src/javascript/eslint.ts @@ -38,9 +38,16 @@ export interface EslintOptions { */ readonly ignorePatterns?: string[]; + /** + * Projenrc file to lint. Use empty string to disable. + * @default PROJEN_RC + */ + readonly lintProjenRcFile?: string; + /** * Should we lint .projenrc.js * @default true + * @deprecated use lintProjenRcFile instead */ readonly lintProjenRc?: boolean; @@ -150,6 +157,7 @@ export class Eslint extends Component { this._allowDevDeps = new Set((devdirs ?? []).map((dir) => `**/${dir}/**`)); const lintProjenRc = options.lintProjenRc ?? true; + const lintProjenRcFile = options.lintProjenRcFile ?? PROJEN_RC; const eslint = project.addTask("eslint", { description: "Runs eslint against the codebase", @@ -159,7 +167,7 @@ export class Eslint extends Component { "--fix", "--no-error-on-unmatched-pattern", ...dirs, - ...(lintProjenRc ? [PROJEN_RC] : []), + ...(lintProjenRc && lintProjenRcFile ? [lintProjenRcFile] : []), ].join(" "), }); @@ -296,7 +304,7 @@ export class Eslint extends Component { // Overrides for .projenrc.js this.overrides = [ { - files: [PROJEN_RC], + files: [lintProjenRcFile || PROJEN_RC], rules: { "@typescript-eslint/no-require-imports": "off", "import/no-extraneous-dependencies": "off", @@ -306,7 +314,7 @@ export class Eslint extends Component { this.ignorePatterns = options.ignorePatterns ?? [ "*.js", - `!${PROJEN_RC}`, + `!${lintProjenRcFile || PROJEN_RC}`, "*.d.ts", "node_modules/", "*.generated.ts", diff --git a/src/typescript/typescript.ts b/src/typescript/typescript.ts index 21010ac058f..26491d35040 100644 --- a/src/typescript/typescript.ts +++ b/src/typescript/typescript.ts @@ -318,12 +318,22 @@ export class TypeScriptProject extends NodeProject { } } + const projenrcTypeScript = options.projenrcTs ?? false; + if (projenrcTypeScript) { + new ProjenrcTs(this, options.projenrcTsOptions); + } + + const projenRcFilename = projenrcTypeScript + ? options.projenrcTsOptions?.filename ?? ".projenrc.ts" + : undefined; + if (options.eslint ?? true) { this.eslint = new Eslint(this, { tsconfigPath: `./${this.tsconfigDev.fileName}`, dirs: [this.srcdir], devdirs: [this.testdir, "build-tools"], fileExtensions: [".ts", ".tsx"], + lintProjenRcFile: projenRcFilename, ...options.eslintOptions, }); @@ -356,11 +366,6 @@ export class TypeScriptProject extends NodeProject { if (this.docgen) { new TypedocDocgen(this); } - - const projenrcTypeScript = options.projenrcTs ?? false; - if (projenrcTypeScript) { - new ProjenrcTs(this, options.projenrcTsOptions); - } } /** diff --git a/test/typescript/typescript.test.ts b/test/typescript/typescript.test.ts index d346a9c6455..70edd20b8f8 100644 --- a/test/typescript/typescript.test.ts +++ b/test/typescript/typescript.test.ts @@ -166,6 +166,25 @@ test("projenrc.ts", () => { }); }); +test("projenrc.ts linted by eslint task", () => { + const prj = new TypeScriptProject({ + name: "test", + defaultReleaseBranch: "main", + projenrcTs: true, + }); + + const snapshot = synthSnapshot(prj); + expect(snapshot[".projen/tasks.json"].tasks.eslint).toStrictEqual({ + description: "Runs eslint against the codebase", + name: "eslint", + steps: [ + { + exec: "eslint --ext .ts,.tsx --fix --no-error-on-unmatched-pattern src test build-tools .projenrc.ts", + }, + ], + }); +}); + test("upgrade task ignores pinned versions", () => { const prj = new TypeScriptProject({ defaultReleaseBranch: "main",