fix(core): handle string and array parserOptions.project values (#17500)

This commit is contained in:
Caleb Ukle 2023-06-14 11:21:34 -05:00 committed by GitHub
parent a21ab98ca8
commit f18d50ca0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 7 deletions

View File

@ -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}` });
});
});

View File

@ -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;
});
}