Skip to content

Commit

Permalink
Fix for probleem caused by how babel interprets TS access modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
ElonVolo committed Feb 3, 2021
1 parent 3bd06fd commit 828aa35
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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];
}
}
Original file line number Diff line number Diff line change
@@ -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];
}
}
1 change: 1 addition & 0 deletions __tests__/transform-test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const j = require('jscodeshift');
const LIA_PREFIX = '[logitall] ';
const LIA_SUFFIX = '';

var describe = require('jscodeshift-helper').describe;

/**
* @name buildConsoleLogExpressionStatement
Expand Down Expand Up @@ -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 = [
Expand Down

0 comments on commit 828aa35

Please sign in to comment.