in packages/amplify-velocity-template/src/compile/references.js [225:329]
getPropMethod: function (property, baseRef, ast) {
var id = property.id;
var ret = '';
// get(xxx)
if (id === 'get' && !(id in baseRef)) {
return getter(baseRef, this.getLiteral(property.args[0]));
}
if (id === 'set' && !(id in baseRef)) {
baseRef[this.getLiteral(property.args[0])] = this.getLiteral(property.args[1]);
return '';
}
// getter for example: getAddress()
if (id.indexOf('get') === 0 && !(id in baseRef)) {
return getter(baseRef, id.slice(3));
}
// setter 处理
if (id.indexOf('set') === 0 && !baseRef[id]) {
baseRef[id.slice(3)] = this.getLiteral(property.args[0]);
// $page.setName(123)
baseRef.toString = function () {
return '';
};
return baseRef;
} else if (id.indexOf('is') === 0 && !(id in baseRef)) {
return getter(baseRef, id.slice(2));
} else if (id === 'keySet' && !baseRef[id]) {
return utils.keys(baseRef);
} else if (id === 'entrySet' && !baseRef[id]) {
ret = [];
utils.forEach(baseRef, function (value, key) {
ret.push({ key: key, value: value });
});
return ret;
} else if (id === 'size' && !baseRef[id]) {
return getSize(baseRef);
} else if (id === 'put' && !baseRef[id]) {
return (baseRef[this.getLiteral(property.args[0])] = this.getLiteral(property.args[1]));
} else if (id === 'add' && !baseRef[id] && typeof baseRef.push === 'function') {
return baseRef.push(this.getLiteral(property.args[0]));
} else if (id === 'remove') {
if (utils.isArray(baseRef)) {
const itemToRemove = this.getLiteral(property.args[0]);
let indexToRemove;
if (typeof itemToRemove === 'number') {
indexToRemove = itemToRemove;
} else {
indexToRemove = baseRef.indexOf(itemToRemove);
}
ret = baseRef[indexToRemove];
baseRef.splice(indexToRemove, 1);
return ret;
} else if (utils.isObject(baseRef)) {
ret = baseRef[this.getLiteral(property.args[0])];
delete baseRef[this.getLiteral(property.args[0])];
return ret;
}
return undefined;
} else if (id === 'subList' && !baseRef[id]) {
return baseRef.slice(this.getLiteral(property.args[0]), this.getLiteral(property.args[1]));
} else {
ret = baseRef[id];
var args = [];
utils.forEach(
property.args,
function (exp) {
args.push(this.getLiteral(exp));
},
this,
);
if (ret && ret.call) {
var that = this;
if (typeof baseRef === 'object' && baseRef) {
baseRef.eval = function () {
return that.eval.apply(that, arguments);
};
}
try {
ret = ret.apply(baseRef, args);
} catch (e) {
var pos = ast.pos;
var text = Velocity.Helper.getRefText(ast);
var err = ' on ' + text + ' at L/N ' + pos.first_line + ':' + pos.first_column;
e.name = '';
e.message += err;
throw new Error(e);
}
} else {
this._throw(ast, property, 'TypeError');
ret = undefined;
}
}
return ret;
},