UNPKG

@angular/cdk

Version:

Angular Material Component Development Kit

83 lines 3.7 kB
"use strict"; /** * @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.dev/license */ Object.defineProperty(exports, "__esModule", { value: true }); exports.CssTokensMigration = void 0; const ts = require("typescript"); const migration_1 = require("../../update-tool/migration"); const literal_1 = require("../typescript/literal"); const upgrade_data_1 = require("../upgrade-data"); /** Characters that can be part of a valid token name. */ const TOKEN_CHARACTER = /[-_a-z0-9]/i; /** * Migration that walks through every string literal, template and stylesheet in * order to migrate outdated CSS tokens to their new name. */ class CssTokensMigration extends migration_1.Migration { constructor() { super(...arguments); /** Change data that upgrades to the specified target version. */ this.data = (0, upgrade_data_1.getVersionUpgradeData)(this, 'cssTokens'); // Only enable the migration rule if there is upgrade data. this.enabled = this.data.length !== 0; } visitNode(node) { if (ts.isStringLiteralLike(node)) { this._visitStringLiteralLike(node); } } visitTemplate(template) { this.data.forEach(data => { if (data.replaceIn && !data.replaceIn.html) { return; } (0, literal_1.findAllSubstringIndices)(template.content, data.replace) .map(offset => template.start + offset) // Filter out matches that are followed by a valid token character, so that we don't match // partial token names. .filter(start => !TOKEN_CHARACTER.test(template.content[start + data.replace.length] || '')) .forEach(start => this._replaceSelector(template.filePath, start, data)); }); } visitStylesheet(stylesheet) { this.data.forEach(data => { if (data.replaceIn && !data.replaceIn.stylesheet) { return; } (0, literal_1.findAllSubstringIndices)(stylesheet.content, data.replace) .map(offset => stylesheet.start + offset) // Filter out matches that are followed by a valid token character, so that we don't match // partial token names. .filter(start => !TOKEN_CHARACTER.test(stylesheet.content[start + data.replace.length] || '')) .forEach(start => this._replaceSelector(stylesheet.filePath, start, data)); }); } _visitStringLiteralLike(node) { const textContent = node.getText(); const filePath = this.fileSystem.resolve(node.getSourceFile().fileName); this.data.forEach(data => { if (data.replaceIn && !data.replaceIn.tsStringLiterals) { return; } (0, literal_1.findAllSubstringIndices)(textContent, data.replace) .map(offset => node.getStart() + offset) // Filter out matches that are followed by a valid token character, so that we don't match // partial token names. .filter(start => !TOKEN_CHARACTER.test(textContent[start + data.replace.length] || '')) .forEach(start => this._replaceSelector(filePath, start, data)); }); } _replaceSelector(filePath, start, data) { this.fileSystem .edit(filePath) .remove(start, data.replace.length) .insertRight(start, data.replaceWith); } } exports.CssTokensMigration = CssTokensMigration; //# sourceMappingURL=css-tokens.js.map