Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(migrations): add migration to replace /deep/ with ::ng-deep #42214

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/schematics/BUILD.bazel
Expand Up @@ -13,6 +13,7 @@ pkg_npm(
"//packages/core/schematics/migrations/abstract-control-parent",
"//packages/core/schematics/migrations/activated-route-snapshot-fragment",
"//packages/core/schematics/migrations/can-activate-with-redirect-to",
"//packages/core/schematics/migrations/deep-shadow-piercing-selector",
"//packages/core/schematics/migrations/dynamic-queries",
"//packages/core/schematics/migrations/initial-navigation",
"//packages/core/schematics/migrations/missing-injectable",
Expand Down
5 changes: 5 additions & 0 deletions packages/core/schematics/migrations.json
Expand Up @@ -94,6 +94,11 @@
"version": "12.0.0-next.6",
"description": "`XhrFactory` has been moved from `@angular/common/http` to `@angular/common`.",
"factory": "./migrations/xhr-factory/index"
},
"migration-v12-deep-shadow-piercing-selector": {
"version": "12.0.2",
"description": "Automatically migrates shadow-piercing selector from `/deep/` to the recommanded alternative `::ng-deep`.",
"factory": "./migrations/deep-shadow-piercing-selector/index"
}
}
}
@@ -0,0 +1,16 @@
load("//tools:defaults.bzl", "ts_library")

ts_library(
name = "deep-shadow-piercing-selector",
srcs = glob(["**/*.ts"]),
tsconfig = "//packages/core/schematics:tsconfig.json",
visibility = [
"//packages/core/schematics:__pkg__",
"//packages/core/schematics/migrations/google3:__pkg__",
"//packages/core/schematics/test:__pkg__",
],
deps = [
"@npm//@angular-devkit/core",
"@npm//@angular-devkit/schematics",
],
)
@@ -0,0 +1,17 @@
## shadow-piercing selector `/deep/` to `::ng-deep`

Automatically migrates shadow-piercing selector from `/deep/` to `::ng-deep`.

#### Before
```css
:host /deep/ * {
cursor: pointer;
}
```

#### After
```css
:host ::ng-deep * {
cursor: pointer;
}
```
@@ -0,0 +1,41 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {extname, join} from '@angular-devkit/core';
import {DirEntry, Rule} from '@angular-devkit/schematics';

const VALID_EXTENSIONS = ['.scss', '.sass', '.css', '.styl', '.less', '.ts'];

function* visitFiles(directory: DirEntry): IterableIterator<string> {
for (const path of directory.subfiles) {
const extension = extname(path);
if (VALID_EXTENSIONS.includes(extension)) {
yield join(directory.path, path);
}
}

for (const path of directory.subdirs) {
if (path === 'node_modules' || path.startsWith('.') || path === 'dist') {
continue;
}

yield* visitFiles(directory.dir(path));
}
}

export default function(): Rule {
return (tree) => {
// Visit all files in an Angular workspace monorepo.
for (const file of visitFiles(tree.root)) {
const content = tree.read(file)?.toString();
if (content?.includes('/deep/ ')) {
tree.overwrite(file, content.replace(/\/deep\/ /g, '::ng-deep '));
}
}
};
}
1 change: 1 addition & 0 deletions packages/core/schematics/test/BUILD.bazel
Expand Up @@ -11,6 +11,7 @@ ts_library(
"//packages/core/schematics/migrations/abstract-control-parent",
"//packages/core/schematics/migrations/activated-route-snapshot-fragment",
"//packages/core/schematics/migrations/can-activate-with-redirect-to",
"//packages/core/schematics/migrations/deep-shadow-piercing-selector",
"//packages/core/schematics/migrations/dynamic-queries",
"//packages/core/schematics/migrations/initial-navigation",
"//packages/core/schematics/migrations/missing-injectable",
Expand Down
@@ -0,0 +1,69 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {tags} from '@angular-devkit/core';
import {EmptyTree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';

describe('`/deep/` to `::ng-deep` migration', () => {
let tree: UnitTestTree;
const runner = new SchematicTestRunner('test', require.resolve('../migrations.json'));

beforeEach(() => {
tree = new UnitTestTree(new EmptyTree());
});

it(`should replace '/deep/' with '::ng-deep' in inline component styles`, async () => {
const fileName = '/index.ts';
const getFileContent = (contentToReplace: string) => tags.stripIndents`
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: '<router-outlet></router-outlet>',
styles: ['` +
contentToReplace + `'],
})
export class AppComponent { }
`;

tree.create(fileName, getFileContent(':host /deep/ * { cursor: pointer; }'));
await runMigration();
expect(tree.readContent(fileName))
.toBe(getFileContent(':host ::ng-deep * { cursor: pointer; }'));
});

for (const styleExtension of ['scss', 'sass', 'css', 'styl', 'less']) {
it(`should replace '/deep/' with '::ng-deep' in ${styleExtension} file`, async () => {
const fileName = `/index.${styleExtension}`;
tree.create(fileName, ':host /deep/ * { cursor: pointer; }');
await runMigration();
expect(tree.readContent(fileName)).toBe(':host ::ng-deep * { cursor: pointer; }');
});
}

it(`should replace '/deep/' with '::ng-deep' when used as root selector`, async () => {
const fileName = '/index.css';
tree.create(fileName, '/deep/ * { cursor: pointer; }');
await runMigration();
expect(tree.readContent(fileName)).toBe('::ng-deep * { cursor: pointer; }');
});

it(`should not replace '/deep/' with '::ng-deep' in unknown file extension`, async () => {
const fileName = '/index.foo';
const content = 'this is a not /deep/ selector';
tree.create(fileName, content);
await runMigration();
expect(tree.readContent(fileName)).toBe(content);
});

async function runMigration(): Promise<void> {
await runner.runSchematicAsync('migration-v12-deep-shadow-piercing-selector', {}, tree)
.toPromise();
}
});