From f18d50ca0c7bc55dbd95f20b12eb3548cf1e9a14 Mon Sep 17 00:00:00 2001 From: Caleb Ukle Date: Wed, 14 Jun 2023 11:21:34 -0500 Subject: [PATCH] fix(core): handle string and array parserOptions.project values (#17500) --- .../move/lib/update-eslintrc-json.spec.ts | 29 +++++++++++++++++++ .../move/lib/update-eslintrc-json.ts | 20 ++++++++----- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/packages/workspace/src/generators/move/lib/update-eslintrc-json.spec.ts b/packages/workspace/src/generators/move/lib/update-eslintrc-json.spec.ts index f128f69144..c627bad8df 100644 --- a/packages/workspace/src/generators/move/lib/update-eslintrc-json.spec.ts +++ b/packages/workspace/src/generators/move/lib/update-eslintrc-json.spec.ts @@ -200,4 +200,33 @@ describe('updateEslint', () => { }) ); }); + + it('should update .eslintrc.json parserOptions.project as a string', async () => { + await libraryGenerator(tree, { + name: 'my-lib', + linter: Linter.EsLint, + setParserOptionsProject: true, + }); + + // Add another parser project to eslint.json + const storybookProject = '.storybook/tsconfig.json'; + updateJson(tree, '/libs/my-lib/.eslintrc.json', (eslintRcJson) => { + eslintRcJson.overrides[0].parserOptions.project = `libs/my-lib/${storybookProject}`; + return eslintRcJson; + }); + + // This step is usually handled elsewhere + tree.rename( + 'libs/my-lib/.eslintrc.json', + 'libs/shared/my-destination/.eslintrc.json' + ); + const projectConfig = readProjectConfiguration(tree, 'my-lib'); + + updateEslintrcJson(tree, schema, projectConfig); + + expect( + readJson(tree, '/libs/shared/my-destination/.eslintrc.json').overrides[0] + .parserOptions + ).toEqual({ project: `libs/shared/my-destination/${storybookProject}` }); + }); }); diff --git a/packages/workspace/src/generators/move/lib/update-eslintrc-json.ts b/packages/workspace/src/generators/move/lib/update-eslintrc-json.ts index fe385740f1..07b226d110 100644 --- a/packages/workspace/src/generators/move/lib/update-eslintrc-json.ts +++ b/packages/workspace/src/generators/move/lib/update-eslintrc-json.ts @@ -64,14 +64,20 @@ export function updateEslintrcJson( ); } - eslintRcJson.overrides?.forEach((o) => { - if (o.parserOptions?.project) { - o.parserOptions.project = o.parserOptions.project.map((p) => - p.replace(project.root, schema.relativeToRootDestination) - ); + eslintRcJson.overrides?.forEach( + (o: { parserOptions?: { project?: string | string[] } }) => { + if (o.parserOptions?.project) { + o.parserOptions.project = Array.isArray(o.parserOptions.project) + ? o.parserOptions.project.map((p) => + p.replace(project.root, schema.relativeToRootDestination) + ) + : o.parserOptions.project.replace( + project.root, + schema.relativeToRootDestination + ); + } } - }); - + ); return eslintRcJson; }); }