Skip to content

Commit

Permalink
Merge pull request #515 from lolmaus/opt-in-for-unambiguous-helpers
Browse files Browse the repository at this point in the history
Move helper disambiguation to a flag that is off by default
  • Loading branch information
mansona committed Feb 14, 2024
2 parents 5b29cde + ab2a93c commit a715623
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
root: true,
parserOptions: {
ecmaVersion: 2017,
ecmaVersion: 'latest',
},
env: {
node: true,
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ If you would like to only convert certain component invocations to use the angle
}
```

### Making helper invocations unambiguous

You may want to convert invocations like `{{concat "foo" "bar"}}` into `{{(concat "foo" "bar")}}`, which may be useful as a temporary step when upgrading to strict-mode Embroider.

In your **config/anglebrackets-codemod-config.json**, add this:

```js
{
"unambiguousHelpers": true
}
```

Note that unambiguous helpers do not work in non-Embroider Ember, as of January 2024.

Note that ambiguous invocations that cannot be statically distinguished between a helper, a property and a component — will not be modified.

## Debugging Workflow

Oftentimes, you want to debug the codemod or the transform to identify issues with the code or to understand
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"yargs": "^17.7.2"
},
"devDependencies": {
"compare-fixture": "^1.1.0",
"coveralls": "^3.1.1",
"eslint": "^8.22.0",
"eslint-config-prettier": "^8.5.0",
Expand Down
41 changes: 38 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 1 addition & 20 deletions test/fixtures/with-telemetry/input/app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,8 @@
{{/bs-nav}}
</div>
<div class="col-sm-8 col-sm-pull-4 col-md-9 col-md-pull-3">
{{utils/bee-bop}}
{{-wat-wat}}
{{utils/-wat-wat}}
{{#if this.isDetailPage}}
<h1>
{{currentComponent.title}}
</h1>
<p class="lead">
{{currentComponent.description}}
</p>
{{api-reference component=this.currentComponent}}
{{/if}}
{{outlet}}

{{#bs-button id="openModal" onClick=(action this.addModal)}}Open{{/bs-button}}

{{#if hasModal}}
{{#bs-modal-simple open=modal onHidden=(action "removeModal") title="Dynamic Dialog"}}
Hi there
{{/bs-modal-simple}}
{{/if}}
{{outlet}}
{{file-less foo=true}}
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<div>this template has no js </div>
{{#bs-button type="primary"}}Primary{{/bs-button}}
23 changes: 2 additions & 21 deletions test/fixtures/with-telemetry/output/app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,8 @@
</BsNav>
</div>
<div class="col-sm-8 col-sm-pull-4 col-md-9 col-md-pull-3">
{{(utils/bee-bop)}}
{{(-wat-wat)}}
{{(utils/-wat-wat)}}
{{#if this.isDetailPage}}
<h1>
{{currentComponent.title}}
</h1>
<p class="lead">
{{currentComponent.description}}
</p>
<ApiReference @component={{this.currentComponent}} />
{{/if}}
{{outlet}}

<BsButton @id="openModal" @onClick={{action this.addModal}}>Open</BsButton>

{{#if hasModal}}
<BsModalSimple @open={{modal}} @onHidden={{action "removeModal"}} @title="Dynamic Dialog">
Hi there
</BsModalSimple>
{{/if}}
{{-wat-wat}}
{{outlet}}
<FileLess @foo={{true}} />
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<div>this template has no js </div>
<BsButton @type="primary">Primary</BsButton>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<button>
{{(titleize "string helpers for ember")}}
{{titleize "string helpers for ember"}}
</button>
{{(biz-baz canConvert="no" why="helper" where="local")}}
{{biz-baz canConvert="no" why="helper" where="local"}}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{{site-header user=this.user class=(if this.user.isAdmin "admin")}}
<SiteHeader @user={{this.user}} @class={{if this.user.isAdmin "admin"}} />

{{#super-select selected=this.user.country as |s|}}
<SuperSelect @selected={{this.user.country}} as |s|>
{{#each this.availableCountries as |country|}}
{{#s.option value=country}}{{country.name}}{{/s.option}}
<s.option @value={{country}}>{{country.name}}</s.option>
{{/each}}
{{/super-select}}
</SuperSelect>

{{ui/button text="Click me"}}
<Ui::Button @text="Click me" />
3 changes: 2 additions & 1 deletion test/run-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const execOpts = { cwd: inputDir, stderr: 'inherit' };
console.log('comparing results');

try {
await execa('diff', ['-rq', './app', '../output/app'], execOpts);
const compareFixture = await import('compare-fixture');
compareFixture.default(path.join(inputDir, 'app'), path.join(inputDir, '../output/app'));
} catch (e) {
console.error('codemod did not run successfully');
console.log(e);
Expand Down
1 change: 1 addition & 0 deletions transforms/angle-brackets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function getOptions() {

options.includeValuelessDataTestAttributes = !!config.includeValuelessDataTestAttributes;
options.skipBuiltInComponents = !!config.skipBuiltInComponents;
options.unambiguousHelpers = !!config.unambiguousHelpers;
}

if (cliOptions.telemetry) {
Expand Down
13 changes: 12 additions & 1 deletion transforms/angle-brackets/telemetry/invokable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@ const HELPER = 'Helper';
const COMPONENT = 'Component';

function invokableName(name, type) {
let invokePath = type === HELPER ? '/helpers/' : '/components/';
let invokePath;

if (name.startsWith('@ember/component/')) {
invokePath = '@ember/component/';
} else if (name.startsWith('@ember/routing/')) {
invokePath = '@ember/routing/';
} else if (type === HELPER) {
invokePath = '/helpers/';
} else {
invokePath = '/components/';
}

return name.substring(name.lastIndexOf(invokePath) + invokePath.length, name.length);
}

Expand Down
29 changes: 19 additions & 10 deletions transforms/angle-brackets/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,26 @@ function isKnownHelper(fullName, config, invokableData) {
}

if (isTelemetryData) {
let isComponent =
!config.helpers.includes(name) &&
[...(components || []), ...BUILT_IN_COMPONENTS].includes(name);
if (config.unambiguousHelpers) {
let isComponent =
!config.helpers.includes(name) &&
[...(components || []), ...BUILT_IN_COMPONENTS].includes(name);

if (isComponent) {
return false;
}
if (isComponent) {
return false;
}

let mergedHelpers = [...KNOWN_HELPERS, ...(helpers || [])];
let isHelper = mergedHelpers.includes(name) || config.helpers.includes(name);
let strName = `${name}`; // coerce boolean and number to string
return isHelper && !strName.includes('.');
let mergedHelpers = [...KNOWN_HELPERS, ...(helpers || [])];
let isHelper = mergedHelpers.includes(name) || config.helpers.includes(name);
let strName = `${name}`; // coerce boolean and number to string
return isHelper && !strName.includes('.');
} else {
let mergedHelpers = [...KNOWN_HELPERS, ...(helpers || [])];
let isHelper = mergedHelpers.includes(name) || config.helpers.includes(name);
let isComponent = [...(components || []), ...BUILT_IN_COMPONENTS].includes(name);
let strName = `${name}`; // coerce boolean and number to string
return (isHelper || !isComponent) && !strName.includes('.');
}
} else {
return KNOWN_HELPERS.includes(name) || config.helpers.includes(name);
}
Expand Down Expand Up @@ -489,6 +497,7 @@ function transformToAngleBracket(fileInfo, config, invokableData) {
) {
return transformComponentNode(node, fileInfo, config);
} else if (
config.unambiguousHelpers &&
isTagKnownHelper &&
node.path.type !== 'SubExpression' &&
walkerPath.parent.node.type !== 'AttrNode' &&
Expand Down
Loading

0 comments on commit a715623

Please sign in to comment.