Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for running on templates in JS files #167

Open
Turbo87 opened this issue Oct 12, 2019 · 6 comments
Open

Support for running on templates in JS files #167

Turbo87 opened this issue Oct 12, 2019 · 6 comments
Labels
enhancement New feature or request

Comments

@Turbo87
Copy link
Collaborator

Turbo87 commented Oct 12, 2019

Example:

await render(hbs`{{
  file-preview/slider
  files=files
  selectedFileIndex=selectedFileIndex
}}`);
@Turbo87 Turbo87 added the enhancement New feature or request label Oct 12, 2019
@rwjblue
Copy link
Member

rwjblue commented Oct 12, 2019

Should support the following import locations:

import hbs from 'htmlbars-inline-precompile';
import hbs from 'ember-cli-htmlbars-inline-precompile';
import { hbs } from 'ember-cli-htmlbars';

(Note that the actual local name is completely up to the app, and does not need to be hbs).

Should also support the following call signatures of hbs:

hbs`{{stuff-here blah="derp"}}`;

hbs('{{stuff-here blah="derp"}}');
hbs('{{stuff-here blah="derp"}}', { moduleName: 'app-name/templates/foo.hbs' });

@jelhan
Copy link

jelhan commented Jan 10, 2020

I wasn't aware that this is formally not supported. Running the codemod with a RegExp that matches JavaScript files works quiet well. If run the codemod on the huge test suite of Ember Bootstrap and it has worked quiet well. This commit is mostly done by the codemod: ember-bootstrap/ember-bootstrap@ae598bc

But there are some problems which causes files to be skipped:

I haven't noticed any wrong conversion. It has either skipped the file or provided correct conversion. In some rare edge cases the conversion was incomplete and missing some yielded blocks.


Update:

All syntax errors were caused by < char in the code. These are two examples: https://github.com/kaliber5/ember-bootstrap/blob/a0e2a2ea1732296f28e964ab2e1b800f7d08597d/tests/integration/components/bs-popover-test.js#L98 https://github.com/kaliber5/ember-bootstrap/blob/a0e2a2ea1732296f28e964ab2e1b800f7d08597d/tests/integration/components/bs-form/element-test.js#L392 Please note that commenting the line out does not have an affect. You need to replace the char while running the codemod.


Update 2:

The A block may only be used inside an HTML element or another block. was caused by a < char in code as well. Temporary replacing this line and another one, which was similar allowed the codemod to run.


Summary

While running the codemod on rendering tests is not official supported it works quite well. It either does it job as expected or fails and skips the file. Such failures seem to be caused by < chars in code. Temporary replacing them allows the codemod to be run. Not the best solution but it was good enough to convert the test suite of Ember Bootstrap.

@lifeart
Copy link

lifeart commented Jan 10, 2020

@flashios09
Copy link

https://github.com/flashios09/ember-extract-inline-templates

// @flashios09

hi @lifeart , i will update the code and publish it this weekend :)

@acknudson
Copy link

I haven't been able to get this to work in our test files, but I would love for that to be possible!

@ghost
Copy link

ghost commented Jan 29, 2020

@ajcolter I believe you need to specify the .js extension explicitly. I had to run:

npx ember-angle-brackets-codemod http://localhost:4200 tests/integration/components/*.js

I saw a few parsing errors that I will open separate issues for. But to confirm @jelhan's findings, the migration worked for the majority of cases.

backspace added a commit to hashicorp/nomad that referenced this issue Jul 7, 2020
I found some of these stragglers with this regular expression:
\{\{#((?!if)(?!unless)(?!each)(?!with)(?!let)(?!link-to)[\s\S])+

I found more with this, but it also finds expected substitutions
used in templates, so it means combing through a lot of false
positives and I may have missed some invocations:
[^=]\{\{#?((?!if)(?!unless)(?!each)(?!with)(?!let)(?!link-to)(?!\/if)(?!else)(?!\/each)[\s\S])+

The ListPagination test was missed by the codemod due to parsing
errors caused by the presence of < and > characters elsewhere
in the file, as described by @jelhan here:
ember-codemods/ember-angle-brackets-codemod#167 (comment)

I thought Toggle was missed because it’s part of
ember-composable-helpers, but when I removed that and ran the
codemod again, it only converted the integration test and
ignored the two templates where it’s used. Adding toggle to
a disallowed list in .template-lintrc.js doesn’t produce
errors for those invocations 😞
backspace added a commit to hashicorp/nomad that referenced this issue Jul 9, 2020
I found some of these stragglers with this regular expression:
\{\{#((?!if)(?!unless)(?!each)(?!with)(?!let)(?!link-to)[\s\S])+

I found more with this, but it also finds expected substitutions
used in templates, so it means combing through a lot of false
positives and I may have missed some invocations:
[^=]\{\{#?((?!if)(?!unless)(?!each)(?!with)(?!let)(?!link-to)(?!\/if)(?!else)(?!\/each)[\s\S])+

The ListPagination test was missed by the codemod due to parsing
errors caused by the presence of < and > characters elsewhere
in the file, as described by @jelhan here:
ember-codemods/ember-angle-brackets-codemod#167 (comment)

I thought Toggle was missed because it’s part of
ember-composable-helpers, but when I removed that and ran the
codemod again, it only converted the integration test and
ignored the two templates where it’s used. Adding toggle to
a disallowed list in .template-lintrc.js doesn’t produce
errors for those invocations 😞
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants