Skip to content

Commit

Permalink
feat(macro): Error message on wrong usage of macro (#40)
Browse files Browse the repository at this point in the history
* Added error message for wrong usage of macro

* Print AST node type to console on error.

* Fixed bug with JSXClosingElement

* Remove console.log on jsx, sorry

* Added contributor maurobringolf

* Added testcase for error with wrong node types

* Update .all-contributorsrc
  • Loading branch information
maurobringolf authored and Kent C. Dodds committed Oct 19, 2017
1 parent 4517d0e commit df57424
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@
"avatar_url": "https://avatars0.githubusercontent.com/u/9248479?v=4",
"profile": "http://bekrin.me",
"contributions": []
},
{
"login": "maurobringolf",
"name": "Mauro Bringolf",
"avatar_url": "https://avatars0.githubusercontent.com/u/18613301?v=4",
"profile": "https://maurobringolf.ch",
"contributions": [
"code",
"test"
]
}
]
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Pre-evaluate code at build-time
[![downloads][downloads-badge]][npm-stat]
[![MIT License][license-badge]][LICENSE]

[![All Contributors](https://img.shields.io/badge/all_contributors-9-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Donate][donate-badge]][donate]
[![Code of Conduct][coc-badge]][coc]
Expand Down Expand Up @@ -341,7 +341,7 @@ Thanks goes to these people ([emoji key][emojis]):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/babel-plugin-preval/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/babel-plugin-preval/commits?author=kentcdodds "Tests") | [<img src="https://avatars3.githubusercontent.com/u/5610087?v=3" width="100px;"/><br /><sub>Matt Phillips</sub>](http://mattphillips.io)<br />[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=mattphillips "Code") [📖](https://github.com/kentcdodds/babel-plugin-preval/commits?author=mattphillips "Documentation") [⚠️](https://github.com/kentcdodds/babel-plugin-preval/commits?author=mattphillips "Tests") | [<img src="https://avatars1.githubusercontent.com/u/28024000?v=3" width="100px;"/><br /><sub>Philip Oliver</sub>](https://twitter.com/philipodev)<br />[🐛](https://github.com/kentcdodds/babel-plugin-preval/issues?q=author%3Aphilipodev "Bug reports") | [<img src="https://avatars2.githubusercontent.com/u/2109702?v=3" width="100px;"/><br /><sub>Sorin Davidoi</sub>](https://toot.cafe/@sorin)<br />[🐛](https://github.com/kentcdodds/babel-plugin-preval/issues?q=author%3Asorin-davidoi "Bug reports") [💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=sorin-davidoi "Code") [⚠️](https://github.com/kentcdodds/babel-plugin-preval/commits?author=sorin-davidoi "Tests") | [<img src="https://avatars4.githubusercontent.com/u/1127238?v=4" width="100px;"/><br /><sub>Luke Herrington</sub>](https://github.com/infiniteluke)<br />[💡](#example-infiniteluke "Examples") | [<img src="https://avatars4.githubusercontent.com/u/22868432?v=4" width="100px;"/><br /><sub>Lufty Wiranda</sub>](http://instagram.com/luftywiranda13)<br />[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=luftywiranda13 "Code") | [<img src="https://avatars0.githubusercontent.com/u/3877773?v=4" width="100px;"/><br /><sub>Oscar</sub>](http://obartra.github.io)<br />[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=obartra "Code") [⚠️](https://github.com/kentcdodds/babel-plugin-preval/commits?author=obartra "Tests") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars1.githubusercontent.com/u/14310216?v=4" width="100px;"/><br /><sub>pro-nasa</sub>](https://github.com/pro-nasa)<br />[📖](https://github.com/kentcdodds/babel-plugin-preval/commits?author=pro-nasa "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/9248479?v=4" width="100px;"/><br /><sub>Sergey Bekrin</sub>](http://bekrin.me)<br /> |
| [<img src="https://avatars1.githubusercontent.com/u/14310216?v=4" width="100px;"/><br /><sub>pro-nasa</sub>](https://github.com/pro-nasa)<br />[📖](https://github.com/kentcdodds/babel-plugin-preval/commits?author=pro-nasa "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/9248479?v=4" width="100px;"/><br /><sub>Sergey Bekrin</sub>](http://bekrin.me)<br /> | [<img src="https://avatars0.githubusercontent.com/u/18613301?v=4" width="100px;"/><br /><sub>Mauro Bringolf</sub>](https://maurobringolf.ch)<br /> |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification.
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/__snapshots__/macros.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,15 @@ const x = preval\`module.exports = require('./fixtures/compute-one')\`
const x = 1;
`;

exports[`error for other nodes 1`] = `
const preval = require('../macro')
x = 3 + preval
↓ ↓ ↓ ↓ ↓ ↓
Error: <PROJECT_ROOT>/src/__tests__/macros.js: ../macro: babel-plugin-preval/macro can only be used as tagged template expression, function call or JSX element. You tried BinaryExpression.
`;
9 changes: 9 additions & 0 deletions src/__tests__/macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ pluginTester({
)
`,
},
{
title: 'error for other nodes',
error: true,
code: `
const preval = require('../macro')
x = 3 + preval
`,
},
]),
})

Expand Down
7 changes: 6 additions & 1 deletion src/macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ function prevalMacros({references, state}) {
},
state,
)
} else if (referencePath.parentPath.type === 'JSXClosingElement') {
// That's okay, we already prevaled this one on its opening element.
} else {
// TODO: throw a helpful error message
throw new Error(
`babel-plugin-preval/macro can only be used as tagged template expression, function call or JSX element. You tried ${referencePath
.parentPath.type}.`,
)
}
})
}
Expand Down

0 comments on commit df57424

Please sign in to comment.