diff --git a/__testfixtures__/method-constructor-logparams-access-modifier.input.ts b/__testfixtures__/method-constructor-logparams-access-modifier.input.ts new file mode 100644 index 0000000..d32543d --- /dev/null +++ b/__testfixtures__/method-constructor-logparams-access-modifier.input.ts @@ -0,0 +1,23 @@ +class BaseClass { + + public items:number[]; + + constructor(private paramOne:number, private paramTwo:number) { + this.items = [paramOne, paramTwo]; + } + + mathOperation(): number { + return this.items[0] + this.items[1]; + } +} + +class Subclass extends BaseClass { + + constructor(private paramOne:number, private paramTwo:number) { + super(paramOne, paramTwo); + } + + mathOperation(): number { + return this.items[0] * this.items[1]; + } +} diff --git a/__testfixtures__/method-constructor-logparams-access-modifier.output.ts b/__testfixtures__/method-constructor-logparams-access-modifier.output.ts new file mode 100644 index 0000000..bf0a4eb --- /dev/null +++ b/__testfixtures__/method-constructor-logparams-access-modifier.output.ts @@ -0,0 +1,61 @@ +class BaseClass { + + public items:number[]; + + constructor(private paramOne:number, private paramTwo:number) { + console.log( + "[logitall] __testfixtures__/method-constructor-logparams-access-modifier.input.ts:5:constructor()" + ); + + console.log( + `[logitall] __testfixtures__/method-constructor-logparams-access-modifier.input.ts:5::param paramOne value: + ${JSON.stringify(paramOne)}` + ); + + console.log( + `[logitall] __testfixtures__/method-constructor-logparams-access-modifier.input.ts:5::param paramTwo value: + ${JSON.stringify(paramTwo)}` + ); + + console.log( + "[logitall] __testfixtures__/method-constructor-logparams-access-modifier.input.ts:6" + ); + + this.items = [paramOne, paramTwo]; + } + + mathOperation(): number { + console.log( + "[logitall] __testfixtures__/method-constructor-logparams-access-modifier.input.ts:9:mathOperation()" + ); + + console.log( + "[logitall] __testfixtures__/method-constructor-logparams-access-modifier.input.ts:10" + ); + + return this.items[0] + this.items[1]; + } +} + +class Subclass extends BaseClass { + + constructor(private paramOne:number, private paramTwo:number) { + super(paramOne, paramTwo); + + console.log( + "[logitall] __testfixtures__/method-constructor-logparams-access-modifier.input.ts:16:constructor()" + ); + } + + mathOperation(): number { + console.log( + "[logitall] __testfixtures__/method-constructor-logparams-access-modifier.input.ts:20:mathOperation()" + ); + + console.log( + "[logitall] __testfixtures__/method-constructor-logparams-access-modifier.input.ts:21" + ); + + return this.items[0] * this.items[1]; + } +} diff --git a/__tests__/transform-test.js b/__tests__/transform-test.js index f9619e7..de000ce 100644 --- a/__tests__/transform-test.js +++ b/__tests__/transform-test.js @@ -11,6 +11,7 @@ defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__', 'para defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__'}, 'function-return', { 'parser': 'ts' }); defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__'}, 'method-constructor', { 'parser': 'ts' }); defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__', 'params': true }, 'method-constructor-logparams', { 'parser': 'ts' }); +defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__', 'params': true }, 'method-constructor-logparams-access-modifier', { 'parser': 'ts' }); defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__'}, 'method-instance', { 'parser': 'ts' }); defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__', 'params' : true}, 'method-instance-logparams', { 'parser': 'ts' }); defineTest(__dirname + '../', 'transform', {'relpath': '__testfixtures__'}, 'method-static', { 'parser': 'ts' }); diff --git a/utils.js b/utils.js index 2e5e81e..62c2dde 100644 --- a/utils.js +++ b/utils.js @@ -6,6 +6,7 @@ const j = require('jscodeshift'); const LIA_PREFIX = '[logitall] '; const LIA_SUFFIX = ''; + var describe = require('jscodeshift-helper').describe; /** * @name buildConsoleLogExpressionStatement @@ -61,6 +62,16 @@ const buildParamLoggingList = (paramNodes, relPathToFile, linenum, functionName) // e.g. let currentNodeName = _.get(currentNode, 'left.name', currentNode.name); + // Fix for situation where Babel does something weird and parses ts constructor function + // parameters as TSParameterProperty node if there's an access modifier (private, public, etc) + + // Babel turn sconstructor(myVar:number) parameter node into an Identifier node + // Baben turns consturctor(private myVar:number) parameter node into a TSParameterProperty node + if (!currentNodeName && currentNode.type === 'TSParameterProperty') { + // The solution is to get TSParameterProperty.paramter.name + currentNodeName = currentNode.parameter.name; + } + let announcement = `${LIA_PREFIX}\t${relPathToFile}:${linenum}${functionName}:param ${currentNodeName} value:${LIA_SUFFIX} \n\t\t\t\t\t\t\t\t\t\t`; let quasis = [