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

Extend babel-template to work as a template tag #5001

Merged
merged 3 commits into from
Oct 4, 2017

Conversation

Jessidhia
Copy link
Member

@Jessidhia Jessidhia commented Dec 15, 2016

Q A
Patch: Bug Fix? no
Major: Breaking Change? no
Minor: New Feature? yes
Deprecations? no
Spec Compliancy? no
Tests Added/Pass? yes
Fixed Tickets
License MIT
Doc PR
Dependency Changes

Supports:

import * as t from 'babel-types'
import template from 'babel-template'
// these all should produce "code;" when generated
template('code;')();
template`code;`();
template`${0}`(t.identifier('code'));
template`${'code'}`({ code: t.identifier('code') });
template`${t.identifier('code')}`()
template({})`code`();

} else {
return template(firstArg, ...rest);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I tried making a separate export instead, but that would be a breaking change because of add-module-exports.

Can we get rid of it? 🙂

@codecov-io
Copy link

Current coverage is 89.14% (diff: 98.14%)

No coverage report found for master at e06faa9.

Powered by Codecov. Last update e06faa9...f8a4233

@hzoo hzoo added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Dec 15, 2016
}

function template (partials: Object | string[], ...args: Array<Object>) {
if (!Array.isArray(partials)) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we throw if args.length > 0 for this case to help in case people mistype something?

}

if (typeof arg === "number") {
replacementMap.set(arg, `$${arg}`);
Copy link
Member

Choose a reason for hiding this comment

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

Do we want $BABEL_TEMPLATE$ on this too? Or is this already an assumption with the current template implementation?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is using the current implementation's positional argument replacement.

It is validated that 0 cannot be used if named arguments are used too, but I forgot to check for /$\d+/ in the string parts themselves...

@loganfsmyth
Copy link
Member

I think this would be a great addition for 7.x, @Kovensky Would you have time to fix the conflict?

@hzoo hzoo added this to the Babel 7.next milestone Sep 14, 2017
Supports:

```js
// these all should produce "code;" when generated
template`code;`();
template`${0}`(t.identifier('code'));
template`${'code'}`({ code: t.identifier('code') });
template`${t.identifier('code')}`()
template({})`code`();
```
@babel-bot
Copy link
Collaborator

babel-bot commented Oct 4, 2017

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/5183/

Diogo Franco (Kovensky) added 2 commits October 4, 2017 11:05
@Jessidhia Jessidhia merged commit 02d0b74 into master Oct 4, 2017
@Jessidhia Jessidhia deleted the babel-template-template branch October 4, 2017 04:07
@RReverser
Copy link
Member

This is a great addition IMO, can it be documented in the README?

@hzoo
Copy link
Member

hzoo commented Oct 5, 2017

Yea we should make an issue about it; it can go in the 7.x post too #6388

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 5, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: New Feature 🚀 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants