Skip to content

Commit

Permalink
feat(eslint-plugin): renamed exported recommended
Browse files Browse the repository at this point in the history
fix(eslint-plugin): turn yaml parser really optional
deprecate(eslint-plugin): recommended exports based on types
  • Loading branch information
kpanot committed Oct 28, 2024
1 parent 6119e0d commit f475e20
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 5 deletions.
10 changes: 10 additions & 0 deletions packages/@o3r/eslint-plugin/migration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "https://raw.githubusercontent.com/angular/angular-cli/master/packages/angular_devkit/schematics/collection-schema.json",
"schematics": {
"migration-v11_4": {
"version": "11.4.0-alpha.0",
"description": "Updates of @o3r/eslint-plugin to v11.4.*",
"factory": "./schematics/ng-update/v10-0/index#updateV11_4"
}
}
}
5 changes: 4 additions & 1 deletion packages/@o3r/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,8 @@
"typescript": "~5.5.4",
"yaml-eslint-parser": "^1.2.2"
},
"schematics": "./collection.json"
"schematics": "./collection.json",
"ng-update": {
"migrations": "./migration.json"
}
}
26 changes: 26 additions & 0 deletions packages/@o3r/eslint-plugin/schematics/ng-update/v11/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable camelcase, @typescript-eslint/naming-convention */
import { chain, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { updateEslintJsonFile } from './update-configs/update-configs';

/**
* Update of Otter library V10.0
*/
function updateV11_4Fn(): Rule {
return (tree: Tree, context: SchematicContext) => {

const updateRules: Rule[] = [
updateEslintJsonFile()
];

return chain(updateRules)(tree, context);
};
}

/**
* Update of Otter library V10.0
*/
export const updateV11_4 = () => {
return import('@o3r/schematics')
.catch(() => { updateV11_4Fn(); })
.then((schemtics) => schemtics?.createSchematicWithMetricsIfInstalled(updateV11_4Fn));
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Tree } from '@angular-devkit/schematics';
import { updateEslintJsonFile } from './update-configs';

describe('updateEslintJsonFile', () => {
it('should update configs', () => {
const initialTree = Tree.empty();
initialTree.create('random.file', 'a file containing json-recommended');
initialTree.create('/test/.eslintrc.json', '{ "extends": ["@o3r/json-recommended", "@other/json-recommended"] }');
initialTree.create('/src/.eslintrc.json', '{ "extends": ["plugin:@o3r/json-recommended", "@o3r/template-recommended"] }');
const newTree = updateEslintJsonFile()(initialTree, null as any) as Tree;

expect(newTree.readText('random.file')).toContain('json-recommended');
expect(newTree.readText('/test/.eslintrc.json')).toBe('{ "extends": ["@o3r/monorepo-recommended", "@other/json-recommended"] }');
expect(newTree.readText('/src/.eslintrc.json')).toBe('{ "extends": ["plugin:@o3r/monorepo-recommended", "@o3r/angular-template-recommended"] }');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Rule } from '@angular-devkit/schematics';
import { basename } from 'node:path';

/**
* Update Otter cms.json in an Angular Project
*/
export function updateEslintJsonFile(): Rule {
const configFilePattern = /^.?eslint(:?rc)?\..*/;
const toReplace = {
// eslint-disable-next-line @typescript-eslint/naming-convention -- following eslint config name
'angular-template-recommended': /(['"](?:plugin:)?@o3r\/)template-recommended(['"])/g,
// eslint-disable-next-line @typescript-eslint/naming-convention -- following eslint config name
'monorepo-recommended': /(['"](?:plugin:)?@o3r\/)json-recommended(['"])/g
};

return (tree) => {
tree.visit((path) => {
if (configFilePattern.test(basename(path))) {
const content = tree.readText(path);
const newContent = Object.entries(toReplace).reduce((acc, [rule, regexp]) => {
return acc.replaceAll(regexp, `$1${rule}$2`);
}, content);

if (content !== newContent) {
tree.overwrite(path, newContent);
}
}
});

return tree;
};
}
15 changes: 15 additions & 0 deletions packages/@o3r/eslint-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,34 @@ module.exports = {
}
},

// deprecated: should use `angular-template-recommended` instead
'template-recommended': {
rules: {
'@o3r/no-folder-import-for-module': 'error',
'@o3r/template-async-number-limitation': 'warn'
}
},

// deprecated: should use `monorepo-recommended` instead
'json-recommended': {
rules: {
'@o3r/json-dependency-versions-harmonize': 'error'
}
},

'angular-template-recommended': {
rules: {
'@o3r/no-folder-import-for-module': 'error',
'@o3r/template-async-number-limitation': 'warn'
}
},

'monorepo-recommended': {
rules: {
'@o3r/json-dependency-versions-harmonize': 'error'
}
},

'yarn-recommended': {
rules: {
'@o3r/yarnrc-package-extensions-harmonize': 'error'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as path from 'node:path';
import * as semver from 'semver';
import { createRule } from '../../utils';
import { getYamlParserServices } from '../utils';
import { type AST, getStaticYAMLValue } from 'yaml-eslint-parser';
import { findWorkspacePackageJsons, /* getBestRange,*/ getBestRanges } from '../../json/json-dependency-versions-harmonize/version-harmonize';
import type { AST } from 'yaml-eslint-parser';
import { findWorkspacePackageJsons, getBestRanges } from '../../json/json-dependency-versions-harmonize/version-harmonize';

interface Options {
/** List of package.json to ignore when determining the dependencies versions */
Expand Down Expand Up @@ -80,8 +80,9 @@ export default createRule<[Options, ...any], 'versionUpdate' | 'error'>({
},
defaultOptions,
create: (context, [options]: Readonly<[Options, ...any]>) => {
const parser = import('yaml-eslint-parser');
const parserServices = getYamlParserServices(context);
const dirname = path.dirname(context.getFilename());
const dirname = path.dirname(context.filename);
const workspace = findWorkspacePackageJsons(dirname);
const bestRanges = workspace ?
getBestRanges(options.dependencyTypesInPackages, workspace.packages.filter(({ content }) => !content.name || !options.excludePackages.includes(content.name))) :
Expand All @@ -91,7 +92,8 @@ export default createRule<[Options, ...any], 'versionUpdate' | 'error'>({
if (parserServices.isYAML) {
return {
// eslint-disable-next-line @typescript-eslint/naming-convention
'YAMLPair': (node: AST.YAMLPair) => {
'YAMLPair': async (node: AST.YAMLPair) => {
const { getStaticYAMLValue } = await parser;

if (node.value) {
const range = getStaticYAMLValue(node.value)?.toString();
Expand Down
3 changes: 3 additions & 0 deletions packages/@o3r/eslint-plugin/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"references": [
{
"path": "./tsconfig.build.json"
},
{
"path": "./tsconfig.builders.json"
}
],
"include": [
Expand Down

0 comments on commit f475e20

Please sign in to comment.