|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {visitAll} from '@angular/compiler'; |
| 10 | + |
| 11 | +import {ElementCollector, ElementToMigrate, MigrateError, Result} from './types'; |
| 12 | +import {calculateNesting, getMainBlock, getOriginals, hasLineBreaks, parseTemplate, reduceNestingOffset} from './util'; |
| 13 | + |
| 14 | +export const boundcase = '[ngSwitchCase]'; |
| 15 | +export const switchcase = '*ngSwitchCase'; |
| 16 | +export const nakedcase = 'ngSwitchCase'; |
| 17 | +export const switchdefault = '*ngSwitchDefault'; |
| 18 | +export const nakeddefault = 'ngSwitchDefault'; |
| 19 | + |
| 20 | +const cases = [ |
| 21 | + boundcase, |
| 22 | + switchcase, |
| 23 | + nakedcase, |
| 24 | + switchdefault, |
| 25 | + nakeddefault, |
| 26 | +]; |
| 27 | + |
| 28 | +/** |
| 29 | + * Replaces structural directive ngSwitch instances with new switch. |
| 30 | + * Returns null if the migration failed (e.g. there was a syntax error). |
| 31 | + */ |
| 32 | +export function migrateCase(template: string): {migrated: string, errors: MigrateError[]} { |
| 33 | + let errors: MigrateError[] = []; |
| 34 | + let parsed = parseTemplate(template); |
| 35 | + if (parsed === null) { |
| 36 | + return {migrated: template, errors}; |
| 37 | + } |
| 38 | + |
| 39 | + let result = template; |
| 40 | + const visitor = new ElementCollector(cases); |
| 41 | + visitAll(visitor, parsed.rootNodes); |
| 42 | + calculateNesting(visitor, hasLineBreaks(template)); |
| 43 | + |
| 44 | + // this tracks the character shift from different lengths of blocks from |
| 45 | + // the prior directives so as to adjust for nested block replacement during |
| 46 | + // migration. Each block calculates length differences and passes that offset |
| 47 | + // to the next migrating block to adjust character offsets properly. |
| 48 | + let offset = 0; |
| 49 | + let nestLevel = -1; |
| 50 | + let postOffsets: number[] = []; |
| 51 | + for (const el of visitor.elements) { |
| 52 | + let migrateResult: Result = {tmpl: result, offsets: {pre: 0, post: 0}}; |
| 53 | + // applies the post offsets after closing |
| 54 | + offset = reduceNestingOffset(el, nestLevel, offset, postOffsets); |
| 55 | + |
| 56 | + if (el.attr.name === switchcase || el.attr.name === nakedcase || el.attr.name === boundcase) { |
| 57 | + try { |
| 58 | + migrateResult = migrateNgSwitchCase(el, result, offset); |
| 59 | + } catch (error: unknown) { |
| 60 | + errors.push({type: switchcase, error}); |
| 61 | + } |
| 62 | + } else if (el.attr.name === switchdefault || el.attr.name === nakeddefault) { |
| 63 | + try { |
| 64 | + migrateResult = migrateNgSwitchDefault(el, result, offset); |
| 65 | + } catch (error: unknown) { |
| 66 | + errors.push({type: switchdefault, error}); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + result = migrateResult.tmpl; |
| 71 | + offset += migrateResult.offsets.pre; |
| 72 | + postOffsets.push(migrateResult.offsets.post); |
| 73 | + nestLevel = el.nestCount; |
| 74 | + } |
| 75 | + |
| 76 | + return {migrated: result, errors}; |
| 77 | +} |
| 78 | + |
| 79 | +function migrateNgSwitchCase(etm: ElementToMigrate, tmpl: string, offset: number): Result { |
| 80 | + // includes the mandatory semicolon before as |
| 81 | + const lbString = etm.hasLineBreaks ? '\n' : ''; |
| 82 | + const leadingSpace = etm.hasLineBreaks ? '' : ' '; |
| 83 | + const condition = etm.attr.value; |
| 84 | + |
| 85 | + const originals = getOriginals(etm, tmpl, offset); |
| 86 | + |
| 87 | + const {start, middle, end} = getMainBlock(etm, tmpl, offset); |
| 88 | + const startBlock = `${leadingSpace}@case (${condition}) {${leadingSpace}${lbString}${start}`; |
| 89 | + const endBlock = `${end}${lbString}${leadingSpace}}`; |
| 90 | + |
| 91 | + const defaultBlock = startBlock + middle + endBlock; |
| 92 | + const updatedTmpl = tmpl.slice(0, etm.start(offset)) + defaultBlock + tmpl.slice(etm.end(offset)); |
| 93 | + |
| 94 | + // this should be the difference between the starting element up to the start of the closing |
| 95 | + // element and the mainblock sans } |
| 96 | + const pre = originals.start.length - startBlock.length; |
| 97 | + const post = originals.end.length - endBlock.length; |
| 98 | + |
| 99 | + return {tmpl: updatedTmpl, offsets: {pre, post}}; |
| 100 | +} |
| 101 | + |
| 102 | +function migrateNgSwitchDefault(etm: ElementToMigrate, tmpl: string, offset: number): Result { |
| 103 | + // includes the mandatory semicolon before as |
| 104 | + const lbString = etm.hasLineBreaks ? '\n' : ''; |
| 105 | + const leadingSpace = etm.hasLineBreaks ? '' : ' '; |
| 106 | + |
| 107 | + const originals = getOriginals(etm, tmpl, offset); |
| 108 | + |
| 109 | + const {start, middle, end} = getMainBlock(etm, tmpl, offset); |
| 110 | + const startBlock = `${leadingSpace}@default {${leadingSpace}${lbString}${start}`; |
| 111 | + const endBlock = `${end}${lbString}${leadingSpace}}`; |
| 112 | + |
| 113 | + const defaultBlock = startBlock + middle + endBlock; |
| 114 | + const updatedTmpl = tmpl.slice(0, etm.start(offset)) + defaultBlock + tmpl.slice(etm.end(offset)); |
| 115 | + |
| 116 | + // this should be the difference between the starting element up to the start of the closing |
| 117 | + // element and the mainblock sans } |
| 118 | + const pre = originals.start.length - startBlock.length; |
| 119 | + const post = originals.end.length - endBlock.length; |
| 120 | + |
| 121 | + return {tmpl: updatedTmpl, offsets: {pre, post}}; |
| 122 | +} |
0 commit comments