-
Notifications
You must be signed in to change notification settings - Fork 6.8k
fix(cdk/schematics): drop tilde imports when updating to v13 #23732
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/cdk/schematics/ng-update/migrations/tilde-import-v13/tilde-import-migration.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @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} from '@angular-devkit/core'; | ||
import {ResolvedResource} from '../../../update-tool/component-resource-collector'; | ||
import {TargetVersion} from '../../../update-tool/target-version'; | ||
import {DevkitMigration} from '../../devkit-migration'; | ||
|
||
/** Migration that removes tilde symbols from imports. */ | ||
export class TildeImportMigration extends DevkitMigration<null> { | ||
enabled = this.targetVersion === TargetVersion.V13; | ||
|
||
override visitStylesheet(stylesheet: ResolvedResource): void { | ||
const extension = extname(stylesheet.filePath); | ||
|
||
if (extension === '.scss' || extension === '.css') { | ||
const content = stylesheet.content; | ||
const migratedContent = content.replace(/@(?:import|use) +['"]~@angular\/.*['"].*;?/g, | ||
(match) => { | ||
const index = match.indexOf('~@angular'); | ||
return match.slice(0, index) + match.slice(index + 1); | ||
}); | ||
|
||
if (migratedContent && migratedContent !== content) { | ||
this.fileSystem.edit(stylesheet.filePath) | ||
.remove(0, stylesheet.content.length) | ||
.insertLeft(0, migratedContent); | ||
} | ||
} | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
src/cdk/schematics/ng-update/test-cases/v13/misc/tilde-import-v13.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import {UnitTestTree} from '@angular-devkit/schematics/testing'; | ||
import {createTestCaseSetup} from '../../../../testing'; | ||
import {join} from 'path'; | ||
import {MIGRATION_PATH} from '../../../../paths'; | ||
|
||
describe('v13 tilde import migration', () => { | ||
const PROJECT_PATH = '/projects/cdk-testing'; | ||
const TEST_PATH = join(PROJECT_PATH, 'src/test.scss'); | ||
let tree: UnitTestTree; | ||
let _writeFile: (filePath: string, text: string) => void; | ||
let runMigration: () => Promise<{logOutput: string}>; | ||
|
||
beforeEach(async () => { | ||
const testSetup = await createTestCaseSetup('migration-v13', MIGRATION_PATH, []); | ||
tree = testSetup.appTree; | ||
runMigration = testSetup.runFixers; | ||
_writeFile = testSetup.writeFile; | ||
}); | ||
|
||
/** Writes an array of lines as a single file. */ | ||
function writeLines(path: string, lines: string[]): void { | ||
_writeFile(path, lines.join('\n')); | ||
} | ||
|
||
/** Reads a file and split it into an array where each item is a new line. */ | ||
function splitFile(path: string): string[] { | ||
return tree.readContent(path).split('\n'); | ||
} | ||
|
||
it('should remove the tilde from angular imports', async () => { | ||
writeLines(TEST_PATH, [ | ||
`@use '~@angular/material' as mat;`, | ||
`@import '~@angular/material/theming';`, | ||
`@import '~@angular/cdk/overlay-prebuilt.css';`, | ||
|
||
`@include mat.button-theme();`, | ||
`@include mat-core();`, | ||
]); | ||
|
||
await runMigration(); | ||
|
||
expect(splitFile(TEST_PATH)).toEqual([ | ||
`@use '@angular/material' as mat;`, | ||
`@import '@angular/material/theming';`, | ||
`@import '@angular/cdk/overlay-prebuilt.css';`, | ||
|
||
`@include mat.button-theme();`, | ||
`@include mat-core();`, | ||
]); | ||
}); | ||
|
||
it('should handle an arbitrary amount of whitespace', async () => { | ||
writeLines(TEST_PATH, [ | ||
`@use '~@angular/material' as mat;`, | ||
|
||
`@include mat.core();`, | ||
]); | ||
|
||
await runMigration(); | ||
|
||
expect(splitFile(TEST_PATH)).toEqual([ | ||
`@use '@angular/material' as mat;`, | ||
|
||
`@include mat.core();`, | ||
]); | ||
}); | ||
|
||
it('should preserve tilde after the start', async () => { | ||
writeLines(TEST_PATH, [ | ||
`@use '~@angular/~material' as mat;`, | ||
`@import '@angular/cdk/~overlay-prebuilt.css';`, | ||
|
||
`@include mat.core();`, | ||
]); | ||
|
||
await runMigration(); | ||
|
||
expect(splitFile(TEST_PATH)).toEqual([ | ||
`@use '@angular/~material' as mat;`, | ||
`@import '@angular/cdk/~overlay-prebuilt.css';`, | ||
|
||
`@include mat.core();`, | ||
]); | ||
}); | ||
|
||
it('should handle different types of quotes', async () => { | ||
writeLines(TEST_PATH, [ | ||
`@use "~@angular/material" as mat;`, | ||
`@import '~@angular/cdk/overlay-prebuilt.css';`, | ||
|
||
`@include mat.button-theme();`, | ||
`@include mat-core();`, | ||
]); | ||
|
||
await runMigration(); | ||
|
||
expect(splitFile(TEST_PATH)).toEqual([ | ||
`@use "@angular/material" as mat;`, | ||
`@import '@angular/cdk/overlay-prebuilt.css';`, | ||
|
||
`@include mat.button-theme();`, | ||
`@include mat-core();`, | ||
]); | ||
}); | ||
|
||
it('should preserve the tilde in non-angular imports', async () => { | ||
writeLines(TEST_PATH, [ | ||
`@use '~@angular-momentum/material' as mat;`, | ||
`@import '~@angular-momentum/material/theming';`, | ||
`@import '@angular/cdk/overlay-prebuilt.css';`, | ||
|
||
`@include mat.button-theme();`, | ||
`@include mat-core();`, | ||
]); | ||
|
||
await runMigration(); | ||
|
||
expect(splitFile(TEST_PATH)).toEqual([ | ||
`@use '~@angular-momentum/material' as mat;`, | ||
`@import '~@angular-momentum/material/theming';`, | ||
`@import '@angular/cdk/overlay-prebuilt.css';`, | ||
|
||
`@include mat.button-theme();`, | ||
`@include mat-core();`, | ||
]); | ||
}); | ||
|
||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.