Skip to content

Instantly share code, notes, and snippets.

@j-f1
Last active April 17, 2017 10:48
Show Gist options
  • Save j-f1/c9577ca91cc32130739d879f28d629e2 to your computer and use it in GitHub Desktop.
Save j-f1/c9577ca91cc32130739d879f28d629e2 to your computer and use it in GitHub Desktop.
babel-plugin-inline-array-methods

Inline Array Methods

A Babel plugin to inline methods on array literals.

Warning: If you have getters on objects in arrays, this could be lossy.
Example:

const x = { get a () { someSideEffect(); return 2 } };
[x.a].length;
// transforms to
const x = { get a () { someSideEffect(); return 2 } };
1;

What?

In:

[1, 2, 3].concat([4, 5, 6]);
[a, b, c].concat([d, e], f, g, [h]);
[1, 2, 3]['concat']([4, 5, 6]);
[1, 2, 3][`conc${"a"}t`]([4, 5, 6]);
[1, 2, 3][concat]([4, 5, 6]);
[1, 2, 3].push([4, 5, 6]);

[1, 2, 3].join();
[a, 'b', 'c'].join();
['a', 'b', 'c'].join();
['a', 'b', 'c'].join(a);
['a', 'b', 'c'].join('@');

[1, 2, 3].length;
[1, 2, 3][1];
[1, 2, 3]['1'];

[].shift();
[1, 2, 3].shift();

[1, 2, 3].slice();
[1, 2, 3].slice(1);
[1, 2, 3].slice(0, 2);
[1, 2, 3].slice(0, -1);

[1, 2, 3].pop();
[a, b, c].pop();
[].pop();

[a, b, c].reverse();
[1, 2, 3].reverse();

[1, 2, 3].splice(1);

Out:

[1, 2, 3, 4, 5, 6];
[a, b, c, d, e, f, g, h];
[1, 2, 3, 4, 5, 6];
[1, 2, 3][`conc${"a"}t`]([4, 5, 6]);
[1, 2, 3][concat]([4, 5, 6]);
4;

"1,2,3";
[a, 'b', 'c'].join();
"a,b,c";
['a', 'b', 'c'].join(a);
"a@b@c";

3;
2;
2;

undefined;
2;

[1, 2, 3];
[2, 3];
[1, 2];
[1, 2, 3].slice(0, -1);

3;
c;
undefined;

[c, b, a];
[3, 2, 1];

[2, 3];

Why?

Why not?

Installation

$ npm install -D b̟̞͎̙̽̉͗͗a̭͇̻̥͗̀̾̕b̧̨͇̥̀̒̀͘e͚̞̬͑́̊͑ͅḹ̼̠̫̏́͗-̧̘̖̣̇̒͌͌p̧͍̬̫͒̔͂͝l̠̙̻̘̓̾̃̏ṳ̜̥̺̉́͒͝g̗͉̖̝̒̄̊̈́ì̺̩͎͓́̄͝n̻̮̖̟̋̿̌̕-̡̨̫̟̾̓̃̕ȕ͈̼̝̯̂̊͠ṉ̞͚̭͂͑̿̾d̦̥̞̘̿͌͌̌ě͎͓̼̲̄̾̎f̡̬̝͌̆̇͜͝ị̬̗̑̎̓̍͜n̡̲̻̞̎̉͠͝e̼̭̦̹͊̄̀͌d͎̩̪͊̀͘̕͜
The MIT License (MIT)
Copyright © 2017 Jed Fox
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
export default function (babel) {
const { types: t } = babel;
const other = Symbol('other');
const undef = t.identifier('undefined');
const calls = {
concat(a, ...args) {
let bad = false;
a.elements = a.elements.concat(...args.map(arg => {
if (arg.type === 'ArrayExpression') return arg.elements;
return arg;
}));
return bad ? undefined : a;
},
join(arr, sep = t.stringLiteral(',')) {
if (arr.elements.some(el => el.value === undefined)) return;
if (sep.type !== 'StringLiteral') return;
let bad = false;
const str = arr.elements.map(el => {
if (!el.type.match(/Literal$/)) {
bad = true;
return;
}
return el.value;
}).join(sep.value);
return bad ? undefined : t.stringLiteral(str);
},
push(arr, ...args) {
return t.numericLiteral(arr.elements.length + args.length);
},
shift(arr) {
if (arr.elements.length === 0) {
return undef;
}
return t.numericLiteral(arr.elements.length - 1);
},
slice(arr, start = t.numericLiteral(0), end) {
if (start.type !== 'NumericLiteral' || (end && end.type !== 'NumericLiteral')) {
return;
}
return t.arrayExpression(arr.elements.slice(start.value, end && end.value))
},
pop(arr) {
return arr.elements[arr.elements.length - 1] || undef;
},
reverse(arr) {
return t.arrayExpression(arr.elements.reverse());
},
splice(arr, start, end, ...args) {
if (start.type !== 'NumericLiteral' || (end && end.type !== 'NumericLiteral')) {
return;
}
if (end) {
args.unshift(end.value);
}
return t.arrayExpression(arr.elements.slice().splice(start.value, ...args));
},
[other]() {}
}
window._t = t;
const members = {
length(arr) {
return t.numericLiteral(arr.elements.length);
},
[other](i, arr) {
if (typeof i !== 'number' && !i.match(/^\d+$/)) return;
return arr.elements[i];
}
}
function getName(member) {
if (member.computed) {
switch (member.property.type) {
case 'StringLiteral':
case 'NumericLiteral':
return member.property.value;
case 'TemplateLiteral':
return '';
}
} else {
return member.property.name;
}
}
function swap(path, member, handlers, ...args) {
const key = getName(member);
if (!key) { return; }
let handler = handlers[key];
if (typeof handler !== 'function') {
handler = members[other].bind(members, key);
}
const replacement = handler(member.object, ...args);
if (replacement) {
path.replaceWith(replacement);
}
}
return {
visitor: {
CallExpression(path) {
const { node } = path;
const { callee: member } = node;
if (member.type === 'MemberExpression' && member.object.type === 'ArrayExpression') {
swap(path, member, calls, ...node.arguments);
}
},
MemberExpression(path) {
const { node: member } = path;
if (member.object.type === 'ArrayExpression') {
swap(path, member, members);
}
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment