Skip to content

Commit

Permalink
Merge pull request #50 from suchitadoshi1987/suchita/customHelpers
Browse files Browse the repository at this point in the history
Add config option to support custom helpers
  • Loading branch information
NullVoxPopuli authored Dec 11, 2019
2 parents 57d0a65 + c1c4918 commit 9713041
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 3 deletions.
3 changes: 3 additions & 0 deletions transforms/no-implicit-this/__testfixtures__/-helpers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"helpers": ["biz", "bang"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"computedProperties": ["foo", "records"],
"ownActions": ["myAction"]
},
"custom-helpers": { "type": "Component" },
"angle-brackets-with-block-params": {
"type": "Component",
"computedProperties": ["foo", "property", "bar"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{biz}}
{{bang}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"config": "./transforms/no-implicit-this/__testfixtures__/-helpers.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{biz}}
{{bang}}
4 changes: 2 additions & 2 deletions transforms/no-implicit-this/helpers/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ function doesTokenNeedThis(
token,
{ components, helpers, scopedParams },
runtimeData,
{ dontAssumeThis }
{ dontAssumeThis, customHelpers }
) {
if (KNOWN_HELPERS.includes(token)) {
if (KNOWN_HELPERS.includes(token) || customHelpers.includes(token)) {
return false;
}

Expand Down
36 changes: 35 additions & 1 deletion transforms/no-implicit-this/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
const path = require('path');
const fs = require('fs');

const { parse: parseHbs, print: printHbs } = require('ember-template-recast');
const { determineThisUsage } = require('./helpers/determine-this-usage');
const { getOptions } = require('codemod-cli');
const { getOptions: getCLIOptions } = require('codemod-cli');
const DEFAULT_OPTIONS = {
dontAssumeThis: false,
};

/**
* Accepts the config path for custom helpers and returns the array of helpers
* if the file path is resolved.
* Context: This will allow the users to specify their custom list of helpers
* along with the known helpers, this would give them more flexibility for handling
* special usecases.
* @param {string} configPath
*/
function _getCustomHelpersFromConfig(configPath) {
let customHelpers = [];
if (configPath) {
let filePath = path.join(process.cwd(), configPath);
let config = JSON.parse(fs.readFileSync(filePath));
if (config.helpers) {
customHelpers = config.helpers;
}
}
return customHelpers;
}

/**
* Returns custom options object to support the custom helpers config path passed
* by the user.
*/
function getOptions() {
let cliOptions = getCLIOptions();
let options = {
dontAssumeThis: cliOptions.dontAssumeThis,
customHelpers: _getCustomHelpersFromConfig(cliOptions.config),
};
return options;
}

module.exports = function transformer(file /*, api */) {
let extension = path.extname(file.path);
let options = Object.assign({}, DEFAULT_OPTIONS, getOptions());
Expand Down

0 comments on commit 9713041

Please sign in to comment.