|
| 1 | +const debug = require('debug')('ember-no-implicit-this-codemod:plugin'); |
1 | 2 | const recast = require('ember-template-recast'); |
2 | 3 |
|
3 | 4 | // everything is copy-pasteable to astexplorer.net. |
@@ -47,33 +48,66 @@ function transform(root, options = {}) { |
47 | 48 |
|
48 | 49 | function handlePathExpression(node) { |
49 | 50 | // 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 | + } |
51 | 55 |
|
52 | 56 | // 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 | + } |
54 | 61 |
|
55 | 62 | // skip {#foo as |bar|}}{{bar}}{{/foo}} |
56 | 63 | // skip <Foo as |bar|>{{bar}}</Foo> |
57 | 64 | 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 | + } |
59 | 69 |
|
60 | 70 | // 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 | + } |
62 | 75 |
|
63 | 76 | // add `this.` prefix |
| 77 | + debug(`Transforming \`%s\` to \`this.%s\``, node.original, node.original); |
64 | 78 | Object.assign(node, b.path(`this.${node.original}`)); |
65 | 79 | } |
66 | 80 |
|
67 | 81 | 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; |
73 | 100 | } |
74 | 101 |
|
75 | 102 | 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; |
77 | 111 | } |
78 | 112 |
|
79 | 113 | let inAttrNode = false; |
@@ -113,7 +147,11 @@ function transform(root, options = {}) { |
113 | 147 |
|
114 | 148 | // skip ember-holy-futuristic-template-namespacing-batman component/helper invocations |
115 | 149 | // (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 | + } |
117 | 155 |
|
118 | 156 | // skip helpers |
119 | 157 | if (isHelper(path.original)) return; |
|
0 commit comments