Skip to content

Commit ed5bcc7

Browse files
Turbo87NullVoxPopuli
authored andcommitted
Add debug logging (#83)
1 parent 352efa8 commit ed5bcc7

4 files changed

Lines changed: 68 additions & 12 deletions

File tree

bin/cli.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
#!/usr/bin/env node
22
'use strict';
33

4-
const { gatherTelemetryForUrl, analyzeEmberObject } = require('ember-codemods-telemetry-helpers');
4+
const debug = require('debug')('ember-no-implicit-this-codemod');
5+
const {
6+
gatherTelemetryForUrl,
7+
analyzeEmberObject,
8+
getTelemetry,
9+
} = require('ember-codemods-telemetry-helpers');
510
const appLocation = process.argv[2];
611
const args = process.argv.slice(3);
712

813
(async () => {
14+
debug('Gathering telemetry data from %s ...', appLocation);
915
await gatherTelemetryForUrl(appLocation, analyzeEmberObject);
1016

17+
let telemetry = getTelemetry();
18+
debug('Gathered telemetry on %d modules', Object.keys(telemetry).length);
19+
1120
require('codemod-cli').runTransform(__dirname, 'no-implicit-this', args, 'hbs');
1221
})();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
],
3333
"dependencies": {
3434
"codemod-cli": "^2.1.0",
35+
"debug": "^4.1.1",
3536
"ember-codemods-telemetry-helpers": "^1.1.0",
3637
"ember-template-recast": "^3.3.0"
3738
},

transforms/no-implicit-this/helpers/plugin.js

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const debug = require('debug')('ember-no-implicit-this-codemod:plugin');
12
const recast = require('ember-template-recast');
23

34
// everything is copy-pasteable to astexplorer.net.
@@ -47,33 +48,66 @@ function transform(root, options = {}) {
4748

4849
function handlePathExpression(node) {
4950
// skip this.foo
50-
if (node.this) return;
51+
if (node.this) {
52+
debug(`Skipping \`%s\` because it is already prefixed with \`this.\``, node.original);
53+
return;
54+
}
5155

5256
// skip @foo
53-
if (node.data) return;
57+
if (node.data) {
58+
debug(`Skipping \`%s\` because it is already prefixed with \`@\``, node.original);
59+
return;
60+
}
5461

5562
// skip {#foo as |bar|}}{{bar}}{{/foo}}
5663
// skip <Foo as |bar|>{{bar}}</Foo>
5764
let firstPart = node.parts[0];
58-
if (scopedParams.includes(firstPart)) return;
65+
if (scopedParams.includes(firstPart)) {
66+
debug(`Skipping \`%s\` because it is a scoped variable`, node.original);
67+
return;
68+
}
5969

6070
// skip `hasBlock` keyword
61-
if (node.original === 'hasBlock') return;
71+
if (node.original === 'hasBlock') {
72+
debug(`Skipping \`%s\` because it is a keyword`, node.original);
73+
return;
74+
}
6275

6376
// add `this.` prefix
77+
debug(`Transforming \`%s\` to \`this.%s\``, node.original, node.original);
6478
Object.assign(node, b.path(`this.${node.original}`));
6579
}
6680

6781
function isHelper(name) {
68-
return (
69-
KNOWN_HELPERS.includes(name) ||
70-
customHelpers.includes(name) ||
71-
Boolean(helpers.find(path => path.endsWith(name)))
72-
);
82+
if (KNOWN_HELPERS.includes(name)) {
83+
debug(`Skipping \`%s\` because it is a known helper`, name);
84+
return true;
85+
}
86+
87+
if (customHelpers.includes(name)) {
88+
debug(`Skipping \`%s\` because it is a custom configured helper`, name);
89+
return true;
90+
}
91+
92+
let helper = helpers.find(path => path.endsWith(name));
93+
if (helper) {
94+
let message = `Skipping \`%s\` because it appears to be a helper from the telemetry data: %s`;
95+
debug(message, name, helper);
96+
return true;
97+
}
98+
99+
return false;
73100
}
74101

75102
function isComponent(name) {
76-
return Boolean(components.find(path => path.endsWith(name)));
103+
let component = components.find(path => path.endsWith(name));
104+
if (component) {
105+
let message = `Skipping \`%s\` because it appears to be a component from the telemetry data: %s`;
106+
debug(message, name, component);
107+
return true;
108+
}
109+
110+
return false;
77111
}
78112

79113
let inAttrNode = false;
@@ -113,7 +147,11 @@ function transform(root, options = {}) {
113147

114148
// skip ember-holy-futuristic-template-namespacing-batman component/helper invocations
115149
// (see https://github.com/rwjblue/ember-holy-futuristic-template-namespacing-batman)
116-
if (path.original.includes('$') || path.original.includes('::')) return;
150+
if (path.original.includes('$') || path.original.includes('::')) {
151+
let message = `Skipping \`%s\` because it looks like a helper/component invocation from ember-holy-futuristic-template-namespacing-batman`;
152+
debug(message, path.original);
153+
return;
154+
}
117155

118156
// skip helpers
119157
if (isHelper(path.original)) return;

transforms/no-implicit-this/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path');
22
const fs = require('fs');
33

4+
const debug = require('debug')('ember-no-implicit-this-codemod:transform');
45
const recast = require('ember-template-recast');
56
const { getTelemetry } = require('ember-codemods-telemetry-helpers');
67
const transform = require('./helpers/plugin');
@@ -45,11 +46,18 @@ module.exports = function transformer(file /*, api */) {
4546
let options = Object.assign({}, DEFAULT_OPTIONS, getOptions());
4647

4748
if (!['.hbs'].includes(extension.toLowerCase())) {
49+
debug('Skipping %s because it does not match the .hbs file extension', file.path);
50+
4851
// do nothing on non-hbs files
4952
return;
5053
}
5154

55+
debug('Parsing %s ...', file.path);
5256
let root = recast.parse(file.source);
57+
58+
debug('Transforming %s ...', file.path);
5359
transform(root, options);
60+
61+
debug('Generating new content for %s ...', file.path);
5462
return recast.print(root);
5563
};

0 commit comments

Comments
 (0)