@angular/cdk
Version:
Angular Material Component Development Kit
95 lines • 4.42 kB
JavaScript
;
/**
* @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.MethodCallArgumentsMigration = void 0;
const ts = __importStar(require("typescript"));
const migration_1 = require("../../update-tool/migration");
const upgrade_data_1 = require("../upgrade-data");
/**
* Migration that visits every TypeScript method call expression and checks if the
* argument count is invalid and needs to be *manually* updated.
*/
class MethodCallArgumentsMigration extends migration_1.Migration {
constructor() {
super(...arguments);
/** Change data that upgrades to the specified target version. */
this.data = (0, upgrade_data_1.getVersionUpgradeData)(this, 'methodCallChecks');
// Only enable the migration rule if there is upgrade data.
this.enabled = this.data.length !== 0;
}
visitNode(node) {
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)) {
this._checkPropertyAccessMethodCall(node);
}
}
_checkPropertyAccessMethodCall(node) {
const propertyAccess = node.expression;
if (!ts.isIdentifier(propertyAccess.name)) {
return;
}
const hostType = this.typeChecker.getTypeAtLocation(propertyAccess.expression);
const hostTypeName = hostType.symbol && hostType.symbol.name;
const methodName = propertyAccess.name.text;
if (!hostTypeName) {
return;
}
// TODO(devversion): Revisit the implementation of this upgrade rule. It seems difficult
// and ambiguous to maintain the data for this rule. e.g. consider a method which has the
// same amount of arguments but just had a type change. In that case we could still add
// new entries to the upgrade data that match the current argument length to just show
// a failure message, but adding that data becomes painful if the method has optional
// parameters and it would mean that the error message would always show up, even if the
// argument is in some cases still assignable to the new parameter type. We could re-use
// the logic we have in the constructor-signature checks to check for assignability and
// to make the upgrade data less verbose.
const failure = this.data
.filter(data => data.method === methodName && data.className === hostTypeName)
.map(data => data.invalidArgCounts.find(f => f.count === node.arguments.length))[0];
if (!failure) {
return;
}
this.createFailureAtNode(node, `Found call to "${hostTypeName + '.' + methodName}" ` +
`with ${failure.count} arguments. Message: ${failure.message}`);
}
}
exports.MethodCallArgumentsMigration = MethodCallArgumentsMigration;
//# sourceMappingURL=method-call-arguments.js.map