Skip to content

Commit

Permalink
refactor: enforce consistent Pipe prefix (#1625)
Browse files Browse the repository at this point in the history
* generate new pipes with correct prefix

BREAKING CHANGES: Pipes `sanitize`, `makeHref` and `htmlEncode` are renamed using an `ish` prefix (see Migrations / From 5.1 to 5.2 for more details).
  • Loading branch information
dhhyi authored Apr 22, 2024
1 parent 2cadc4b commit e845597
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
"style": "camelCase"
}
],
"@angular-eslint/pipe-prefix": [
"error",
{
"prefixes": ["ish"]
}
],
"@angular-eslint/sort-ngmodule-metadata-arrays": "warn",
"@ngrx/avoid-combining-selectors": "off",
"@ngrx/avoid-mapping-selectors": "off",
Expand Down
4 changes: 4 additions & 0 deletions docs/guides/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ kb_sync_latest_only

The store action and method `addBasketToNewOrderTemplate` of the OrderTemplatesFacade have been renamed to `createOrderTemplateFromLineItems` and refactored slightly.

The Intershop PWA specific Pipes `sanitize`, `makeHref` and `htmlEncode` were renamed using the common `ish` prefix that is used for the other custom Pipes as well.
When migrating all occurrences of these Pipes need to be renamed to `ishSanitize`, `ishMakeHref` and `ishHtmlEncode`.
The code generation was adapted to generate new Pipes from the beginning with the correct prefixes now.

## From 5.0 to 5.1

The OrderListComponent is strictly presentational, components using it have to supply the data.
Expand Down
6 changes: 6 additions & 0 deletions schematics/customization/add/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,16 @@ const addPrefixToRule = ruleConfig => {
ruleConfig.prefix = ruleConfig.prefix.filter((val, idx, arr) => arr.indexOf(val) === idx);
};

const addPrefixesToRule = ruleConfig => {
ruleConfig.prefixes.push('custom');
ruleConfig.prefixes = ruleConfig.prefixes.filter((val, idx, arr) => arr.indexOf(val) === idx);
};

const eslintJson = parse(fs.readFileSync('./.eslintrc.json', { encoding: 'UTF-8' }));
const overrideRules = eslintJson.overrides.find(c => c.files.includes('*.ts')).rules;
addPrefixToRule(overrideRules['@angular-eslint/directive-selector'][1]);
addPrefixToRule(overrideRules['@angular-eslint/component-selector'][1]);
addPrefixesToRule(overrideRules['@angular-eslint/pipe-prefix'][1]);

const reusePatterns = overrideRules['ish-custom-rules/project-structure'][1].reusePatterns;
reusePatterns.theme = reusePatterns.theme.replace('b2b|b2c', `b2b|b2c|${theme}`);
Expand Down
9 changes: 8 additions & 1 deletion schematics/src/pipe/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import {
} from '@angular-devkit/schematics';
import { PWAPipeOptionsSchema as Options } from 'schemas/pipe/schema';

import { applyNameAndPath, detectExtension, determineArtifactName, findDeclaringModule } from '../utils/common';
import {
applyNameAndPath,
detectExtension,
determineArtifactName,
findDeclaringModule,
generateSelector,
} from '../utils/common';
import { applyLintFix } from '../utils/lint-fix';
import { addDeclarationToNgModule, addExportToNgModule, addProviderToNgModule } from '../utils/registration';

Expand All @@ -23,6 +29,7 @@ export function createPipe(options: Options): Rule {
options = await detectExtension('pipe', host, options);
options = await applyNameAndPath('pipe', host, options);
options = determineArtifactName('pipe', host, options);
options = await generateSelector(host, options);

if (!options.extension) {
options.module = `core/pipes.module`;
Expand Down
8 changes: 8 additions & 0 deletions schematics/src/pipe/factory_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,12 @@ export class PipesModule { }
const content = tree.readContent('/src/app/extensions/feature/feature.module.ts');
expect(content).toContain('FooPipe');
});

it('should name the pipe with the correct prefix', async () => {
const options = { ...defaultOptions, name: 'foo' };

const tree = await schematicRunner.runSchematic('pipe', options, appTree);
const content = tree.readContent('/src/app/core/pipes/foo.pipe.ts');
expect(content).toContain('ishFoo');
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: '<%= camelize(name) %>', pure: true })
@Pipe({ name: '<%= camelize(selector) %>', pure: true })
export class <%= classify(name) %>Pipe implements PipeTransform {
transform(value: boolean): string {
return 'test: ' + (value ? 'okay' : 'failed');
Expand Down
4 changes: 4 additions & 0 deletions schematics/src/pipe/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"$source": "workingDirectory"
}
},
"prefix": {
"type": "string",
"visible": false
},
"restricted": {
"type": "boolean",
"visible": false,
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/pipes/html-encode.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Pipe, PipeTransform } from '@angular/core';
* so they can be displayed as plain text in a web page.
* https://jasonwatmore.com/vanilla-js-html-encode-in-javascript
*/
@Pipe({ name: 'htmlEncode', pure: true })
@Pipe({ name: 'ishHtmlEncode', pure: true })
export class HtmlEncodePipe implements PipeTransform {
transform(value: string): string {
return value.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/pipes/make-href.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { map, switchMap } from 'rxjs/operators';
import { omit } from 'ish-core/utils/functions';
import { MultiSiteService } from 'ish-core/utils/multi-site/multi-site.service';

@Pipe({ name: 'makeHref', pure: false })
@Pipe({ name: 'ishMakeHref', pure: false })
export class MakeHrefPipe implements PipeTransform {
constructor(private multiSiteService: MultiSiteService) {}
transform(location: LocationStrategy, urlParams: Record<string, string>): Observable<string> {
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/pipes/sanitize.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'sanitize', pure: true })
@Pipe({ name: 'ishSanitize', pure: true })
export class SanitizePipe implements PipeTransform {
transform(value: string): string {
return value !== undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h1
[innerHTML]="
'account.overview.personal_message_b2b.text'
| translate : { '0': user.firstName + '&nbsp;' + user.lastName, '1': customer.companyName | htmlEncode }
| translate : { '0': user.firstName + '&nbsp;' + user.lastName, '1': customer.companyName | ishHtmlEncode }
"
data-testing-id="personal-message-b2b"
></h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ <h1 class="h2">{{ 'search.noResult.heading' | translate }}</h1>

<p
class="no-search-result-title"
[innerHTML]="'search.noResult.message' | translate : { '0': searchTerm | htmlEncode }"
[innerHTML]="'search.noResult.message' | translate : { '0': searchTerm | ishHtmlEncode }"
></p>

<div class="search-container main-search-container">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[title]="facet.displayName + ' (' + facet.count + ')'"
(click)="filter(facet)"
(keypress.down)="filter(facet)"
[attr.data-testing-id]="'filter-link-' + (facet.displayName | sanitize)"
[attr.data-testing-id]="'filter-link-' + (facet.displayName | ishSanitize)"
tabindex="0"
>
<span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
type="button"
class="btn btn-link btn-link-action"
(click)="filter(facet)"
[attr.data-testing-id]="'filter-link-' + (facet.name | sanitize)"
[attr.data-testing-id]="'filter-link-' + (facet.name | ishSanitize)"
>
<span class="filter-item-name"> {{ facet.displayName }} </span>
<span class="count"> ({{ facet.count }}) </span>
Expand All @@ -23,7 +23,7 @@
type="button"
class="filter-item-name btn btn-link btn-link-action"
(click)="filter(facet)"
[attr.data-testing-id]="'filter-link-' + (facet.name | sanitize)"
[attr.data-testing-id]="'filter-link-' + (facet.name | ishSanitize)"
>
{{ facet.displayName }}
<ng-container *ngIf="facet.level === maxLevel"> ({{ facet.count }}) </ng-container>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<ul *ngIf="availableLocales.length">
<ng-container *ngFor="let l of availableLocales">
<li *ngIf="l !== locale">
<a [href]="location | makeHref : { lang: l } | async" (click)="setLocaleCookie(l)" role="menuitem">
<a [href]="location | ishMakeHref : { lang: l } | async" (click)="setLocaleCookie(l)" role="menuitem">
{{ 'locale.' + l + '.long' | translate }}
</a>
</li>
Expand Down

0 comments on commit e845597

Please sign in to comment.