-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6303efd
commit da739b5
Showing
12 changed files
with
257 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
export interface HtmlComment { | ||
readonly tag: string; | ||
readonly withAttrs: string[]; | ||
readonly comment: string; | ||
} |
This file contains 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
This file contains 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,6 @@ | ||
export * from './remove-inputs'; | ||
export * from './replace-attr-values'; | ||
export * from './replace-attrs'; | ||
export * from './replace-tag'; | ||
export * from './replace-tags'; | ||
export * from './template-comments'; |
44 changes: 44 additions & 0 deletions
44
projects/cdk/schematics/ng-update/utils/templates/template-comments.ts
This file contains 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,44 @@ | ||
import {UpdateRecorder} from '@angular-devkit/schematics'; | ||
import {DevkitFileSystem} from 'ng-morph'; | ||
|
||
import {TODO_MARK} from '../../../utils/insert-todo'; | ||
import {findElementsWithAttributeOnTag} from '../../../utils/templates/elements'; | ||
import { | ||
getTemplateFromTemplateResource, | ||
getTemplateOffset, | ||
} from '../../../utils/templates/template-resource'; | ||
import {TemplateResource} from '../../interfaces'; | ||
import {HtmlComment} from '../../interfaces'; | ||
|
||
export function addHTMLCommentTags({ | ||
resource, | ||
recorder, | ||
fileSystem, | ||
data, | ||
}: { | ||
fileSystem: DevkitFileSystem; | ||
recorder: UpdateRecorder; | ||
data: readonly HtmlComment[]; | ||
resource: TemplateResource; | ||
}): void { | ||
const template = getTemplateFromTemplateResource(resource, fileSystem); | ||
const templateOffset = getTemplateOffset(resource); | ||
|
||
data.forEach(({comment, tag, withAttrs}) => { | ||
const elementStartOffsets = [ | ||
...findElementsWithAttributeOnTag(template, withAttrs, [tag]), | ||
...findElementsWithAttributeOnTag( | ||
template, | ||
withAttrs.map(attr => `[${attr}]`), | ||
[tag], | ||
), | ||
].map( | ||
({sourceCodeLocation}) => | ||
(sourceCodeLocation?.startOffset || 0) + templateOffset, | ||
); | ||
|
||
elementStartOffsets.forEach(offset => { | ||
recorder.insertRight(offset, `<!-- ${TODO_MARK} ${comment} -->\n`); | ||
}); | ||
}); | ||
} |
9 changes: 9 additions & 0 deletions
9
projects/cdk/schematics/ng-update/v4/steps/constants/html-comments.ts
This file contains 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,9 @@ | ||
import {HtmlComment} from '../../../interfaces'; | ||
|
||
export const HTML_COMMENTS: HtmlComment[] = [ | ||
{ | ||
tag: `tui-toggle`, | ||
withAttrs: [`singleColor`, `showLoader`], | ||
comment: `toggle [singleColor] and [showLoader] inputs have been removed due to design changes`, | ||
}, | ||
]; |
This file contains 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
5 changes: 5 additions & 0 deletions
5
projects/cdk/schematics/ng-update/v4/steps/constants/index.ts
This file contains 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,5 @@ | ||
export * from './attrs-to-replace'; | ||
export * from './html-comments'; | ||
export * from './identifiers-to-replace'; | ||
export * from './inputs-to-remove'; | ||
export * from './tags-to-replace'; |
This file contains 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
This file contains 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
1 change: 1 addition & 0 deletions
1
projects/cdk/schematics/ng-update/v4/steps/templates/toggles/index.ts
This file contains 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './migrate-checkbox'; | ||
export * from './migrate-radio'; | ||
export * from './migrate-toggle'; |
40 changes: 40 additions & 0 deletions
40
projects/cdk/schematics/ng-update/v4/steps/templates/toggles/migrate-toggle.ts
This file contains 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,40 @@ | ||
import {UpdateRecorder} from '@angular-devkit/schematics'; | ||
import {DevkitFileSystem} from 'ng-morph'; | ||
|
||
import {findElementsByTagName} from '../../../../../utils/templates/elements'; | ||
import { | ||
getTemplateFromTemplateResource, | ||
getTemplateOffset, | ||
} from '../../../../../utils/templates/template-resource'; | ||
import {TemplateResource} from '../../../../interfaces'; | ||
import {closeStartTag, removeClosingTag, replaceOpenTag, replaceSizeAttr} from './common'; | ||
|
||
export function migrateToggle({ | ||
resource, | ||
recorder, | ||
fileSystem, | ||
}: { | ||
fileSystem: DevkitFileSystem; | ||
recorder: UpdateRecorder; | ||
resource: TemplateResource; | ||
}): void { | ||
const template = getTemplateFromTemplateResource(resource, fileSystem); | ||
const templateOffset = getTemplateOffset(resource); | ||
|
||
const elements = findElementsByTagName(template, `tui-toggle`); | ||
|
||
elements.forEach(({attrs, sourceCodeLocation}) => { | ||
if (!sourceCodeLocation) { | ||
return; | ||
} | ||
|
||
replaceSizeAttr(attrs, sourceCodeLocation, recorder, templateOffset); | ||
replaceOpenTag(sourceCodeLocation, recorder, templateOffset, { | ||
tag: `tui-toggle`, | ||
directive: `tuiToggle`, | ||
type: `checkbox`, | ||
}); | ||
closeStartTag(sourceCodeLocation, recorder, templateOffset); | ||
removeClosingTag(sourceCodeLocation, recorder, templateOffset); | ||
}); | ||
} |
120 changes: 120 additions & 0 deletions
120
projects/cdk/schematics/ng-update/v4/tests/schematic-migrate-toggle.spec.ts
This file contains 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,120 @@ | ||
/* eslint-disable rxjs/no-topromise */ | ||
import {HostTree} from '@angular-devkit/schematics'; | ||
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing'; | ||
import {TuiSchema} from '@taiga-ui/cdk/schematics/ng-add/schema'; | ||
import { | ||
createProject, | ||
createSourceFile, | ||
resetActiveProject, | ||
saveActiveProject, | ||
setActiveProject, | ||
} from 'ng-morph'; | ||
import {join} from 'path'; | ||
|
||
import {createAngularJson} from '../../../utils/create-angular-json'; | ||
|
||
const collectionPath = join(__dirname, `../../../migration.json`); | ||
|
||
const COMPONENT_BEFORE = ` | ||
import { TuiToggleModule } from "@taiga-ui/experimental"; | ||
@Component({ | ||
standalone: true, | ||
templateUrl: './test.template.html', | ||
imports: [TuiToggleModule] | ||
}) | ||
export class TestComponent { | ||
}`; | ||
|
||
const COMPONENT_AFTER = `import { TuiToggleModule } from "@taiga-ui/kit"; | ||
@Component({ | ||
standalone: true, | ||
templateUrl: './test.template.html', | ||
imports: [TuiToggleModule] | ||
}) | ||
export class TestComponent { | ||
}`; | ||
|
||
const TEMPLATE_BEFORE = ` | ||
<tui-toggle | ||
formControlName="test" | ||
class="toggle" | ||
[showIcons]="true" | ||
></tui-toggle> | ||
<tui-toggle | ||
[showIcons]="true" | ||
[singleColor]="true" | ||
></tui-toggle> | ||
`; | ||
|
||
const TEMPLATE_AFTER = ` | ||
<input | ||
tuiToggle | ||
type="checkbox" | ||
formControlName="test" | ||
class="toggle" | ||
[showIcons]="true" | ||
/> | ||
<!-- TODO: (Taiga UI migration) toggle [singleColor] and [showLoader] inputs have been removed due to design changes --> | ||
<input | ||
tuiToggle | ||
type="checkbox" | ||
[showIcons]="true" | ||
${``} | ||
/> | ||
`; | ||
|
||
describe(`ng-update`, () => { | ||
let host: UnitTestTree; | ||
let runner: SchematicTestRunner; | ||
|
||
beforeEach(() => { | ||
host = new UnitTestTree(new HostTree()); | ||
runner = new SchematicTestRunner(`schematics`, collectionPath); | ||
|
||
setActiveProject(createProject(host)); | ||
|
||
createMainFiles(); | ||
|
||
saveActiveProject(); | ||
}); | ||
|
||
it(`should migrate toggle in template`, async () => { | ||
const tree = await runner | ||
.runSchematicAsync( | ||
`updateToV4`, | ||
{'skip-logs': process.env[`TUI_CI`] === `true`} as Partial<TuiSchema>, | ||
host, | ||
) | ||
.toPromise(); | ||
|
||
expect(tree.readContent(`test/app/test.template.html`)).toEqual(TEMPLATE_AFTER); | ||
}); | ||
|
||
it(`should migrate toggle references in ts files`, async () => { | ||
const tree = await runner | ||
.runSchematicAsync( | ||
`updateToV4`, | ||
{'skip-logs': process.env[`TUI_CI`] === `true`} as Partial<TuiSchema>, | ||
host, | ||
) | ||
.toPromise(); | ||
|
||
expect(tree.readContent(`test/app/test.component.ts`)).toEqual(COMPONENT_AFTER); | ||
}); | ||
|
||
afterEach(() => resetActiveProject()); | ||
}); | ||
|
||
function createMainFiles(): void { | ||
createSourceFile(`test/app/test.component.ts`, COMPONENT_BEFORE); | ||
|
||
createSourceFile(`test/app/test.template.html`, TEMPLATE_BEFORE); | ||
|
||
createAngularJson(); | ||
createSourceFile( | ||
`package.json`, | ||
`{"dependencies": {"@angular/core": "~13.0.0", "@taiga-ui/addon-commerce": "~3.42.0"}}`, | ||
); | ||
} |