|
| 1 | +import * as ts from 'typescript'; |
| 2 | +import {ResolvedResource} from '../../update-tool/component-resource-collector'; |
| 3 | +import {WorkspacePath} from '../../update-tool/file-system'; |
| 4 | +import {Migration} from '../../update-tool/migration'; |
| 5 | +import {CssTokenUpgradeData} from '../data/css-tokens'; |
| 6 | +import {findAllSubstringIndices} from '../typescript/literal'; |
| 7 | +import {getVersionUpgradeData, UpgradeData} from '../upgrade-data'; |
| 8 | + |
| 9 | +/** Characters that can be part of a valid token name. */ |
| 10 | +const TOKEN_CHARACTER = /[-_a-z0-9]/i; |
| 11 | + |
| 12 | +/** |
| 13 | + * Migration that walks through every string literal, template and stylesheet in |
| 14 | + * order to migrate outdated CSS tokens to their new name. |
| 15 | + */ |
| 16 | +export class CssTokensMigration extends Migration<UpgradeData> { |
| 17 | + /** Change data that upgrades to the specified target version. */ |
| 18 | + data: CssTokenUpgradeData[] = getVersionUpgradeData(this, 'cssTokens'); |
| 19 | + |
| 20 | + // Only enable the migration rule if there is upgrade data. |
| 21 | + enabled = this.data.length !== 0; |
| 22 | + |
| 23 | + override visitNode(node: ts.Node): void { |
| 24 | + if (ts.isStringLiteralLike(node)) { |
| 25 | + this._visitStringLiteralLike(node); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + override visitTemplate(template: ResolvedResource): void { |
| 30 | + this.data.forEach(data => { |
| 31 | + if (data.replaceIn && !data.replaceIn.html) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + findAllSubstringIndices(template.content, data.replace) |
| 36 | + .map(offset => template.start + offset) |
| 37 | + // Filter out matches that are followed by a valid token character, so that we don't match |
| 38 | + // partial token names. |
| 39 | + .filter(start => !TOKEN_CHARACTER.test(template.content[start + data.replace.length] || '')) |
| 40 | + .forEach(start => this._replaceSelector(template.filePath, start, data)); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + override visitStylesheet(stylesheet: ResolvedResource): void { |
| 45 | + this.data.forEach(data => { |
| 46 | + if (data.replaceIn && !data.replaceIn.stylesheet) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + findAllSubstringIndices(stylesheet.content, data.replace) |
| 51 | + .map(offset => stylesheet.start + offset) |
| 52 | + // Filter out matches that are followed by a valid token character, so that we don't match |
| 53 | + // partial token names. |
| 54 | + .filter( |
| 55 | + start => !TOKEN_CHARACTER.test(stylesheet.content[start + data.replace.length] || ''), |
| 56 | + ) |
| 57 | + .forEach(start => this._replaceSelector(stylesheet.filePath, start, data)); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + private _visitStringLiteralLike(node: ts.StringLiteralLike) { |
| 62 | + if (node.parent && node.parent.kind !== ts.SyntaxKind.CallExpression) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const textContent = node.getText(); |
| 67 | + const filePath = this.fileSystem.resolve(node.getSourceFile().fileName); |
| 68 | + |
| 69 | + this.data.forEach(data => { |
| 70 | + if (data.replaceIn && !data.replaceIn.tsStringLiterals) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + findAllSubstringIndices(textContent, data.replace) |
| 75 | + .map(offset => node.getStart() + offset) |
| 76 | + // Filter out matches that are followed by a valid token character, so that we don't match |
| 77 | + // partial token names. |
| 78 | + .filter(start => !TOKEN_CHARACTER.test(textContent[start + data.replace.length] || '')) |
| 79 | + .forEach(start => this._replaceSelector(filePath, start, data)); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + private _replaceSelector(filePath: WorkspacePath, start: number, data: CssTokenUpgradeData) { |
| 84 | + this.fileSystem |
| 85 | + .edit(filePath) |
| 86 | + .remove(start, data.replace.length) |
| 87 | + .insertRight(start, data.replaceWith); |
| 88 | + } |
| 89 | +} |
0 commit comments