Skip to content

Commit 9713041

Browse files
Merge pull request #50 from suchitadoshi1987/suchita/customHelpers
Add config option to support custom helpers
2 parents 57d0a65 + c1c4918 commit 9713041

7 files changed

Lines changed: 48 additions & 3 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"helpers": ["biz", "bang"]
3+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"computedProperties": ["foo", "records"],
1111
"ownActions": ["myAction"]
1212
},
13+
"custom-helpers": { "type": "Component" },
1314
"angle-brackets-with-block-params": {
1415
"type": "Component",
1516
"computedProperties": ["foo", "property", "bar"],
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{{biz}}
2+
{{bang}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"config": "./transforms/no-implicit-this/__testfixtures__/-helpers.json"
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{{biz}}
2+
{{bang}}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ function doesTokenNeedThis(
6363
token,
6464
{ components, helpers, scopedParams },
6565
runtimeData,
66-
{ dontAssumeThis }
66+
{ dontAssumeThis, customHelpers }
6767
) {
68-
if (KNOWN_HELPERS.includes(token)) {
68+
if (KNOWN_HELPERS.includes(token) || customHelpers.includes(token)) {
6969
return false;
7070
}
7171

transforms/no-implicit-this/index.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,46 @@
11
const path = require('path');
2+
const fs = require('fs');
23

34
const { parse: parseHbs, print: printHbs } = require('ember-template-recast');
45
const { determineThisUsage } = require('./helpers/determine-this-usage');
5-
const { getOptions } = require('codemod-cli');
6+
const { getOptions: getCLIOptions } = require('codemod-cli');
67
const DEFAULT_OPTIONS = {
78
dontAssumeThis: false,
89
};
910

11+
/**
12+
* Accepts the config path for custom helpers and returns the array of helpers
13+
* if the file path is resolved.
14+
* Context: This will allow the users to specify their custom list of helpers
15+
* along with the known helpers, this would give them more flexibility for handling
16+
* special usecases.
17+
* @param {string} configPath
18+
*/
19+
function _getCustomHelpersFromConfig(configPath) {
20+
let customHelpers = [];
21+
if (configPath) {
22+
let filePath = path.join(process.cwd(), configPath);
23+
let config = JSON.parse(fs.readFileSync(filePath));
24+
if (config.helpers) {
25+
customHelpers = config.helpers;
26+
}
27+
}
28+
return customHelpers;
29+
}
30+
31+
/**
32+
* Returns custom options object to support the custom helpers config path passed
33+
* by the user.
34+
*/
35+
function getOptions() {
36+
let cliOptions = getCLIOptions();
37+
let options = {
38+
dontAssumeThis: cliOptions.dontAssumeThis,
39+
customHelpers: _getCustomHelpersFromConfig(cliOptions.config),
40+
};
41+
return options;
42+
}
43+
1044
module.exports = function transformer(file /*, api */) {
1145
let extension = path.extname(file.path);
1246
let options = Object.assign({}, DEFAULT_OPTIONS, getOptions());

0 commit comments

Comments
 (0)