Changeset: Improve copyOp() API

Use `Object.assign()` to implement `copyOp()`, which simplifies the
code and provides a return value. Also make the second op optional.
This commit is contained in:
Richard Hansen 2021-10-02 18:17:11 -04:00
parent 02ef78e174
commit 94f5507671

View file

@ -205,14 +205,10 @@ exports.newOp = (optOpcode) => ({
* Copies op1 to op2 * Copies op1 to op2
* *
* @param {Op} op1 - src Op * @param {Op} op1 - src Op
* @param {Op} op2 - dest Op * @param {Op} [op2] - dest Op. If not given, a new Op is used.
* @returns {Op} `op2`
*/ */
const copyOp = (op1, op2) => { const copyOp = (op1, op2 = exports.newOp()) => Object.assign(op2, op1);
op2.opcode = op1.opcode;
op2.chars = op1.chars;
op2.lines = op1.lines;
op2.attribs = op1.attribs;
};
/** /**
* Serializes a sequence of Ops. * Serializes a sequence of Ops.