diff --git a/README.md b/README.md index 8b1623a..bd748c0 100644 --- a/README.md +++ b/README.md @@ -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"] }] ] } @@ -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 }] ] } @@ -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 }] ] } @@ -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 }] ] } diff --git a/src/__test__/__snapshots__/ignoreRegEx.spec.js.snap b/src/__test__/__snapshots__/ignoreRegEx.spec.js.snap new file mode 100644 index 0000000..1ebce07 --- /dev/null +++ b/src/__test__/__snapshots__/ignoreRegEx.spec.js.snap @@ -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; +};" +`; diff --git a/src/__test__/ignoreRegEx.spec.js b/src/__test__/ignoreRegEx.spec.js new file mode 100644 index 0000000..631d0b8 --- /dev/null +++ b/src/__test__/ignoreRegEx.spec.js @@ -0,0 +1,5 @@ +import runSpec from "./run_spec"; + +runSpec("customMethods - log only", { + ignorePatterns: ['node_modules', 'all.js'], +}); diff --git a/src/index.js b/src/index.js index 12f76dc..b7a2b44 100644 --- a/src/index.js +++ b/src/index.js @@ -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; } @@ -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; @@ -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) => {