Skip to content

Commit

Permalink
fix(linter): convert parser options to flat config even is parser is …
Browse files Browse the repository at this point in the history
…missing
  • Loading branch information
meeroslav committed Mar 19, 2024
1 parent f36e038 commit 76d553f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,70 @@ describe('convert-to-flat-config generator', () => {
expect(tree.exists('eslint.config.js')).toBeTruthy();
expect(tree.exists('libs/test-lib/eslint.config.js')).toBeTruthy();
});

it('should handle parser options even if parser is extended', async () => {
addProjectConfiguration(tree, 'dx-assets-ui', {
root: 'apps/dx-assets-ui',
targets: {},
});
await lintProjectGenerator(tree, {
skipFormat: false,
linter: Linter.EsLint,

project: 'dx-assets-ui',
setParserOptionsProject: false,
});
updateJson(tree, 'apps/dx-assets-ui/.eslintrc.json', () => {
return {
extends: ['../../.eslintrc.json'],
ignorePatterns: ['!**/*', '__fixtures__/**/*'],
overrides: [
{
files: ['*.ts', '*.tsx', '*.js', '*.jsx'],
parserOptions: {
project: ['apps/dx-assets-ui/tsconfig.*?.json'],
},
rules: {},
},
{
files: ['*.ts', '*.tsx'],
rules: {},
},
{
files: ['*.js', '*.jsx'],
rules: {},
},
],
};
});

await convertToFlatConfigGenerator(tree, options);
expect(tree.exists('apps/dx-assets-ui/eslint.config.js')).toBeTruthy();
expect(tree.exists('eslint.config.js')).toBeTruthy();
expect(tree.read('apps/dx-assets-ui/eslint.config.js', 'utf-8'))
.toMatchInlineSnapshot(`
"const baseConfig = require('../../eslint.config.js');
module.exports = [
...baseConfig,
{
files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx'],
rules: {},
languageSettings: {
parserOptions: { project: ['apps/dx-assets-ui/tsconfig.*?.json'] },
},
},
{
files: ['**/*.ts', '**/*.tsx'],
rules: {},
},
{
files: ['**/*.js', '**/*.jsx'],
rules: {},
},
{ ignores: ['__fixtures__/**/*'] },
];
"
`);
});
});
11 changes: 10 additions & 1 deletion packages/eslint/src/generators/utils/flat-config/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,16 +760,25 @@ export function generateFlatOverride(
!override.plugins &&
!override.parser
) {
if (override.parserOptions) {
const { parserOptions, ...rest } = override;
return generateAst({ ...rest, languageSettings: { parserOptions } });
}
return generateAst(override);
}
const { files, excludedFiles, rules, ...rest } = override;
const { files, excludedFiles, rules, parserOptions, ...rest } = override;

const objectLiteralElements: ts.ObjectLiteralElementLike[] = [
ts.factory.createSpreadAssignment(ts.factory.createIdentifier('config')),
];
addTSObjectProperty(objectLiteralElements, 'files', files);
addTSObjectProperty(objectLiteralElements, 'excludedFiles', excludedFiles);
addTSObjectProperty(objectLiteralElements, 'rules', rules);
if (parserOptions) {
addTSObjectProperty(objectLiteralElements, 'languageSettings', {
parserOptions,
});
}

return ts.factory.createSpreadElement(
ts.factory.createCallExpression(
Expand Down

0 comments on commit 76d553f

Please sign in to comment.