Skip to content

Commit 690d696

Browse files
Merge pull request #12 from patocallaghan/patoc/conservative-mode
Added `--conservative-mode` config option
2 parents a7fe059 + 95198c2 commit 690d696

8 files changed

Lines changed: 34 additions & 10 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ yarn global add ember-no-implicit-this-codemod
1616
ember-no-implicit-this-codemod <TRANSFORM NAME> path/of/files/ or/some**/*glob.js
1717
```
1818

19+
The codemod accepts the following options:
20+
21+
| Option | Value | Default | Details |
22+
| --- | --- | ---| --- |
23+
| --dont-assume-this | boolean | false | The codemod won't prepend `this.` to `PathExpressions` names which don't appear in a component's telemetry data |
24+
1925
## Transforms
2026

2127
<!--TRANSFORMS_START-->

transforms/no-implicit-this/__testfixtures__/-mock-telemetry.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"type": "Component",
2424
"computedProperties": ["foo"]
2525
},
26+
"dont-assume-this.input": {
27+
"type": "Component",
28+
"computedProperties": ["foo"]
29+
},
2630
"handlebars-with-positional-params.input": {
2731
"type": "Component",
2832
"computedProperties": ["foo", "property"],
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{{foo}}
2+
{{bar}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"dontAssumeThis": true
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{{this.foo}}
2+
{{bar}}

transforms/no-implicit-this/helpers/determine-this-usage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const transformPlugin = require('./plugin');
99
*
1010
* @param {*} ast
1111
*/
12-
function determineThisUsage(ast, file) {
12+
function determineThisUsage(ast, file, options) {
1313
let { path: filePath } = file;
1414
let runtimeData = getTelemetryFor(path.resolve(filePath));
1515

@@ -18,7 +18,7 @@ function determineThisUsage(ast, file) {
1818
return;
1919
}
2020

21-
recast.transform(ast, env => transformPlugin(env, runtimeData));
21+
recast.transform(ast, env => transformPlugin(env, runtimeData, options));
2222

2323
return ast;
2424
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const ARGLESS_BUILTINS = [
1515
/**
1616
* plugin entrypoint
1717
*/
18-
function transformPlugin(env, runtimeData) {
18+
function transformPlugin(env, runtimeData, options) {
1919
let { builders: b } = env.syntax;
2020

2121
let scopedParams = [];
@@ -44,7 +44,7 @@ function transformPlugin(env, runtimeData) {
4444
let token = ast.parts[0];
4545

4646
if (token !== 'this') {
47-
let isThisNeeded = doesTokenNeedThis(token, nonThises, runtimeData);
47+
let isThisNeeded = doesTokenNeedThis(token, nonThises, runtimeData, options);
4848

4949
if (isThisNeeded) {
5050
return b.path(`this.${ast.parts.join('.')}`);
@@ -64,7 +64,12 @@ function transformPlugin(env, runtimeData) {
6464
// - no:
6565
// - is-helper: false
6666
// - is-component: false
67-
function doesTokenNeedThis(token, { components, helpers, scopedParams }, runtimeData) {
67+
function doesTokenNeedThis(
68+
token,
69+
{ components, helpers, scopedParams },
70+
runtimeData,
71+
{ dontAssumeThis }
72+
) {
6873
if (ARGLESS_BUILTINS.includes(token)) {
6974
return false;
7075
}
@@ -98,7 +103,7 @@ function doesTokenNeedThis(token, { components, helpers, scopedParams }, runtime
98103
return false;
99104
}
100105

101-
return true;
106+
return dontAssumeThis ? false : true;
102107
}
103108

104109
function populateInvokeables() {

transforms/no-implicit-this/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ const path = require('path');
22

33
// const { getParser } = require('codemod-cli').jscodeshift;
44
const { parse: parseHbs, print: printHbs } = require('ember-template-recast');
5-
65
const { determineThisUsage } = require('./helpers/determine-this-usage');
7-
8-
// const { getOptions } = require('codemod-cli');
6+
const { getOptions } = require('codemod-cli');
7+
const DEFAULT_OPTIONS = {
8+
dontAssumeThis: false,
9+
};
910

1011
module.exports = function transformer(file /*, api */) {
1112
let extension = path.extname(file.path);
13+
const options = Object.assign({}, DEFAULT_OPTIONS, getOptions());
1214

1315
if (!['.hbs'].includes(extension.toLowerCase())) {
1416
// do nothing on non-hbs files
@@ -17,7 +19,7 @@ module.exports = function transformer(file /*, api */) {
1719

1820
let root = parseHbs(file.source);
1921

20-
let replaced = determineThisUsage(root, file);
22+
let replaced = determineThisUsage(root, file, options);
2123

2224
if (replaced) {
2325
return printHbs(replaced);

0 commit comments

Comments
 (0)