Skip to content

Commit

Permalink
fix(amplify-velocity-template): support 'get' and 'set' of array vars
Browse files Browse the repository at this point in the history
The implementation of set and get methods of an array in velocity is not working like in the real
environment.

fix is based on shepherdwind/velocity.js#133

fix aws-amplify#5741
  • Loading branch information
yuth committed Jan 16, 2021
1 parent 3da53b7 commit 5776804
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
57 changes: 39 additions & 18 deletions packages/amplify-velocity-template/src/compile/references.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module.exports = function(Velocity, utils) {
function(key) {
this.config.unescape[key] = true;
},
this
this,
);
},

Expand Down Expand Up @@ -132,7 +132,7 @@ module.exports = function(Velocity, utils) {
// 第三个参数,返回后面的参数ast
ret = this.getAttributes(property, ret, ast);
},
this
this,
);
}

Expand Down Expand Up @@ -164,7 +164,7 @@ module.exports = function(Velocity, utils) {

return false;
},
this
this,
);

return {
Expand Down Expand Up @@ -226,20 +226,23 @@ module.exports = function(Velocity, utils) {
var id = property.id;
var ret = '';

// getter 处理
if (id.indexOf('get') === 0 && !(id in baseRef)) {
if (id.length === 3) {
// get('address')
ret = getter(baseRef, this.getLiteral(property.args[0]));
} else {
// getAddress()
ret = getter(baseRef, id.slice(3));
}
// get(xxx)
if (id === 'get' && !(id in baseRef)) {
return getter(baseRef, this.getLiteral(property.args[0]));
}

return ret;
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 处理
} else if (id.indexOf('set') === 0 && !baseRef[id]) {
// setter 处理
if (id.indexOf('set') === 0 && !baseRef[id]) {
baseRef[id.slice(3)] = this.getLiteral(property.args[0]);
// $page.setName(123)
baseRef.toString = function() {
Expand All @@ -263,6 +266,24 @@ module.exports = function(Velocity, utils) {
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)) {
if (typeof index === 'number') {
var index = this.getLiteral(property.args[0]);
} else {
var index = baseRef.indexOf(this.getLiteral(property.args[0]));
}

ret = baseRef[index];
baseRef.splice(index, 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 {
Expand All @@ -274,7 +295,7 @@ module.exports = function(Velocity, utils) {
function(exp) {
args.push(this.getLiteral(exp));
},
this
this,
);

if (ret && ret.call) {
Expand All @@ -291,10 +312,10 @@ module.exports = function(Velocity, utils) {
} catch (e) {
var pos = ast.pos;
var text = Velocity.Helper.getRefText(ast);
var err = ' on ' + text + ' at Line number ' + pos.first_line + ':' + pos.first_column;
var err = ' on ' + text + ' at L/N ' + pos.first_line + ':' + pos.first_column;
e.name = '';
e.message += err;
throw e;
throw new Error(e);
}
} else {
this._throw(ast, property, 'TypeError');
Expand Down
20 changes: 10 additions & 10 deletions packages/amplify-velocity-template/tests/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Compile', function() {
address: 'bar',
Address: 'foo',
},
})
}),
);

assert.equal('bar bar', render(vm1, { customer: { address: 'bar' } }));
Expand All @@ -40,7 +40,7 @@ describe('Compile', function() {
Address: 'bar',
address: 'foo',
},
})
}),
);
});

Expand All @@ -52,7 +52,7 @@ describe('Compile', function() {
foo: function() {
return { bar: 'hello' };
},
})
}),
);

assert.equal(
Expand All @@ -61,7 +61,7 @@ describe('Compile', function() {
foo: function() {
return 'foo';
},
})
}),
);
});

Expand Down Expand Up @@ -214,7 +214,7 @@ describe('Compile', function() {
values.push(value);
return 'foo';
},
}
},
);
assert.deepEqual(values, ['bar']);
assert.equal(ret.trim(), 'foo');
Expand Down Expand Up @@ -471,7 +471,7 @@ describe('Compile', function() {

assert.throws(function() {
compile.render(context);
}, /Line number 3:0/);
}, /L\/N 3:0/);
});

it('print error stack of user-defined macro', function() {
Expand Down Expand Up @@ -601,7 +601,7 @@ describe('Compile', function() {
'\nThis content is ignored. $val\n',
render(vm, {
val: 'foo',
})
}),
);
});

Expand All @@ -611,7 +611,7 @@ describe('Compile', function() {
'This content is ignored. $val\na',
render(vm, {
val: 'foo',
})
}),
);
});
});
Expand Down Expand Up @@ -639,7 +639,7 @@ describe('Compile', function() {
},
},
}).trim(),
expected
expected,
);
});
});
Expand Down Expand Up @@ -770,7 +770,7 @@ describe('Compile', function() {
key3: { key4: 'value4' },
},
}),
expected
expected,
);
});

Expand Down

0 comments on commit 5776804

Please sign in to comment.