fix: improved inlining
This commit is contained in:
parent
e05bae167a
commit
d24e8d9368
|
@ -1,10 +1,10 @@
|
|||
import {parse} from '@babel/parser';
|
||||
|
||||
export default (code) => {
|
||||
export default (code, options) => {
|
||||
try {
|
||||
return parse(code);
|
||||
return parse(code, options);
|
||||
}
|
||||
catch (error) {
|
||||
throw new Error(`Error parsing code: ${error.message}\nParsing:\n${code}`);
|
||||
throw new Error(`Error parsing code: ${error.message}\nParsing:\n${code}\nwith options:${JSON.stringify(options)}`);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -8,6 +8,10 @@ import {
|
|||
types,
|
||||
walkAncestor,
|
||||
} from '@avocado/code';
|
||||
import {D} from '@flecks/core';
|
||||
|
||||
const debug = D('@avocado/entity/accessors');
|
||||
const debugSilly = debug.extend('silly');
|
||||
|
||||
const blacklistedAccessorKeys = [
|
||||
's13nId',
|
||||
|
@ -56,28 +60,43 @@ function traitAccessorForProperty(type, property, descriptor) {
|
|||
}
|
||||
let {get, set} = descriptor;
|
||||
if (get) {
|
||||
const getAst = extractFunction(parse(`(${get.toString()})`));
|
||||
debugSilly('trying to inline %s.%s.get', type, property);
|
||||
// @todo instead of this, we could stage it in an object literal, as below.
|
||||
let getString = get.toString();
|
||||
if (getString.startsWith('get ')) {
|
||||
getString = `function ${getString.slice(4)}`;
|
||||
}
|
||||
const getAst = extractFunction(parse(`(${getString})`, {allowSuperOutsideMethod: true}));
|
||||
if (hasExternalDependency(getAst)) {
|
||||
debugSilly('BAIL: external (%s)', type, property, generate(getAst.body).code);
|
||||
// eslint-disable-next-line no-new-func
|
||||
get = new Function('', `return this.traits['${type}']['${property}'];`);
|
||||
}
|
||||
else {
|
||||
debugSilly('optimized');
|
||||
rewriteTraitThisToEntityThis(type)(getAst);
|
||||
// eslint-disable-next-line no-new-func
|
||||
get = new Function('', generate(getAst.body).code);
|
||||
// eslint-disable-next-line no-eval
|
||||
({get} = eval(`({ get() ${generate(getAst.body).code} })`));
|
||||
}
|
||||
}
|
||||
if (set) {
|
||||
const setAst = extractFunction(parse(`(${set.toString()})`));
|
||||
debugSilly('trying to inline %s.%s.set', type, property);
|
||||
let setString = set.toString();
|
||||
if (setString.startsWith('set ')) {
|
||||
setString = `function ${setString.slice(4)}`;
|
||||
}
|
||||
const setAst = extractFunction(parse(`(${setString})`, {allowSuperOutsideMethod: true}));
|
||||
if (hasExternalDependency(setAst)) {
|
||||
debugSilly('BAIL: external (%s)', type, property, generate(setAst.body).code);
|
||||
// eslint-disable-next-line no-new-func
|
||||
set = new Function('value', `this.traits['${type}']['${property}'] = value;`);
|
||||
}
|
||||
else {
|
||||
debugSilly('optimized');
|
||||
rewriteTraitThisToEntityThis(type)(setAst);
|
||||
rewriteParams(setAst, [types.identifier('value')]);
|
||||
// eslint-disable-next-line no-new-func
|
||||
set = new Function('value', generate(setAst.body).code);
|
||||
// eslint-disable-next-line no-eval
|
||||
({set} = eval(`({ set(value) ${generate(setAst.body).code} })`));
|
||||
}
|
||||
}
|
||||
if (!(type in traitAccessorForPropertyMap)) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user