Skip to content

Commit

Permalink
fix(linter): scope ts typescript-eslint rules to the relevant files a…
Browse files Browse the repository at this point in the history
…nd provide globals for jest
  • Loading branch information
leosvelperez committed Oct 9, 2024
1 parent db10812 commit c960209
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 8 deletions.
5 changes: 4 additions & 1 deletion packages/eslint-plugin/src/flat-configs/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ const isPrettierAvailable =
*/
export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
{
files: ['**/*.js', '**/*.jsx'],
extends: tseslint.configs.recommended,
},
...(isPrettierAvailable ? [require('eslint-config-prettier')] : []),
{
languageOptions: {
Expand Down
5 changes: 4 additions & 1 deletion packages/eslint-plugin/src/flat-configs/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const isPrettierAvailable =
*/
export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
{
files: ['**/*.ts', '**/*.tsx'],
extends: tseslint.configs.recommended,
},
...(isPrettierAvailable ? [require('eslint-config-prettier')] : []),
{
plugins: { '@typescript-eslint': tseslint.plugin },
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"typescript",
"eslint",
"@angular-devkit/core",
"@typescript-eslint/eslint-plugin"
"@typescript-eslint/eslint-plugin",
"globals"
]
}
]
Expand Down
12 changes: 12 additions & 0 deletions packages/eslint/src/generators/init/global-eslint-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const getGlobalEsLintConfiguration = (
};

export const getGlobalFlatEslintConfiguration = (
unitTestRunner?: string,
rootProject?: boolean
): string => {
const nodeList = createNodeList(new Map(), []);
Expand Down Expand Up @@ -150,5 +151,16 @@ export const getGlobalFlatEslintConfiguration = (
})
);

if (unitTestRunner === 'jest') {
content = addImportToFlatConfig(content, 'globals', 'globals');
content = addBlockToFlatConfigExport(
content,
generateFlatOverride(
{ files: jestOverride.files },
{ languageOptions: { globals: ['jest'] } }
)
);
}

return content;
};
2 changes: 1 addition & 1 deletion packages/eslint/src/generators/init/init-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function migrateConfigToMonorepoStyle(
tree.exists('eslint.config.js')
? 'eslint.base.config.js'
: 'eslint.config.js',
getGlobalFlatEslintConfiguration()
getGlobalFlatEslintConfiguration(unitTestRunner)
);
} else {
const eslintFile = findEslintFile(tree, '.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ function setUpLegacyRootEslintRc(tree: Tree, options: SetupRootEsLintOptions) {
function setUpRootFlatConfig(tree: Tree, options: SetupRootEsLintOptions) {
tree.write(
'eslint.config.js',
getGlobalFlatEslintConfiguration(options.rootProject)
getGlobalFlatEslintConfiguration(
options.unitTestRunner,
options.rootProject
)
);

return !options.skipPackageJson
Expand Down
26 changes: 26 additions & 0 deletions packages/eslint/src/generators/utils/flat-config/ast-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,32 @@ describe('ast-utils', () => {
}
}))"
`);

// Generate the correct globals using the flat config specific options
const result = printTsNode(
generateFlatOverride(
{
files: ['*.spec.ts', '*.spec.tsx', '*.spec.js', '*.spec.jsx'],
rules: {},
},
{ languageOptions: { globals: ['jest'] } }
)
);
expect(result).toMatchInlineSnapshot(`
"{
files: [
"**/*.spec.ts",
"**/*.spec.tsx",
"**/*.spec.js",
"**/*.spec.jsx"
],
languageOptions: { globals: {
...globals.jest
} },
// Override or add rules here
rules: {}
}"
`);
});
});

Expand Down
40 changes: 37 additions & 3 deletions packages/eslint/src/generators/utils/flat-config/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
StringChange,
} from '@nx/devkit';
import { Linter } from 'eslint';
import type * as globals from 'globals';
import * as ts from 'typescript';
import { mapFilePath } from './path-utils';

Expand Down Expand Up @@ -902,6 +903,11 @@ export function overrideNeedsCompat(
export function generateFlatOverride(
_override: Partial<Linter.ConfigOverride<Linter.RulesRecord>> & {
ignores?: Linter.FlatConfig['ignores'];
},
flatOptions?: {
languageOptions?: {
globals: Array<keyof typeof globals>;
};
}
): ts.ObjectLiteralExpression | ts.SpreadElement {
const override = mapFilePaths(_override);
Expand All @@ -914,14 +920,27 @@ export function generateFlatOverride(
files = [files];
}

const flatConfigOverride: Linter.FlatConfig = {
const flatConfigOverride: Omit<Linter.FlatConfig, 'languageOptions'> & {
languageOptions?: Omit<
Linter.FlatConfig['languageOptions'],
'globals'
> & {
globals?: Array<keyof typeof globals>;
};
} = {
files,
};

if (override.ignores) {
flatConfigOverride.ignores = override.ignores;
}

if (flatOptions?.languageOptions?.globals) {
flatConfigOverride.languageOptions = {
globals: flatOptions.languageOptions.globals,
};
}

if (override.rules) {
flatConfigOverride.rules = override.rules;
}
Expand Down Expand Up @@ -959,7 +978,7 @@ export function generateFlatOverride(
}

return generateAst(flatConfigOverride, {
keyToMatch: /^(parser|rules)$/,
keyToMatch: /^(parser|rules|globals)$/,
replacer: (propertyAssignment, propertyName) => {
if (propertyName === 'rules') {
// Add comment that user can override rules if there are no overrides.
Expand All @@ -978,7 +997,7 @@ export function generateFlatOverride(
);
}
return propertyAssignment;
} else {
} else if (propertyName === 'parser') {
// Change parser to require statement.
return ts.factory.createPropertyAssignment(
'parser',
Expand All @@ -994,6 +1013,21 @@ export function generateFlatOverride(
]
)
);
} else if (propertyName === 'globals') {
return ts.factory.createPropertyAssignment(
ts.factory.createIdentifier('globals'),
ts.factory.createObjectLiteralExpression(
flatConfigOverride.languageOptions.globals.map((g) =>
ts.factory.createSpreadAssignment(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier('globals'),
ts.factory.createIdentifier(g)
)
)
),
true
)
);
}
},
});
Expand Down

0 comments on commit c960209

Please sign in to comment.