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 printing embedded handlebars in javascript files #8647

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

dcyriller
Copy link
Collaborator

@dcyriller dcyriller commented Jun 26, 2020

Description

hbs usages

hbs is provided by the ember-cli-htmlbars library.

The first usage is in tests, where components are rendered:

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

await render(hbs`
  <MyComponent />
`);

A second usage can be with Storybook:

import { text } from '@storybook/addon-knobs';

import hbs from 'htmlbars-inline-precompile';

export default {
  title: 'Components/Checkbox',
};

export const checkbox = () => {
  return {
    template: hbs`<Checkbox @label={{this.label}} {{on "click" this.toggleCheckbox}} />`,
    context: {
      label: text('title', 'Welcome to storybook'),
      toggleCheckbox() {},
    },
  };
};

checkbox.story = {
  name: '...',
};

technical details

javascript's TaggedTemplateExpressions with the tag hbs will be printed using glimmer printer.

credit

This implementation mimicks html's.

It is adapted in two ways:

  • hbs doesn't support printing <script> tags
  • hbs doesn't support interpolation with ${}

Links / Related

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory)
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/pr-XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

src/language-js/embed.js Outdated Show resolved Hide resolved
@fisker
Copy link
Member

fisker commented Jun 28, 2020

Prettier pr-8647
Playground link

--parser babel

Input:

hbs`\``;
html`\``;

Output:

hbs```;
html`\``;

@fisker
Copy link
Member

fisker commented Jun 28, 2020

Prettier pr-8647
Playground link

--parser babel

Input:

hbs`${a}`;
html`${a}`;

Output:

hbs``;
html`${a}`;

@dcyriller

This comment has been minimized.

Copy link
Member

@thorn0 thorn0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please see this comment: #5993 (comment)

@dcyriller

This comment has been minimized.

@dcyriller

This comment has been minimized.

@thorn0
Copy link
Member

thorn0 commented Aug 23, 2020

@dcyriller "The idea is to design a way to use this option to specify the correspondence between template tags, special comments, etc. and embedded syntaxes whereas the default behavior won't be extended anymore." This PR extends the defautl behavior.

**technical details**
javascript's `TaggedTemplateExpression`s with the tag `hbs` will be
printed using glimmer printer.

**credit**
The implementation is taken from html's.

It is adapted in two ways:
- `hbs` doesn't support printing <script> tags
- `hbs` doesn't support interpolation with ${}

**usages**
`hbs` is provided by the [ember-cli-htmlbars
library](https://github.com/ember-cli/ember-cli-htmlbars).

The first usage is in tests, where components are rendered:
```js
import { hbs } from 'ember-cli-htmlbars';

await render(hbs`
  <MyComponent />
`);
```

A second usage can be with Storybook:
```js
import { text } from '@storybook/addon-knobs';

import hbs from 'htmlbars-inline-precompile';

export default {
  title: 'Components/Checkbox',
};

export const checkbox = () => {
  return {
    template: hbs`<Checkbox @Label={{this.label}} {{on "click" this.toggleCheckbox}} />`,
    context: {
      label: text('title', 'Welcome to storybook'),
      toggleCheckbox() {},
    },
  };
};

checkbox.story = {
  name: '...',
};
```
To have a consistent implementation with other embed functions. Indeed,
a PR is proposing to strip all the finnl new lines in textToDoc
function.
@dcyriller dcyriller changed the title Support printing handlebars in javascript files though hbs Support printing TaggedTemplateExpression hbs Aug 24, 2020
@dcyriller dcyriller changed the title Support printing TaggedTemplateExpression hbs Support printing embedded handlebars in javascript files Aug 24, 2020
@dcyriller
Copy link
Collaborator Author

dcyriller commented Aug 24, 2020

@thorn0 I read carefully the linked PR and the various issues. I understand well the problem with formatting embedded snippets and that the 1st goal of Prettier is correctness.

So I've narrowed this implementation to only format:

hbs`some {{handlebars content}} here`

Previously it would also format:

const code = /* hbs */ `some {{handlebars content}} here`;

If an user selects the option --embedded-language-formatting=off, the embedded snippet won't get formatted. If --embedded-language-formatting=auto, it will. (The tests show it)

I'm not sure what else I should do. 🤔

@thorn0
Copy link
Member

thorn0 commented Aug 27, 2020

What I'm saying is that this PR (as well as #5993 and #9025) is blocked by #5588. We won't move forward with it until we solve that problem.

Base automatically changed from master to main January 23, 2021 17:13
@aogaili
Copy link

aogaili commented Dec 8, 2022

Any reason why this change is not being merged? If #5588 then why not moving that as well?

We're using ember in a large project and would also like to introduce prettier and this is the only stopper.

@Alonski
Copy link
Contributor

Alonski commented Feb 19, 2024

@dcyriller 👋 Do you still work on this stuff? Any idea if this can be pushed forward somehow? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants