Skip to content

Commit

Permalink
feat(ignore): added ability to ignore patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
kwelch committed May 6, 2017
1 parent 2ed69b8 commit 66b3ff8
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 6 deletions.
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,22 @@ This option provides control over which console statments are adjusted. Methods
{
plugins: [
["captains-log", {
methods: ['debug', 'info']
"methods": ['debug', 'info']
}]
]
}
```

### Ignore Patterns
This option provides control over which files are adjusted. Ignore Patterns is set within your `.babelrc` as an array of strings.

**Default**: `["node_modules"]`

```
{
plugins: [
["captains-log", {
"ignorePatterns": ["node_modules", ".spec.js"]
}]
]
}
Expand All @@ -85,7 +100,7 @@ Flags are values set for all methods and are used to turn that feature on or off
{
plugins: [
["captains-log", {
injectVariableName: true
"injectVariableName": true
}]
]
}
Expand All @@ -98,7 +113,7 @@ Flags are values set for all methods and are used to turn that feature on or off
{
plugins: [
["captains-log", {
injectScope: true
"injectScope": true
}]
]
}
Expand All @@ -111,7 +126,7 @@ Flags are values set for all methods and are used to turn that feature on or off
{
plugins: [
["captains-log", {
injectFileName: true
"injectFileName": true
}]
]
}
Expand Down
193 changes: 193 additions & 0 deletions src/__test__/__snapshots__/ignoreRegEx.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`all.js 1`] = `
"console.log(a);
console.log(a.length);
console.log(a.map());
function add() {
console.log(\\"1\\");
}
const subtract = () => {
console.log(\\"2\\");
};
let multiply;
multiple = () => {
console.log(\\"3\\");
};
let divide;
divide = function() {
console.log(\\"4\\");
};
const power = function pow() {
console.log(5);
};
const obj = {
method1: function() {
console.log(6);
},
method2: () => {
console.log(7);
},
method3() {
console.log(8);
},
};
const Component = () => {
const privateMethod = () => {
console.log(1);
};
return class HighOrderComponent {
showList() {
arr.map(i => {
console.log(i);
});
}
render() {
console.log(2);
}
};
};
class ToDoComponent {
render() {
console.log(this.props);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
console.log(a);
console.log(a.length);
console.log(a.map());
function add() {
console.log(\\"1\\");
}
const subtract = () => {
console.log(\\"2\\");
};
let multiply;
multiple = () => {
console.log(\\"3\\");
};
let divide;
divide = function () {
console.log(\\"4\\");
};
const power = function pow() {
console.log(5);
};
const obj = {
method1: function () {
console.log(6);
},
method2: () => {
console.log(7);
},
method3() {
console.log(8);
}
};
const Component = () => {
const privateMethod = () => {
console.log(1);
};
return class HighOrderComponent {
showList() {
arr.map(i => {
console.log(i);
});
}
render() {
console.log(2);
}
};
};
class ToDoComponent {
render() {
console.log(this.props);
}
}"
`;

exports[`method.js 1`] = `
"function logAll() {
console.assert(1 === 1);
console.clear();
console.count(\\"count\\");
console.debug(\\"debug\\");
console.dir(obj);
console.error(\\"error\\");
console.exception(\\"exception\\");
console.group(\\"group\\");
console.groupEnd(\\"group\\");
console.groupCollapsed(\\"groupCollapsed\\");
console.groupEnd(\\"groupCollapsed\\");
console.info(\\"info\\");
console.log(\\"log\\");
console.profile(\\"profile\\");
console.profileEnd(\\"profile\\");
console.table([\\"table\\"]);
console.time(\\"time\\");
console.timeEnd(\\"time\\");
console.trace(\\"trace\\");
console.warn(\\"warn\\");
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function logAll() {
console.assert(1 === 1);
console.clear();
console.count(\\"count\\");
console.debug(\\"method.js(5:2)\\", \\"logAll:\\", \\"debug\\");
console.dir(obj);
console.error(\\"method.js(7:2)\\", \\"logAll:\\", \\"error\\");
console.exception(\\"method.js(8:2)\\", \\"logAll:\\", \\"exception\\");
console.group(\\"group\\");
console.groupEnd(\\"group\\");
console.groupCollapsed(\\"groupCollapsed\\");
console.groupEnd(\\"groupCollapsed\\");
console.info(\\"method.js(13:2)\\", \\"logAll:\\", \\"info\\");
console.log(\\"method.js(14:2)\\", \\"logAll:\\", \\"log\\");
console.profile(\\"profile\\");
console.profileEnd(\\"profile\\");
console.table([\\"table\\"]);
console.time(\\"time\\");
console.timeEnd(\\"time\\");
console.trace(\\"trace\\");
console.warn(\\"method.js(21:2)\\", \\"logAll:\\", \\"warn\\");
}"
`;

exports[`simple.js 1`] = `
"function add(a, b) {
console.log(a, b);
return a + b;
}
const subtract = (a, b) => {
console.info(a, b);
return a - b;
};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function add(a, b) {
console.log(\\"simple.js(2:2)\\", \\"add:\\", \\"a\\", a, \\"b\\", b);
return a + b;
}
const subtract = (a, b) => {
console.info(\\"simple.js(7:2)\\", \\"subtract:\\", \\"a\\", a, \\"b\\", b);
return a - b;
};"
`;
5 changes: 5 additions & 0 deletions src/__test__/ignoreRegEx.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import runSpec from "./run_spec";

runSpec("customMethods - log only", {
ignorePatterns: ['node_modules', 'all.js'],
});
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export default function({ types: t }) {
return {
name,
visitor: {
Identifier(path, { opts }) {
Identifier(path, { opts = {}, file }) {
if (matchesIgnorePattern(opts.ignorePatterns, file)) {
return;
}
if (!looksLike(path.node, { name: "console" })) {
return;
}
Expand Down Expand Up @@ -72,6 +75,9 @@ export default function({ types: t }) {
},
},
};
function matchesIgnorePattern(ignorePatterns = ["node_modules"], file) {
return ignorePatterns.some(pattern => file.opts.filename.includes(pattern));
}

function getConsoleCallMethodName(callExpression) {
return callExpression.get("callee.property").node.name;
Expand All @@ -95,7 +101,7 @@ export default function({ types: t }) {
}

function buildSettings(opts) {
const { methods, ...flags } = opts;
const { methods, ignorePatterns, ...flags } = opts;
// output spreads the flags over each method
// in the future this could be expanded to allow method level config
return (methods || defaultMethods).reduce((acc, curr) => {
Expand Down

0 comments on commit 66b3ff8

Please sign in to comment.