UNPKG

@angular/cdk

Version:

Angular Material Component Development Kit

104 lines 4.36 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 */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.AttributeSelectorsMigration = void 0; const ts = __importStar(require("typescript")); const migration_1 = require("../../update-tool/migration"); const literal_1 = require("../typescript/literal"); const upgrade_data_1 = require("../upgrade-data"); /** * Migration that walks through every string literal, template and stylesheet * in order to switch deprecated attribute selectors to the updated selector. */ class AttributeSelectorsMigration extends migration_1.Migration { constructor() { super(...arguments); /** Required upgrade changes for specified target version. */ this.data = (0, upgrade_data_1.getVersionUpgradeData)(this, 'attributeSelectors'); // 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(selector => { (0, literal_1.findAllSubstringIndices)(template.content, selector.replace) .map(offset => template.start + offset) .forEach(start => this._replaceSelector(template.filePath, start, selector)); }); } visitStylesheet(stylesheet) { this.data.forEach(selector => { const currentSelector = `[${selector.replace}]`; const updatedSelector = `[${selector.replaceWith}]`; (0, literal_1.findAllSubstringIndices)(stylesheet.content, currentSelector) .map(offset => stylesheet.start + offset) .forEach(start => this._replaceSelector(stylesheet.filePath, start, { replace: currentSelector, replaceWith: updatedSelector, })); }); } _visitStringLiteralLike(literal) { if (literal.parent && literal.parent.kind !== ts.SyntaxKind.CallExpression) { return; } const literalText = literal.getText(); const filePath = this.fileSystem.resolve(literal.getSourceFile().fileName); this.data.forEach(selector => { (0, literal_1.findAllSubstringIndices)(literalText, selector.replace) .map(offset => literal.getStart() + offset) .forEach(start => this._replaceSelector(filePath, start, selector)); }); } _replaceSelector(filePath, start, data) { this.fileSystem .edit(filePath) .remove(start, data.replace.length) .insertRight(start, data.replaceWith); } } exports.AttributeSelectorsMigration = AttributeSelectorsMigration; //# sourceMappingURL=attribute-selectors.js.map