Skip to content
Merged
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
11 changes: 11 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@
"doc",
"test"
]
},
{
"login": "djfarly",
"name": "Jan Willem Henckel",
"avatar_url": "https://avatars3.githubusercontent.com/u/5230863?v=4",
"profile": "https://jan.cologne",
"contributions": [
"code",
"doc",
"test"
]
}
],
"repoType": "github"
Expand Down
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ never done that before, that's great! Check this free short video tutorial to
learn how: http://kcd.im/pull-request
-->

* `babel-plugin-codegen` version:
* `node` version:
* `npm` (or `yarn`) version:
- `babel-plugin-codegen` version:
- `node` version:
- `npm` (or `yarn`) version:

Relevant code or config

Expand Down
8 changes: 4 additions & 4 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ merge of your pull request!

<!-- to check an item, place an "x" in the box like so: "- [x] Documentation" -->

* [ ] Documentation
* [ ] Tests
* [ ] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->
* [ ] Added myself to contributors table <!-- this is optional, see the contributing guidelines for instructions -->
- [ ] Documentation
- [ ] Tests
- [ ] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->
- [ ] Added myself to contributors table <!-- this is optional, see the contributing guidelines for instructions -->

<!-- feel free to add additional comments -->
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dist
.opt-in
.opt-out
.DS_Store
.eslintcache

# these cause more harm than good
# when working with contributors
Expand Down
89 changes: 47 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Could you run npx kcd-scripts format to fix all the formatting? I think you editor messed up a few things...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

done :)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Hmmm.... This is weird... It's formatting things completely different for lists... Did prettier change how it formats lists?

[![Build Status][build-badge]][build]
[![Code Coverage][coverage-badge]][coverage]
[![version][version-badge]][package]
[![downloads][downloads-badge]][npmcharts]
[![version][version-badge]][package] [![downloads][downloads-badge]][npmcharts]
[![MIT License][license-badge]][license]

[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Code of Conduct][coc-badge]][coc]
[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc]
[![Babel Macro](https://img.shields.io/badge/babel--macro-%F0%9F%8E%A3-f5da55.svg?style=flat-square)](https://github.com/kentcdodds/babel-plugin-macros)

[![Watch on GitHub][github-watch-badge]][github-watch]
Expand All @@ -30,7 +28,9 @@ maintain the exports in my source file. So someone created a post-build script
to concatenate them to the end of the file. I built this plugin so I could do
that without having an ad-hoc post-build script.

> Read ["Make maintainable workarounds with codegen 💥"](https://blog.kentcdodds.com/make-maintainable-workarounds-with-codegen-d34163a09c13) for more inspiration
> Read
> ["Make maintainable workarounds with codegen 💥"](https://blog.kentcdodds.com/make-maintainable-workarounds-with-codegen-d34163a09c13)
> for more inspiration

## This solution

Expand All @@ -49,23 +49,23 @@ and swaps your usage node with the new AST node.

<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

* [Installation](#installation)
* [Usage](#usage)
* [Template Tag](#template-tag)
* [import comment](#import-comment)
* [codegen.require](#codegenrequire)
* [codegen file comment (`// @codegen`)](#codegen-file-comment--codegen)
* [Configure with Babel](#configure-with-babel)
* [Via `.babelrc` (Recommended)](#via-babelrc-recommended)
* [Via CLI](#via-cli)
* [Via Node API](#via-node-api)
* [Use with `babel-plugin-macros`](#use-with-babel-plugin-macros)
* [APIs not supported by the macro](#apis-not-supported-by-the-macro)
* [Caveats](#caveats)
* [Inspiration](#inspiration)
* [Other Solutions](#other-solutions)
* [Contributors](#contributors)
* [LICENSE](#license)
- [Installation](#installation)
- [Usage](#usage)
- [Template Tag](#template-tag)
- [import comment](#import-comment)
- [codegen.require](#codegenrequire)
- [codegen file comment (`// @codegen`)](#codegen-file-comment--codegen)
- [Configure with Babel](#configure-with-babel)
- [Via `.babelrc` (Recommended)](#via-babelrc-recommended)
- [Via CLI](#via-cli)
- [Via Node API](#via-node-api)
- [Use with `babel-plugin-macros`](#use-with-babel-plugin-macros)
- [APIs not supported by the macro](#apis-not-supported-by-the-macro)
- [Caveats](#caveats)
- [Inspiration](#inspiration)
- [Other Solutions](#other-solutions)
- [Contributors](#contributors)
- [LICENSE](#license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand All @@ -89,13 +89,12 @@ Important notes:

1. All code run by `codegen` is _not_ run in a sandboxed environment
2. All code _must_ run synchronously.
3. All code will be transpiled via `babel-core` directly or `babel-register`
and should follow all of the normal rules for `.babelrc` resolution (the
closest `.babelrc` to the file being run is the one that's used). This means
you can rely on any babel plugins/transforms that you're used to using
elsewhere in your codebase.
4. The code that's generated may or may not be transpiled (babel plugin ordering
is tricky business). **You should generate the code that you wish to ship.**
3. All code will be transpiled via the same instance of babel this plugin is
called with, thus inheriting all presets and plugins. This means you can
rely on any babel plugins/transforms that you're used to using elsewhere in
your codebase.
4. The code that's generated might be transpiled. Please check the output to
make sure. (babel plugin ordering is tricky business 😇)

### Template Tag

Expand Down Expand Up @@ -153,7 +152,8 @@ export a function which accepts those arguments and returns a string.
import /* codegen(3) */ './assign-identity'
```

**After** (`assign-identity.js` is: `module.exports = input => 'var x = ' + JSON.stringify(input) + ';'`):
**After** (`assign-identity.js` is:
`module.exports = input => 'var x = ' + JSON.stringify(input) + ';'`):

```javascript
var x = 3
Expand All @@ -167,17 +167,20 @@ var x = 3
const x = codegen.require('./es6-identity', 3)
```

**After** (`es6-identity.js` is: `export default input => 'var x = ' + JSON.stringify(input) + ';'`):
**After** (`es6-identity.js` is:
`export default input => 'var x = ' + JSON.stringify(input) + ';'`):

```javascript
const x = 3
```

### codegen file comment (`// @codegen`)

Using the codegen file comment will update a whole file to be evaluated down to an export.
Using the codegen file comment will update a whole file to be evaluated down to
an export.

Whereas the above usages (assignment/import/require) will only codegen the scope of the assignment or file being imported.
Whereas the above usages (assignment/import/require) will only codegen the scope
of the assignment or file being imported.

**Before**:

Expand Down Expand Up @@ -225,9 +228,10 @@ require('babel-core').transform('code', {

## Use with `babel-plugin-macros`

Once you've [configured `babel-plugin-macros`](https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/user.md)
you can import/require the codegen macro at `babel-plugin-codegen/macro`.
For example:
Once you've
[configured `babel-plugin-macros`](https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/user.md)
you can import/require the codegen macro at `babel-plugin-codegen/macro`. For
example:

```javascript
import codegen from 'babel-plugin-codegen/macro'
Expand All @@ -243,10 +247,11 @@ export const c = "c";

### APIs not supported by the macro

* [file comment (`// @codegen`)](#codegen-file-comment--codegen)
* [import comment](#import-comment)
- [file comment (`// @codegen`)](#codegen-file-comment--codegen)
- [import comment](#import-comment)

> You could also use [`codegen.macro`][codegen.macro] if you'd prefer to type less 😀
> You could also use [`codegen.macro`][codegen.macro] if you'd prefer to type
> less 😀

## Caveats

Expand Down Expand Up @@ -274,8 +279,8 @@ Thanks goes to these people ([emoji key][emojis]):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->

<!-- prettier-ignore -->
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1958812?v=4" width="100px;"/><br /><sub><b>Michael Rawlings</b></sub>](https://github.com/mlrawlings)<br />[💻](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Code") [📖](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Documentation") [⚠️](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Tests") |
| :---: | :---: |
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1958812?v=4" width="100px;"/><br /><sub><b>Michael Rawlings</b></sub>](https://github.com/mlrawlings)<br />[💻](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Code") [📖](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Documentation") [⚠️](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Tests") | [<img src="https://avatars3.githubusercontent.com/u/5230863?v=4" width="100px;"/><br /><sub><b>Jan Willem Henckel</b></sub>](https://jan.cologne)<br />[💻](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=djfarly "Code") [📖](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=djfarly "Documentation") [⚠️](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=djfarly "Tests") |
| :---: | :---: | :---: |

<!-- ALL-CONTRIBUTORS-LIST:END -->

Expand Down
32 changes: 16 additions & 16 deletions other/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

**Table of Contents**

* [Our Pledge](#our-pledge)
* [Our Standards](#our-standards)
* [Our Responsibilities](#our-responsibilities)
* [Scope](#scope)
* [Enforcement](#enforcement)
* [Attribution](#attribution)
- [Our Pledge](#our-pledge)
- [Our Standards](#our-standards)
- [Our Responsibilities](#our-responsibilities)
- [Scope](#scope)
- [Enforcement](#enforcement)
- [Attribution](#attribution)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand All @@ -29,21 +29,21 @@ orientation.
Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
- Using welcoming and inclusive language
- Being respectful of differing viewpoints and experiences
- Gracefully accepting constructive criticism
- Focusing on what is best for the community
- Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
- The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
- Trolling, insulting/derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
- Other conduct which could reasonably be considered inappropriate in a
professional setting

## Our Responsibilities
Expand Down
10 changes: 5 additions & 5 deletions other/MAINTAINING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

**Table of Contents**

* [Code of Conduct](#code-of-conduct)
* [Issues](#issues)
* [Pull Requests](#pull-requests)
* [Release](#release)
* [Thanks!](#thanks)
- [Code of Conduct](#code-of-conduct)
- [Issues](#issues)
- [Pull Requests](#pull-requests)
- [Release](#release)
- [Thanks!](#thanks)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
"author": "Kent C. Dodds <[email protected]> (http://kentcdodds.com/)",
"license": "MIT",
"dependencies": {
"babel-core": "^6.26.3",
"babel-plugin-macros": "^2.2.1",
"babel-register": "^6.26.0",
"babel-template": "^6.26.0",
"require-from-string": "^2.0.2"
},
"devDependencies": {
Expand Down
30 changes: 29 additions & 1 deletion src/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ const x = codegen.require("./fixtures/return-one", "should not be here...")

↓ ↓ ↓ ↓ ↓ ↓

Error: <PROJECT_ROOT>/src/__tests__/index.js: \`codegen.require\`-ed module (./fixtures/return-one) cannot accept arguments because it does not export a function. You passed the arguments: should not be here...
Error: <PROJECT_ROOT>/src/__tests__/index.js: codegen module (src/__tests__/fixtures/return-one) cannot accept arguments because it does not export a function. You passed the arguments: should not be here...

`;

Expand All @@ -225,6 +225,34 @@ var two = "2";

`;

exports[`codegen 27. codegen: 27. codegen 1`] = `

codegen\`module.exports = "var ALLCAPS = 'ALLCAPS'"\`

↓ ↓ ↓ ↓ ↓ ↓

var ALLCAPS;
ALLCAPS = 'ALLCAPS', void 0;

`;

exports[`codegen accepts babels parser options for generated code: accepts babels parser options for generated code 1`] = `

// @codegen
module.exports = "var fNum: number = do { if(true) {100} else {200} };"

↓ ↓ ↓ ↓ ↓ ↓

var fNum: number = do {
if (true) {
100;
} else {
200;
}
};

`;

exports[`codegen handles multipe nodes: handles multipe nodes 1`] = `

codegen.require('./fixtures/multiple-nodes')
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pluginTester({
// @codegen
/* comment */`,
},
'codegen`module.exports = "var ALLCAPS = \'ALLCAPS\'"`',

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I added this test for the ALLCAPS template regression in babel7.
I guess the plugin test is using babel 6? Can we somehow test this?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

We would need to refactor https://github.com/babel-utils/babel-plugin-tester to be able to accept babel-core as an option so we can provide our own version.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I've implemented this feature so we can test this properly now if you like.

],
})

Expand Down Expand Up @@ -133,5 +134,15 @@ pluginTester({
\\\`
\`
`,
'accepts babels parser options for generated code': {
babelOptions: {
filename: __filename,
parserOpts: {plugins: ['flow', 'doExpressions']},
},
code: `
// @codegen
module.exports = "var fNum: number = do { if(true) {100} else {200} };"
`,
},
},
})
Loading