Skip to content

Commit

Permalink
feat(scope): Updated scoping handlers
Browse files Browse the repository at this point in the history
Increased to handle classes
deeper more accurate scopes
  • Loading branch information
kwelch committed May 1, 2017
1 parent 0c11423 commit 036ef7e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/__test__/__fixtures__/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const Component = () => {
const privateMethod = () => {
console.log(1);
};
return class {
return class HighOrderComponent {
showList() {
arr.map(i => {
console.log(i);
Expand Down
14 changes: 7 additions & 7 deletions src/__test__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const Component = () => {
const privateMethod = () => {
console.log(1);
};
return class {
return class HighOrderComponent {
showList() {
arr.map(i => {
console.log(i);
Expand Down Expand Up @@ -83,28 +83,28 @@ const power = function pow() {
const obj = {
method1: function () {
console.log(\\"all.js(29:4)\\", \\"method1:\\", 6);
console.log(\\"all.js(29:4)\\", \\"obj.method1:\\", 6);
},
method2: () => {
console.log(\\"all.js(32:4)\\", \\"method2:\\", 7);
console.log(\\"all.js(32:4)\\", \\"obj.method2:\\", 7);
},
method3() {
console.log(\\"all.js(35:4)\\", \\"method3:\\", 8);
console.log(\\"all.js(35:4)\\", \\"obj.method3:\\", 8);
}
};
const Component = () => {
const privateMethod = () => {
console.log(\\"all.js(41:4)\\", \\"Component.privateMethod:\\", 1);
};
return class {
return class HighOrderComponent {
showList() {
arr.map(i => {
console.log(\\"all.js(46:8)\\", \\"i\\", i);
console.log(\\"all.js(46:8)\\", \\"Component.HighOrderComponent.showList:\\", \\"i\\", i);
});
}
render() {
console.log(\\"all.js(50:6)\\", \\"Component.render:\\", 2);
console.log(\\"all.js(50:6)\\", \\"Component.HighOrderComponent.render:\\", 2);
}
};
};"
Expand Down
14 changes: 7 additions & 7 deletions src/__test__/__snapshots__/logMethodOnly.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const Component = () => {
const privateMethod = () => {
console.log(1);
};
return class {
return class HighOrderComponent {
showList() {
arr.map(i => {
console.log(i);
Expand Down Expand Up @@ -83,28 +83,28 @@ const power = function pow() {
const obj = {
method1: function () {
console.log(\\"all.js(29:4)\\", \\"method1:\\", 6);
console.log(\\"all.js(29:4)\\", \\"obj.method1:\\", 6);
},
method2: () => {
console.log(\\"all.js(32:4)\\", \\"method2:\\", 7);
console.log(\\"all.js(32:4)\\", \\"obj.method2:\\", 7);
},
method3() {
console.log(\\"all.js(35:4)\\", \\"method3:\\", 8);
console.log(\\"all.js(35:4)\\", \\"obj.method3:\\", 8);
}
};
const Component = () => {
const privateMethod = () => {
console.log(\\"all.js(41:4)\\", \\"Component.privateMethod:\\", 1);
};
return class {
return class HighOrderComponent {
showList() {
arr.map(i => {
console.log(\\"all.js(46:8)\\", \\"i\\", i);
console.log(\\"all.js(46:8)\\", \\"Component.HighOrderComponent.showList:\\", \\"i\\", i);
});
}
render() {
console.log(\\"all.js(50:6)\\", \\"Component.render:\\", 2);
console.log(\\"all.js(50:6)\\", \\"Component.HighOrderComponent.render:\\", 2);
}
};
};"
Expand Down
55 changes: 22 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ const defaultSettings = {

const defaultMethods = ["debug", "error", "exception", "info", "log", "warn"];

const idNameSelector = path => path.node.id.name;
const keyNameSelector = path => path.node.key.name;

const scopeHandlers = {
FunctionDeclaration: idNameSelector,
VariableDeclarator: idNameSelector,
ObjectProperty: keyNameSelector,
ObjectMethod: keyNameSelector,
ClassMethod: keyNameSelector,
ClassExpression: idNameSelector,
ClassDeclaration: idNameSelector,

AssignmentExpression: path => path.node.left.name,
};

export default function({ types: t }) {
const name = "babel-plugin-captains-log";
const callExpressions = new Set();
Expand Down Expand Up @@ -92,40 +107,14 @@ export default function({ types: t }) {
}

function findCallScope(path, scope = []) {
const parentFunc = path.findParent(t.isFunction);
const parentFunc = path.findParent(path =>
Object.keys(scopeHandlers).includes(path.type)
);
if (parentFunc) {
if (t.isFunctionDeclaration(parentFunc)) {
return findCallScope(parentFunc, [parentFunc.node.id.name, ...scope]);
}
if (
t.isFunctionExpression(parentFunc) ||
t.isArrowFunctionExpression(parentFunc)
) {
if (looksLike(parentFunc.parent, { type: "VariableDeclarator" })) {
return findCallScope(parentFunc.parentPath, [
parentFunc.parent.id.name,
...scope,
]);
}
if (looksLike(parentFunc.parent, { type: "AssignmentExpression" })) {
return findCallScope(parentFunc.parentPath, [
parentFunc.parent.left.name,
...scope,
]);
}
if (looksLike(parentFunc.parent, { type: "ObjectProperty" })) {
return findCallScope(parentFunc.parentPath, [
parentFunc.parent.key.name,
...scope,
]);
}
}
if (t.isObjectMethod(parentFunc) || parentFunc.isClassMethod()) {
return findCallScope(parentFunc.parentPath, [
parentFunc.node.key.name,
...scope,
]);
}
return findCallScope(parentFunc, [
scopeHandlers[parentFunc.type](parentFunc),
...scope,
]);
}
return scope.length ? `${scope.join(".")}:` : "";
}
Expand Down

0 comments on commit 036ef7e

Please sign in to comment.