babel-plugin-aexpr-source-transformation
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -32,11 +32,29 @@ "use strict"; | ||
var customTemplates = {}; | ||
customTemplates[SET_MEMBER] = template("\n (function(obj, prop, operator, val) {\n return obj[prop] = val;\n });\n"); | ||
var GENERATED_IMPORT_IDENTIFIER = Symbol("generated import identifier"); | ||
customTemplates[GET_MEMBER] = template("\n (function(obj, prop) {\n return obj[prop];\n });\n"); | ||
// let customTemplates = {}; | ||
// customTemplates[SET_MEMBER] = template(` | ||
// (function(obj, prop, operator, val) { | ||
// return obj[prop] = val; | ||
// }); | ||
// `); | ||
// | ||
// customTemplates[GET_MEMBER] = template(` | ||
// (function(obj, prop) { | ||
// return obj[prop]; | ||
// }); | ||
// `); | ||
// | ||
// customTemplates[GET_AND_CALL_MEMBER] = template(` | ||
// (function(obj, prop, args) { | ||
// return obj[prop](...args) | ||
// }); | ||
// `); | ||
// | ||
// customTemplates[AEXPR_IDENTIFIER_NAME] = template(` | ||
// (function(expr) { | ||
// return { onChange(cb) {}}; | ||
// }); | ||
// `); | ||
customTemplates[GET_AND_CALL_MEMBER] = template("\n (function(obj, prop, args) {\n return obj[prop](...args)\n });\n"); | ||
customTemplates[AEXPR_IDENTIFIER_NAME] = template("\n (function(expr) {\n return { onChange(cb) {}};\n });\n"); | ||
function addCustomTemplate(file, name) { | ||
@@ -46,27 +64,29 @@ var declar = file.declarations[name]; | ||
return file.declarations[name] = file.addImport("aexpr-source-transformation-propagation", name, name); | ||
var identifier = file.declarations[name] = file.addImport("aexpr-source-transformation-propagation", name, name); | ||
identifier[GENERATED_IMPORT_IDENTIFIER] = true; | ||
return identifier; | ||
var ref = customTemplates[name]; | ||
console.log(file.addImport("aexpr-source-transformation-propagation", "aexpr")); | ||
var uid = file.declarations[name] = file.scope.generateUidIdentifier(name); | ||
ref = ref().expression; | ||
ref[GENERATED_FUNCTION] = true; | ||
if (t.isFunctionExpression(ref) && !ref.id) { | ||
ref.body._compact = true; | ||
ref._generated = true; | ||
ref.id = uid; | ||
ref.type = "FunctionDeclaration"; | ||
file.path.unshiftContainer("body", ref); | ||
} else { | ||
ref._compact = true; | ||
file.scope.push({ | ||
id: uid, | ||
init: ref, | ||
unique: true | ||
}); | ||
} | ||
return uid; | ||
// let ref = customTemplates[name]; | ||
// console.log(file.addImport("aexpr-source-transformation-propagation", "aexpr")); | ||
// let uid = file.declarations[name] = file.scope.generateUidIdentifier(name); | ||
// | ||
// ref = ref().expression; | ||
// ref[GENERATED_FUNCTION] = true; | ||
// | ||
// if (t.isFunctionExpression(ref) && !ref.id) { | ||
// ref.body._compact = true; | ||
// ref._generated = true; | ||
// ref.id = uid; | ||
// ref.type = "FunctionDeclaration"; | ||
// file.path.unshiftContainer("body", ref); | ||
// } else { | ||
// ref._compact = true; | ||
// file.scope.push({ | ||
// id: uid, | ||
// init: ref, | ||
// unique: true | ||
// }); | ||
// } | ||
// | ||
// return uid; | ||
} | ||
@@ -96,9 +116,42 @@ | ||
console.log("read ignored");return; | ||
}; | ||
} | ||
function getIdentifierForExplicitScopeObject(parentWithScope) { | ||
var bindings = parentWithScope.scope.bindings; | ||
var scopeName = Object.keys(bindings).find(function (key) { | ||
return bindings[key].path && bindings[key].path.node && bindings[key].path.node.id && bindings[key].path.node.id[FLAG_GENERATED_SCOPE_OBJECT]; // should actually be IS_EXPLICIT_SCOPE_OBJECT | ||
}); | ||
var uniqueIdentifier = void 0; | ||
if (scopeName) { | ||
uniqueIdentifier = t.identifier(scopeName); | ||
} else { | ||
uniqueIdentifier = parentWithScope.scope.generateUidIdentifier('scope'); | ||
uniqueIdentifier[FLAG_GENERATED_SCOPE_OBJECT] = true; | ||
parentWithScope.scope.push({ | ||
kind: 'let', | ||
id: uniqueIdentifier, | ||
init: t.objectExpression([]) | ||
}); | ||
} | ||
uniqueIdentifier[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER] = true; | ||
return uniqueIdentifier; | ||
} | ||
path.traverse({ | ||
Identifier: function Identifier(path) { | ||
//console.log(path.node.name) | ||
// Check for a call to aexpr: | ||
function logIdentifier(msg, path) { | ||
console.log(msg, path.node.name, path.node.loc ? path.node.loc.start.line : ''); | ||
} | ||
if (path.node[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER]) { | ||
return; | ||
} | ||
// Check for a call to undeclared aexpr: | ||
if (t.isCallExpression(path.parent) && path.node.name === AEXPR_IDENTIFIER_NAME && !path.scope.hasBinding(AEXPR_IDENTIFIER_NAME)) { | ||
//logIdentifier("call to aexpr", path); | ||
path.replaceWith(addCustomTemplate(state.file, AEXPR_IDENTIFIER_NAME)); | ||
@@ -108,4 +161,49 @@ return; | ||
if ( | ||
// TODO: is there a general way to exclude non-variables? | ||
!t.isObjectProperty(path.parent) && !t.isClassDeclaration(path.parent) && !t.isClassMethod(path.parent) && !t.isImportSpecifier(path.parent) && !t.isMemberExpression(path.parent) && !t.isObjectMethod(path.parent) && !t.isVariableDeclarator(path.parent) && !t.isFunctionDeclaration(path.parent) && !t.isRestElement(path.parent) && (!t.isAssignmentExpression(path.parent) || !(path.parentKey === 'left'))) { | ||
if (path.scope.hasBinding(path.node.name)) { | ||
//logIdentifier('get local var', path) | ||
path.node[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER] = true; | ||
var parentWithScope = path.findParent(function (par) { | ||
return par.scope.hasOwnBinding(path.node.name); | ||
}); | ||
if (parentWithScope) { | ||
path.replaceWith(t.sequenceExpression([t.callExpression(addCustomTemplate(state.file, GET_LOCAL), [getIdentifierForExplicitScopeObject(parentWithScope), t.stringLiteral(path.node.name)]), path.node])); | ||
} | ||
} else { | ||
//logIdentifier('get global var', path); | ||
path.node[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER] = true; | ||
path.replaceWith(t.sequenceExpression([t.callExpression(addCustomTemplate(state.file, GET_GLOBAL), [t.stringLiteral(path.node.name)]), path.node])); | ||
} | ||
return; | ||
} | ||
//logIdentifier('others', path); | ||
return; | ||
if (path.node[GENERATED_IMPORT_IDENTIFIER]) { | ||
logIdentifier('Generated Import Identifier', path); | ||
return; | ||
} | ||
// global? | ||
//if (!path.isReferencedIdentifier()) { | ||
//logIdentifier('is no referenced identifier', path); | ||
//return; | ||
//} | ||
// The identifier should not reference a variable in current scope | ||
if (path.scope.hasBinding(path.node.name)) { | ||
logIdentifier('Binding for ', path); | ||
return; | ||
} | ||
logIdentifier('Unexpected Case', path); | ||
return; | ||
//if(RESERVED_IDENTIFIERS.includes(path.node.name)) { return; } | ||
@@ -149,18 +247,33 @@ | ||
// check, whether we assign to a member (no support for pattern right now) | ||
if (!t.isMemberExpression(path.node.left)) { | ||
return; | ||
if (t.isMemberExpression(path.node.left) && !isGenerated(path) && SET_MEMBER_BY_OPERATORS[path.node.operator]) { | ||
//state.file.addImport | ||
path.replaceWith(t.callExpression(addCustomTemplate(state.file, SET_MEMBER_BY_OPERATORS[path.node.operator]), [path.node.left.object, getPropertyFromMemberExpression(path.node.left), | ||
//t.stringLiteral(path.node.operator), | ||
path.node.right])); | ||
} | ||
if (isGenerated(path)) { | ||
return; | ||
} | ||
//state.file.addImport | ||
if (t.isIdentifier(path.node.left) && !path.node[FLAG_SHOULD_NOT_REWRITE_ASSIGNMENT_EXPRESSION]) { | ||
if (path.scope.hasBinding(path.node.left.name)) { | ||
//console.log('set local', path.node.left.name); | ||
path.node[FLAG_SHOULD_NOT_REWRITE_ASSIGNMENT_EXPRESSION] = true; | ||
if (!SET_MEMBER_BY_OPERATORS[path.node.operator]) { | ||
return; | ||
var parentWithScope = path.findParent(function (par) { | ||
return par.scope.hasOwnBinding(path.node.left.name); | ||
}); | ||
if (parentWithScope) { | ||
var valueToReturn = t.identifier(path.node.left.name); | ||
valueToReturn[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER] = true; | ||
path.replaceWith(t.sequenceExpression([path.node, t.callExpression(addCustomTemplate(state.file, SET_LOCAL), [getIdentifierForExplicitScopeObject(parentWithScope), t.stringLiteral(path.node.left.name)]), valueToReturn])); | ||
} | ||
} else { | ||
// global assginment | ||
//console.log('---global---', path.node.left.name); | ||
path.node[FLAG_SHOULD_NOT_REWRITE_ASSIGNMENT_EXPRESSION] = true; | ||
var _valueToReturn = t.identifier(path.node.left.name); | ||
_valueToReturn[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER] = true; | ||
path.replaceWith(t.sequenceExpression([path.node, t.callExpression(addCustomTemplate(state.file, SET_GLOBAL), [t.stringLiteral(path.node.left.name)]), _valueToReturn])); | ||
} | ||
} | ||
path.replaceWith(t.callExpression(addCustomTemplate(state.file, SET_MEMBER_BY_OPERATORS[path.node.operator]), [path.node.left.object, getPropertyFromMemberExpression(path.node.left), | ||
//t.stringLiteral(path.node.operator), | ||
path.node.right])); | ||
}, | ||
@@ -175,3 +288,3 @@ MemberExpression: function MemberExpression(path) { | ||
} | ||
//FLAG_SHOULD_NOT_REWRITE_ASSIGNMENT_EXPRESSION | ||
path.replaceWith(t.callExpression(addCustomTemplate(state.file, GET_MEMBER), [path.node.object, getPropertyFromMemberExpression(path.node)])); | ||
@@ -193,114 +306,2 @@ }, | ||
} | ||
}, | ||
// TODO: also | ||
Identifier: function Identifier(path, state) { | ||
//console.log(state); | ||
// Check for a call to aexpr: | ||
if (t.isCallExpression(path.parent) && path.node.name === AEXPR_IDENTIFIER_NAME && !path.scope.hasBinding(AEXPR_IDENTIFIER_NAME)) { | ||
//path.replaceWith( | ||
//addCustomTemplate(state.file, AEXPR_IDENTIFIER_NAME) | ||
//); | ||
return; | ||
} | ||
return; | ||
//if(RESERVED_IDENTIFIERS.includes(path.node.name)) { return; } | ||
if (t.isClassDeclaration(path.parent)) { | ||
console.log("classDecl", path.node.name); | ||
return; | ||
} | ||
if (t.isClassMethod(path.parent)) { | ||
console.log("classMethod", path.node.name); | ||
return; | ||
} | ||
if (t.isObjectMethod(path.parent)) { | ||
console.log("objectMethod", path.node.name); | ||
return; | ||
} | ||
if (t.isVariableDeclarator(path.parent)) { | ||
console.log("varDecl", path.node.name); | ||
return; | ||
} | ||
// is this correct here? | ||
// TODO: is it correct for the locals plugin? | ||
if (!path.isReferencedIdentifier()) { | ||
console.log("def", path.node.name); | ||
return; | ||
} | ||
// is locally defined variable? | ||
if (path.scope.hasBinding(path.node.name)) { | ||
console.log("local", path.node.name); | ||
} else { | ||
// we have a global | ||
console.log("global", path.node.name); | ||
} | ||
}, | ||
AssignmentExpression: function AssignmentExpression(path, state) { | ||
// check, whether we assign to a member (no support for pattern right now) | ||
if (!t.isMemberExpression(path.node.left)) { | ||
return; | ||
} | ||
if (isGenerated(path)) { | ||
return; | ||
} | ||
//state.file.addImport | ||
//path.replaceWith( | ||
// t.callExpression( | ||
//addCustomTemplate(state.file, SET_MEMBER)//, | ||
// [ | ||
// path.node.left.object, | ||
// getPropertyFromMemberExpression(path.node.left), | ||
// t.stringLiteral(path.node.operator), | ||
// path.node.right | ||
// ] | ||
// ) | ||
//); | ||
}, | ||
MemberExpression: function MemberExpression(path, state) { | ||
// lval (left values) are ignored for now | ||
if (t.isAssignmentExpression(path.parent) && path.key === 'left') { | ||
return; | ||
} | ||
if (isGenerated(path)) { | ||
return; | ||
} | ||
//path.replaceWith( | ||
// t.callExpression( | ||
//addCustomTemplate(state.file, GET_MEMBER)//, | ||
// [ | ||
// path.node.object, | ||
// getPropertyFromMemberExpression(path.node) | ||
//] | ||
//) | ||
// | ||
}, | ||
CallExpression: function CallExpression(path, state) { | ||
if (isGenerated(path)) { | ||
return; | ||
} | ||
// check whether we call a MemberExpression | ||
if (t.isMemberExpression(path.node.callee)) { | ||
// path.replaceWith( | ||
// t.callExpression( | ||
//addCustomTemplate(state.file, GET_AND_CALL_MEMBER)//, | ||
// [ | ||
// path.node.callee.object, | ||
// getPropertyFromMemberExpression(path.node.callee), | ||
// t.arrayExpression(path.node.arguments) | ||
// ] | ||
// ) | ||
// ) | ||
} else { | ||
if (t.isIdentifier(path.node.callee) && true) {} | ||
} | ||
} | ||
@@ -313,3 +314,2 @@ } | ||
var SET_MEMBER = "setMember"; | ||
var GET_MEMBER = "getMember"; | ||
@@ -337,6 +337,12 @@ var GET_AND_CALL_MEMBER = "getAndCallMember"; | ||
// const SET_LOCAL = "setLocal"; | ||
// const GET_LOCAL = "getLocal"; | ||
var SET_LOCAL = "setLocal"; | ||
var GET_LOCAL = "getLocal"; | ||
// const SET_GLOBAL = "setGlobal"; | ||
// const GET_GLOBAL = "getGlobal"; | ||
var SET_GLOBAL = "setGlobal"; | ||
var GET_GLOBAL = "getGlobal"; | ||
// TODO: use multiple flag for indication of generated content, marking explicit scopes, etc. | ||
var FLAG_GENERATED_SCOPE_OBJECT = Symbol('FLAG: generated scope object'); | ||
var FLAG_SHOULD_NOT_REWRITE_IDENTIFIER = Symbol('FLAG: should not rewrite identifier'); | ||
var FLAG_SHOULD_NOT_REWRITE_MEMBER_EXPRESSION = Symbol('FLAG: should not rewrite member expression'); | ||
var FLAG_SHOULD_NOT_REWRITE_ASSIGNMENT_EXPRESSION = Symbol('FLAG: should not rewrite assignment expression'); |
420
index.js
const AEXPR_IDENTIFIER_NAME = "aexpr"; | ||
const SET_MEMBER = "setMember"; | ||
const GET_MEMBER = "getMember"; | ||
@@ -26,8 +25,14 @@ const GET_AND_CALL_MEMBER = "getAndCallMember"; | ||
// const SET_LOCAL = "setLocal"; | ||
// const GET_LOCAL = "getLocal"; | ||
const SET_LOCAL = "setLocal"; | ||
const GET_LOCAL = "getLocal"; | ||
// const SET_GLOBAL = "setGlobal"; | ||
// const GET_GLOBAL = "getGlobal"; | ||
const SET_GLOBAL = "setGlobal"; | ||
const GET_GLOBAL = "getGlobal"; | ||
// TODO: use multiple flag for indication of generated content, marking explicit scopes, etc. | ||
const FLAG_GENERATED_SCOPE_OBJECT = Symbol('FLAG: generated scope object'); | ||
const FLAG_SHOULD_NOT_REWRITE_IDENTIFIER = Symbol('FLAG: should not rewrite identifier'); | ||
const FLAG_SHOULD_NOT_REWRITE_MEMBER_EXPRESSION = Symbol('FLAG: should not rewrite member expression'); | ||
const FLAG_SHOULD_NOT_REWRITE_ASSIGNMENT_EXPRESSION = Symbol('FLAG: should not rewrite assignment expression'); | ||
export default function(param) { | ||
@@ -53,27 +58,29 @@ let { types: t, template, traverse } = param; | ||
let customTemplates = {} | ||
customTemplates[SET_MEMBER] = template(` | ||
(function(obj, prop, operator, val) { | ||
return obj[prop] = val; | ||
}); | ||
`); | ||
const GENERATED_IMPORT_IDENTIFIER = Symbol("generated import identifier"); | ||
customTemplates[GET_MEMBER] = template(` | ||
(function(obj, prop) { | ||
return obj[prop]; | ||
}); | ||
`); | ||
// let customTemplates = {}; | ||
// customTemplates[SET_MEMBER] = template(` | ||
// (function(obj, prop, operator, val) { | ||
// return obj[prop] = val; | ||
// }); | ||
// `); | ||
// | ||
// customTemplates[GET_MEMBER] = template(` | ||
// (function(obj, prop) { | ||
// return obj[prop]; | ||
// }); | ||
// `); | ||
// | ||
// customTemplates[GET_AND_CALL_MEMBER] = template(` | ||
// (function(obj, prop, args) { | ||
// return obj[prop](...args) | ||
// }); | ||
// `); | ||
// | ||
// customTemplates[AEXPR_IDENTIFIER_NAME] = template(` | ||
// (function(expr) { | ||
// return { onChange(cb) {}}; | ||
// }); | ||
// `); | ||
customTemplates[GET_AND_CALL_MEMBER] = template(` | ||
(function(obj, prop, args) { | ||
return obj[prop](...args) | ||
}); | ||
`); | ||
customTemplates[AEXPR_IDENTIFIER_NAME] = template(` | ||
(function(expr) { | ||
return { onChange(cb) {}}; | ||
}); | ||
`); | ||
function addCustomTemplate(file, name) { | ||
@@ -83,27 +90,29 @@ let declar = file.declarations[name]; | ||
return file.declarations[name] = file.addImport("aexpr-source-transformation-propagation", name, name); | ||
let identifier = file.declarations[name] = file.addImport("aexpr-source-transformation-propagation", name, name); | ||
identifier[GENERATED_IMPORT_IDENTIFIER] = true; | ||
return identifier; | ||
let ref = customTemplates[name]; | ||
console.log(file.addImport("aexpr-source-transformation-propagation", "aexpr")); | ||
let uid = file.declarations[name] = file.scope.generateUidIdentifier(name); | ||
ref = ref().expression; | ||
ref[GENERATED_FUNCTION] = true | ||
if (t.isFunctionExpression(ref) && !ref.id) { | ||
ref.body._compact = true; | ||
ref._generated = true; | ||
ref.id = uid; | ||
ref.type = "FunctionDeclaration"; | ||
file.path.unshiftContainer("body", ref); | ||
} else { | ||
ref._compact = true; | ||
file.scope.push({ | ||
id: uid, | ||
init: ref, | ||
unique: true | ||
}); | ||
} | ||
return uid; | ||
// let ref = customTemplates[name]; | ||
// console.log(file.addImport("aexpr-source-transformation-propagation", "aexpr")); | ||
// let uid = file.declarations[name] = file.scope.generateUidIdentifier(name); | ||
// | ||
// ref = ref().expression; | ||
// ref[GENERATED_FUNCTION] = true; | ||
// | ||
// if (t.isFunctionExpression(ref) && !ref.id) { | ||
// ref.body._compact = true; | ||
// ref._generated = true; | ||
// ref.id = uid; | ||
// ref.type = "FunctionDeclaration"; | ||
// file.path.unshiftContainer("body", ref); | ||
// } else { | ||
// ref._compact = true; | ||
// file.scope.push({ | ||
// id: uid, | ||
// init: ref, | ||
// unique: true | ||
// }); | ||
// } | ||
// | ||
// return uid; | ||
} | ||
@@ -121,3 +130,3 @@ | ||
) { | ||
console.log("IGNORED!!!") | ||
console.log("IGNORED!!!"); | ||
file[IGNORE_INDICATOR] = true; | ||
@@ -132,8 +141,41 @@ } | ||
console.log("file", path, state); | ||
if(state.file[IGNORE_INDICATOR]) { console.log("read ignored"); return; }; | ||
if(state.file[IGNORE_INDICATOR]) { console.log("read ignored"); return; } | ||
function getIdentifierForExplicitScopeObject(parentWithScope) { | ||
let bindings = parentWithScope.scope.bindings; | ||
let scopeName = Object.keys(bindings).find(key => { | ||
return bindings[key].path && | ||
bindings[key].path.node && | ||
bindings[key].path.node.id && | ||
bindings[key].path.node.id[FLAG_GENERATED_SCOPE_OBJECT] // should actually be IS_EXPLICIT_SCOPE_OBJECT | ||
}); | ||
let uniqueIdentifier; | ||
if(scopeName) { | ||
uniqueIdentifier = t.identifier(scopeName); | ||
} else { | ||
uniqueIdentifier = parentWithScope.scope.generateUidIdentifier('scope'); | ||
uniqueIdentifier[FLAG_GENERATED_SCOPE_OBJECT] = true; | ||
parentWithScope.scope.push({ | ||
kind: 'let', | ||
id: uniqueIdentifier, | ||
init: t.objectExpression([]) | ||
}); | ||
} | ||
uniqueIdentifier[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER] = true; | ||
return uniqueIdentifier; | ||
} | ||
path.traverse({ | ||
Identifier(path) { | ||
//console.log(path.node.name) | ||
// Check for a call to aexpr: | ||
function logIdentifier(msg, path) { | ||
console.log(msg, path.node.name, path.node.loc ? path.node.loc.start.line : ''); | ||
} | ||
if(path.node[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER]) { return; } | ||
// Check for a call to undeclared aexpr: | ||
if( | ||
@@ -144,2 +186,3 @@ t.isCallExpression(path.parent) && | ||
) { | ||
//logIdentifier("call to aexpr", path); | ||
path.replaceWith( | ||
@@ -151,4 +194,78 @@ addCustomTemplate(state.file, AEXPR_IDENTIFIER_NAME) | ||
if( | ||
// TODO: is there a general way to exclude non-variables? | ||
!t.isObjectProperty(path.parent) && | ||
!t.isClassDeclaration(path.parent) && | ||
!t.isClassMethod(path.parent) && | ||
!t.isImportSpecifier(path.parent) && | ||
!t.isMemberExpression(path.parent) && | ||
!t.isObjectMethod(path.parent) && | ||
!t.isVariableDeclarator(path.parent) && | ||
!t.isFunctionDeclaration(path.parent) && | ||
!t.isRestElement(path.parent) && | ||
(!t.isAssignmentExpression(path.parent) || !(path.parentKey === 'left')) | ||
) { | ||
if(path.scope.hasBinding(path.node.name)) { | ||
//logIdentifier('get local var', path) | ||
path.node[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER] = true; | ||
let parentWithScope = path.findParent(par => | ||
par.scope.hasOwnBinding(path.node.name) | ||
); | ||
if(parentWithScope) { | ||
path.replaceWith( | ||
t.sequenceExpression([ | ||
t.callExpression( | ||
addCustomTemplate(state.file, GET_LOCAL), | ||
[ | ||
getIdentifierForExplicitScopeObject(parentWithScope), | ||
t.stringLiteral(path.node.name) | ||
] | ||
), | ||
path.node | ||
]) | ||
); | ||
} | ||
} else { | ||
//logIdentifier('get global var', path); | ||
path.node[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER] = true; | ||
path.replaceWith( | ||
t.sequenceExpression([ | ||
t.callExpression( | ||
addCustomTemplate(state.file, GET_GLOBAL), | ||
[t.stringLiteral(path.node.name)] | ||
), | ||
path.node | ||
]) | ||
); | ||
} | ||
return; | ||
} | ||
//logIdentifier('others', path); | ||
return; | ||
if(path.node[GENERATED_IMPORT_IDENTIFIER]) { | ||
logIdentifier('Generated Import Identifier', path) | ||
return; | ||
} | ||
// global? | ||
//if (!path.isReferencedIdentifier()) { | ||
//logIdentifier('is no referenced identifier', path); | ||
//return; | ||
//} | ||
// The identifier should not reference a variable in current scope | ||
if (path.scope.hasBinding(path.node.name)) { | ||
logIdentifier('Binding for ', path); | ||
return; | ||
} | ||
logIdentifier('Unexpected Case', path); | ||
return; | ||
//if(RESERVED_IDENTIFIERS.includes(path.node.name)) { return; } | ||
@@ -192,20 +309,69 @@ | ||
// check, whether we assign to a member (no support for pattern right now) | ||
if(!t.isMemberExpression(path.node.left)) { return; } | ||
if(isGenerated(path)) { return; } | ||
if(t.isMemberExpression(path.node.left) && | ||
!isGenerated(path) && | ||
SET_MEMBER_BY_OPERATORS[path.node.operator] | ||
) { | ||
//state.file.addImport | ||
path.replaceWith( | ||
t.callExpression( | ||
addCustomTemplate(state.file, SET_MEMBER_BY_OPERATORS[path.node.operator]), | ||
[ | ||
path.node.left.object, | ||
getPropertyFromMemberExpression(path.node.left), | ||
//t.stringLiteral(path.node.operator), | ||
path.node.right | ||
] | ||
) | ||
); | ||
} | ||
//state.file.addImport | ||
if(!SET_MEMBER_BY_OPERATORS[path.node.operator]) { return; } | ||
if(t.isIdentifier(path.node.left) && | ||
!path.node[FLAG_SHOULD_NOT_REWRITE_ASSIGNMENT_EXPRESSION]) { | ||
if(path.scope.hasBinding(path.node.left.name)) { | ||
//console.log('set local', path.node.left.name); | ||
path.node[FLAG_SHOULD_NOT_REWRITE_ASSIGNMENT_EXPRESSION] = true; | ||
path.replaceWith( | ||
t.callExpression( | ||
addCustomTemplate(state.file, SET_MEMBER_BY_OPERATORS[path.node.operator]), | ||
[ | ||
path.node.left.object, | ||
getPropertyFromMemberExpression(path.node.left), | ||
//t.stringLiteral(path.node.operator), | ||
path.node.right | ||
] | ||
) | ||
); | ||
let parentWithScope = path.findParent(par => | ||
par.scope.hasOwnBinding(path.node.left.name) | ||
); | ||
if(parentWithScope) { | ||
let valueToReturn = t.identifier(path.node.left.name); | ||
valueToReturn[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER] = true; | ||
path.replaceWith( | ||
t.sequenceExpression([ | ||
path.node, | ||
t.callExpression( | ||
addCustomTemplate(state.file, SET_LOCAL), | ||
[ | ||
getIdentifierForExplicitScopeObject(parentWithScope), | ||
t.stringLiteral(path.node.left.name) | ||
] | ||
), | ||
valueToReturn | ||
]) | ||
); | ||
} | ||
} else { | ||
// global assginment | ||
//console.log('---global---', path.node.left.name); | ||
path.node[FLAG_SHOULD_NOT_REWRITE_ASSIGNMENT_EXPRESSION] = true; | ||
let valueToReturn = t.identifier(path.node.left.name); | ||
valueToReturn[FLAG_SHOULD_NOT_REWRITE_IDENTIFIER] = true; | ||
path.replaceWith( | ||
t.sequenceExpression([ | ||
path.node, | ||
t.callExpression( | ||
addCustomTemplate(state.file, SET_GLOBAL), | ||
[t.stringLiteral(path.node.left.name)] | ||
), | ||
valueToReturn | ||
])); | ||
} | ||
} | ||
}, | ||
@@ -217,3 +383,3 @@ | ||
if(isGenerated(path)) { return; } | ||
//FLAG_SHOULD_NOT_REWRITE_ASSIGNMENT_EXPRESSION | ||
path.replaceWith( | ||
@@ -249,117 +415,5 @@ t.callExpression( | ||
} | ||
} | ||
}); | ||
} | ||
}, | ||
// TODO: also | ||
Identifier(path, state) { | ||
//console.log(state); | ||
// Check for a call to aexpr: | ||
if( | ||
t.isCallExpression(path.parent) && | ||
path.node.name === AEXPR_IDENTIFIER_NAME && | ||
!path.scope.hasBinding(AEXPR_IDENTIFIER_NAME) | ||
) { | ||
//path.replaceWith( | ||
//addCustomTemplate(state.file, AEXPR_IDENTIFIER_NAME) | ||
//); | ||
return; | ||
} | ||
return; | ||
//if(RESERVED_IDENTIFIERS.includes(path.node.name)) { return; } | ||
if(t.isClassDeclaration(path.parent)) { | ||
console.log("classDecl", path.node.name); | ||
return; | ||
} | ||
if(t.isClassMethod(path.parent)) { | ||
console.log("classMethod", path.node.name); | ||
return; | ||
} | ||
if(t.isObjectMethod(path.parent)) { | ||
console.log("objectMethod", path.node.name); | ||
return; | ||
} | ||
if(t.isVariableDeclarator(path.parent)) { | ||
console.log("varDecl", path.node.name); | ||
return; | ||
} | ||
// is this correct here? | ||
// TODO: is it correct for the locals plugin? | ||
if (!path.isReferencedIdentifier()) { | ||
console.log("def", path.node.name); | ||
return; | ||
} | ||
// is locally defined variable? | ||
if (path.scope.hasBinding(path.node.name)) { | ||
console.log("local", path.node.name); | ||
} else { | ||
// we have a global | ||
console.log("global", path.node.name); | ||
} | ||
}, | ||
AssignmentExpression(path, state) { | ||
// check, whether we assign to a member (no support for pattern right now) | ||
if(!t.isMemberExpression(path.node.left)) { return; } | ||
if(isGenerated(path)) { return; } | ||
//state.file.addImport | ||
//path.replaceWith( | ||
// t.callExpression( | ||
//addCustomTemplate(state.file, SET_MEMBER)//, | ||
// [ | ||
// path.node.left.object, | ||
// getPropertyFromMemberExpression(path.node.left), | ||
// t.stringLiteral(path.node.operator), | ||
// path.node.right | ||
// ] | ||
// ) | ||
//); | ||
}, | ||
MemberExpression(path, state) { | ||
// lval (left values) are ignored for now | ||
if(t.isAssignmentExpression(path.parent) && path.key === 'left') { return; } | ||
if(isGenerated(path)) { return; } | ||
//path.replaceWith( | ||
// t.callExpression( | ||
//addCustomTemplate(state.file, GET_MEMBER)//, | ||
// [ | ||
// path.node.object, | ||
// getPropertyFromMemberExpression(path.node) | ||
//] | ||
//) | ||
// | ||
}, | ||
CallExpression(path, state) { | ||
if(isGenerated(path)) { return; } | ||
// check whether we call a MemberExpression | ||
if(t.isMemberExpression(path.node.callee)) { | ||
// path.replaceWith( | ||
// t.callExpression( | ||
//addCustomTemplate(state.file, GET_AND_CALL_MEMBER)//, | ||
// [ | ||
// path.node.callee.object, | ||
// getPropertyFromMemberExpression(path.node.callee), | ||
// t.arrayExpression(path.node.arguments) | ||
// ] | ||
// ) | ||
// ) | ||
} else { | ||
if(t.isIdentifier(path.node.callee) && true) { | ||
} | ||
} | ||
} | ||
@@ -366,0 +420,0 @@ } |
{ | ||
"name": "babel-plugin-aexpr-source-transformation", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "3rd implementation strategy of active expressions, via a babel transformation", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import { getMember as _getMember } from "aexpr-source-transformation-propagation"; | ||
import { getLocal as _getLocal } from "aexpr-source-transformation-propagation"; | ||
import { getAndCallMember as _getAndCallMember } from "aexpr-source-transformation-propagation"; | ||
import { setMember as _setMember } from "aexpr-source-transformation-propagation"; | ||
let _scope = {}; | ||
var a = { b: 1, fn() { | ||
@@ -10,2 +12,2 @@ return {}; | ||
// Mixin everything together | ||
_setMember(_getAndCallMember(a, "fn", []), _getMember(c, "d"), 1); | ||
_setMember(_getAndCallMember((_getLocal(_scope, "a"), a), "fn", []), _getMember((_getLocal(_scope, "c"), c), "d"), 1); |
import { getMember as _getMember } from "aexpr-source-transformation-propagation"; | ||
import { getLocal as _getLocal } from "aexpr-source-transformation-propagation"; | ||
import { setMember as _setMember } from "aexpr-source-transformation-propagation"; | ||
class Rectangle { | ||
constructor(x, y, width, height) { | ||
_setMember(this, "x", x); | ||
_setMember(this, "y", y); | ||
_setMember(this, "width", width); | ||
_setMember(this, "height", height); | ||
let _scope = {}; | ||
_setMember(this, "x", (_getLocal(_scope, "x"), x)); | ||
_setMember(this, "y", (_getLocal(_scope, "y"), y)); | ||
_setMember(this, "width", (_getLocal(_scope, "width"), width)); | ||
_setMember(this, "height", (_getLocal(_scope, "height"), height)); | ||
} | ||
@@ -10,0 +13,0 @@ |
@@ -0,4 +1,5 @@ | ||
import { getGlobal as _getGlobal } from "aexpr-source-transformation-propagation"; | ||
import { getMember as _getMember } from "aexpr-source-transformation-propagation"; | ||
import { setMember as _setMember } from "aexpr-source-transformation-propagation"; | ||
// nested left side of assignment | ||
_setMember(_getMember(a, "b"), "c", _getMember(y, "z")); | ||
_setMember(_getMember((_getGlobal("a"), a), "b"), "c", _getMember((_getGlobal("y"), y), "z")); |
import { setMemberAddition as _setMemberAddition } from "aexpr-source-transformation-propagation"; | ||
import { getGlobal as _getGlobal } from "aexpr-source-transformation-propagation"; | ||
import { setMemberUnsignedRightShift as _setMemberUnsignedRightShift } from "aexpr-source-transformation-propagation"; | ||
_setMemberUnsignedRightShift(a, "a", i); | ||
_setMemberAddition(a, "b", 15); | ||
_setMemberUnsignedRightShift((_getGlobal("a"), a), "a", (_getGlobal("i"), i)); | ||
_setMemberAddition((_getGlobal("a"), a), "b", 15); |
@@ -0,3 +1,4 @@ | ||
import { getGlobal as _getGlobal } from "aexpr-source-transformation-propagation"; | ||
import { aexpr as _aexpr } from "aexpr-source-transformation-propagation"; | ||
import { getAndCallMember as _getAndCallMember } from "aexpr-source-transformation-propagation"; | ||
_getAndCallMember(_aexpr(() => _getAndCallMember(r1, "area", []), foo), "onChange", [() => _getAndCallMember(console, "log", ["changed"])]); | ||
_getAndCallMember(_aexpr(() => _getAndCallMember((_getGlobal("r1"), r1), "area", []), (_getGlobal("foo"), foo)), "onChange", [() => _getAndCallMember((_getGlobal("console"), console), "log", ["changed"])]); |
@@ -13,6 +13,8 @@ "use strict"; | ||
{file: 'ignore'}, | ||
{file: 'nested_locals'}, | ||
{file: 'class_example'}, | ||
{file: 'locals_and_globals'}, | ||
{file: 'nested_left_assignment'}, | ||
{file: 'all-accesses'}, | ||
{file: 'class_example'}, | ||
{file: 'class_example'}, | ||
{file: 'with_aexpr'}, | ||
@@ -19,0 +21,0 @@ {file: 'operators'} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47492
29
907
1