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

adds skipPrettier and self closing #57

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,49 @@ You can also disable the conversion of the built-in components `{{link-to}}`, `{
}
```

You can also disable the final **Prettier** formatting by adding `skipPrettier` as follows:

**config/anglebrackets-codemod-config.json**

```js
{
"helpers": [],
"skipPrettier": true
Copy link
Collaborator

@GavinJoyce GavinJoyce Jun 25, 2019

Choose a reason for hiding this comment

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

Perhaps, instead of a boolean, it would be better to add a formatter option whose possible values can be prettier (the default) and none? That would allow us the option of adding a new formatter (eg. ember-template-recast or prettier-experimental) in future

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sounds good. I'll implement that.

}
```
By disabling Prettier, oddly formatted `hbs` files will remain (somewhat) unchanged:

#### From
```hbs
{{#bs-button-group
value=buttonGroupValue
type="checkbox"
onChange=(action (mut buttonGroupValue)) as |bg|}}
{{#bg.button value=1}}1{{/bg.button}}
{{#bg.button value=2}}2{{/bg.button}}
{{#bg.button value=3}}3{{/bg.button}}
{{#bg.button value=3}}3{{/bg.button}}
{{/bs-button-group}}
```

#### To
```hbs
<BsButtonGroup @value={{buttonGroupValue}} @type="checkbox" @onChange={{action (mut buttonGroupValue)}} as |bg|>
<bg.button @value={{1}}>1</bg.button>
<bg.button @value={{2}}>2</bg.button>
<bg.button @value={{3}}>3</bg.button>
<bg.button @value={{3}}>3</bg.button>
</BsButtonGroup>
```

You can execute the codemod with custom configuration by specifying a `--config` command line option as follows:

```sh
$ cd my-ember-app-or-addon
$ npx ember-angle-brackets-codemod angle-brackets app/templates --config ./config/anglebrackets-codemod-config.json
```

### Getting a list of helpers
To get a list of helpers in your app you can do this in the Developer Console in your browser inside of your app:
```js
var componentLikeHelpers = Object.keys(require.entries)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"helpers": ["some-helper1", "some-helper2", "some-helper3"],
"skipBuiltInComponents": true
"skipBuiltInComponents": true,
"skipPrettier": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,26 @@
{{link-to "Title" "some.route"}}
{{textarea value=this.model.body}}
{{input type="checkbox" name="email-opt-in" checked=this.model.emailPreference}}

{{site-header user=this.user class=(if this.user.isAdmin "admin")}}
{{#super-select selected=this.user.country as |s|}}
{{#each this.availableCountries as |country|}}
{{#s.option value=country}}{{country.name}}{{/s.option}}
{{/each}}
{{/super-select}}

{{#bs-button-group
value=buttonGroupValue
type="checkbox"
onChange=(action (mut buttonGroupValue)) as |bg|}}
{{#bg.button value=1}}1{{/bg.button}}
{{#bg.button value=2}}2{{/bg.button}}
{{#bg.button value=3}}3{{/bg.button}}
{{#bg.button value=3}}3{{/bg.button}}
{{/bs-button-group}}

someFunction({
{{#unless installationIsAnonymous}}
console.log("hi");
{{/unless}}
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,23 @@
{{link-to "Title" "some.route"}}
{{textarea value=this.model.body}}
{{input type="checkbox" name="email-opt-in" checked=this.model.emailPreference}}

<SiteHeader @user={{this.user}} class={{if this.user.isAdmin "admin"}} />
<SuperSelect @selected={{this.user.country}} as |s|>
{{#each this.availableCountries as |country|}}
<s.option @value={{country}}>{{country.name}}</s.option>
{{/each}}
</SuperSelect>

<BsButtonGroup @value={{buttonGroupValue}} @type="checkbox" @onChange={{action (mut buttonGroupValue)}} as |bg|>
<bg.button @value={{1}}>1</bg.button>
<bg.button @value={{2}}>2</bg.button>
<bg.button @value={{3}}>3</bg.button>
<bg.button @value={{3}}>3</bg.button>
</BsButtonGroup>

someFunction({
{{#unless installationIsAnonymous}}
console.log("hi");
{{/unless}}
});
14 changes: 10 additions & 4 deletions transforms/angle-brackets/transforms/angle-brackets-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Config {
constructor(options) {
this.helpers = [];
this.skipBuiltInComponents = false;
this.skipPrettier = false;

if (options.config) {
let filePath = path.join(process.cwd(), options.config);
Expand All @@ -19,6 +20,7 @@ class Config {
}

this.skipBuiltInComponents = !!config.skipBuiltInComponents;
this.skipPrettier = !!config.skipPrettier;
}
}
}
Expand Down Expand Up @@ -350,8 +352,12 @@ module.exports = function(fileInfo, api, options) {
let attributes;
let children = node.program ? node.program.body : undefined;
let blockParams = node.program ? node.program.blockParams : undefined;
let selfClosing = node.type !== 'BlockStatement';

if(tagName === 'link-to') {
if (tagName === 'link-to') {

selfClosing = false;

if (node.type === 'MustacheStatement') {
let params = node.params;
let textParam = params.shift(); //the first param becomes the block content
Expand All @@ -373,10 +379,10 @@ module.exports = function(fileInfo, api, options) {
attributes = transformNodeAttributes(node);
}

return b.element(newTagName, {
return b.element({ name: newTagName, selfClosing }, {
attrs: attributes,
children,
blockParams
blockParams,
});
};

Expand Down Expand Up @@ -411,5 +417,5 @@ module.exports = function(fileInfo, api, options) {

let uglySource = glimmer.print(ast).replace(attrEqualEmptyString,"");
let dataOk = uglySource.replace(dataEqualsNoValue, "$1");
return prettier.format(dataOk, { parser: "glimmer" });
return config.skipPrettier ? dataOk : prettier.format(dataOk, { parser: "glimmer" });
};