Skip to content

Commit

Permalink
fix: improve macro detection
Browse files Browse the repository at this point in the history
And upgrade deps
  • Loading branch information
Kent C. Dodds committed Oct 14, 2017
1 parent 643c7e6 commit 945c8eb
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 49 deletions.
6 changes: 6 additions & 0 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const babelConfig = require('kcd-scripts/babel')

babelConfig.plugins = babelConfig.plugins.filter(
pluginPath => !pluginPath.includes('babel-macros/dist/index.js'),
)
module.exports = babelConfig
66 changes: 38 additions & 28 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,65 @@ Thanks for being willing to contribute!
## Project setup

1. Fork and clone the repo
2. `$ npm install` to install dependencies
3. `$ npm start validate` to validate you've got it working
4. Create a branch for your PR

This project uses [`nps`][nps] and you can run `npm start` to see what scripts are available.
2. Run `npm run setup -s` to install dependencies and run validation
3. Create a branch for your PR with `git checkout -b pr/your-branch-name`

> Tip: Keep your `master` branch pointing at the original repository and make
> pull requests from branches on your fork. To do this, run:
>
> ```
> git remote add upstream https://github.com/kentcdodds/babel-macros.git
> git fetch upstream
> git branch --set-upstream-to=upstream/master master
> ```
>
> This will add the original repository as a "remote" called "upstream,"
> Then fetch the git information from that remote, then set your local `master`
> branch to use the upstream master branch whenever you run `git pull`.
> Then you can make all of your pull request branches based on this `master`
> branch. Whenever you want to update your version of `master`, do a regular
> `git pull`.
## Add yourself as a contributor

This project follows the [all contributors][all-contributors] specification. To add yourself to the table of
contributors on the README.md, please use the automated script as part of your PR:
This project follows the [all contributors][all-contributors] specification.
To add yourself to the table of contributors on the `README.md`, please use the
automated script as part of your PR:

```console
npm start "contributors.add <YOUR_GITHUB_USERNAME>"
npm run add-contributor
```

Follow the prompt. If you've already added yourself to the list and are making a
new type of contribution, you can run it again and select the added contribution
type. If you need to edit the `.all-contributorsrc` file by hand that's fine
too, just run `npm start contributors.generate` to regenerate the table.
Follow the prompt and commit `.all-contributorsrc` and `README.md` in the PR.
If you've already added yourself to the list and are making
a new type of contribution, you can run it again and select the added
contribution type.

## Committing and Pushing changes

This project uses [`semantic-release`][semantic-release] to do automatic releases and generate a changelog based on the
commit history. So we follow [a convention][convention] for commit messages. Please follow this convention for your
commit messages. If you don't, it's not a huge deal because we can change your commit message when we merge anyway.

Please make sure to run the tests before you commit your changes. You can run `npm start test.update` which will
update any snapshots that need updating. Make sure to include those changes in your commit.
Please make sure to run the tests before you commit your changes. You can run
`npm run test:update` which will update any snapshots that need updating.
Make sure to include those changes (if they exist) in your commit.

### opt into git hooks

There are git hooks set up with this project that are automatically installed when you install dependencies. They're
really handy, but are turned off by default (so as to not hinder new contributors). You can opt into these by creating
a file called `.opt-in` at the root of the project and putting this inside:
There are git hooks set up with this project that are automatically installed
when you install dependencies. They're really handy, but are turned off by
default (so as to not hinder new contributors). You can opt into these by
creating a file called `.opt-in` at the root of the project and putting this
inside:

```
pre-commit
```

## Help needed

Please checkout the [ROADMAP.md][ROADMAP] and raise an issue to discuss
any of the items in the want to do or might do list.
Please checkout the [the open issues][issues]

Also, please watch the repo and respond to questions/bug reports/feature requests! Thanks!
Also, please watch the repo and respond to questions/bug reports/feature
requests! Thanks!

[egghead]: https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github
[semantic-release]: https://npmjs.com/package/semantic-release
[convention]: https://github.com/conventional-changelog/conventional-changelog-angular/blob/ed32559941719a130bb0327f886d6a32a8cbc2ba/convention.md
[all-contributors]: https://github.com/kentcdodds/all-contributors
[ROADMAP]: ./other/ROADMAP.md
[nps]: https://npmjs.com/package/nps
[issues]: https://github.com/kentcdodds/babel-macros/issues
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// const printAST = require('ast-pretty-print')
const {createMacro} = require('../src')
const {createMacro} = require('../../src')

module.exports = createMacro(evalMacro)

Expand Down
File renamed without changes.
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"test": "kcd-scripts test",
"test:update": "npm test -- --updateSnapshot --coverage",
"validate": "kcd-scripts validate",
"setup": "npm install && npm run validate -s",
"precommit": "kcd-scripts precommit"
},
"files": ["dist"],
Expand All @@ -24,16 +25,20 @@
"license": "MIT",
"dependencies": {},
"devDependencies": {
"ast-pretty-print": "^2.0.0",
"ast-pretty-print": "2.0.1",
"babel-core": "^6.25.0",
"babel-plugin-tester": "^4.0.0",
"babylon": "6.17.4",
"kcd-scripts": "0.3.4"
"babylon": "6.18.0",
"cpy": "6.0.0",
"kcd-scripts": "0.27.0"
},
"eslintConfig": {
"extends": "./node_modules/kcd-scripts/eslint.js"
},
"eslintIgnore": ["node_modules", "coverage", "dist"],
"babel": {
"presets": ["./.babelrc.js"]
},
"repository": {
"type": "git",
"url": "https://github.com/kentcdodds/babel-macros.git"
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ const x = 79;

exports[`appends the npm URL for errors thrown by node modules 1`] = `
import errorThrower from 'error-thrower.macro'
import errorThrower from 'babel-macros-test-error-thrower.macro'
errorThrower('hi')
↓ ↓ ↓ ↓ ↓ ↓
Error: <PROJECT_ROOT>/src/__tests__/index.js: error-thrower.macro: not helpful Learn more: https://www.npmjs.com/package/error-thrower.macro
Error: <PROJECT_ROOT>/src/__tests__/index.js: babel-macros-test-error-thrower.macro: not helpful Learn more: https://www.npmjs.com/package/babel-macros-test-error-thrower.macro
`;
exports[`appends the npm URL for errors thrown by node modules with a slash 1`] = `
import errorThrower from 'error-thrower/macro'
import errorThrower from 'babel-macros-test-error-thrower/macro'
errorThrower('hi')
↓ ↓ ↓ ↓ ↓ ↓
Error: <PROJECT_ROOT>/src/__tests__/index.js: error-thrower/macro: not helpful Learn more: https://www.npmjs.com/package/error-thrower
Error: <PROJECT_ROOT>/src/__tests__/index.js: babel-macros-test-error-thrower/macro: not helpful Learn more: https://www.npmjs.com/package/babel-macros-test-error-thrower
`;
Expand Down Expand Up @@ -88,7 +88,7 @@ Error: <PROJECT_ROOT>/src/__tests__/index.js: ./fixtures/error-thrower.macro: ve
exports[`supports macros from node_modules 1`] = `
import fakeMacro from 'fake/macro'
import fakeMacro from 'babel-macros-test-fake/macro'
fakeMacro('hi')
↓ ↓ ↓ ↓ ↓ ↓
Expand Down
35 changes: 24 additions & 11 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import path from 'path'
// eslint-disable-next-line
import fakeMacro from 'fake/macro'
import cpy from 'cpy'
import babel from 'babel-core'
import pluginTester from 'babel-plugin-tester'
import plugin from '../'

afterEach(() => {
fakeMacro.innerFn.mockClear()
const projectRoot = path.join(__dirname, '../../')

beforeAll(() => {
// copy our mock modules to the node_modules directory
// so we can test how things work when importing a macro
// from the node_modules directory.
return cpy(['**/*.js'], path.join('..', '..', 'node_modules'), {
parents: true,
cwd: path.join(projectRoot, 'other', 'mock-modules'),
})
})

const projectRoot = path.join(__dirname, '../../')
afterEach(() => {
// eslint-disable-next-line
require('babel-macros-test-fake/macro').innerFn.mockClear()
})

// we don't actually need this,
// but I found that it removes double escaping when I include it...
expect.addSnapshotSerializer({
print(val) {
return val.split(projectRoot).join('<PROJECT_ROOT>/')
return val
.split(projectRoot)
.join('<PROJECT_ROOT>/')
.replace(/\\/g, '/')
},
test(val) {
return typeof val === 'string'
Expand Down Expand Up @@ -87,12 +98,14 @@ pluginTester({
{
title: 'supports macros from node_modules',
code: `
import fakeMacro from 'fake/macro'
import fakeMacro from 'babel-macros-test-fake/macro'
fakeMacro('hi')
`,
teardown() {
// kinda abusing the babel-plugin-tester API here
// to make an extra assertion
// eslint-disable-next-line
const fakeMacro = require('babel-macros-test-fake/macro')
expect(fakeMacro.innerFn).toHaveBeenCalledTimes(1)
expect(fakeMacro.innerFn).toHaveBeenCalledWith({
references: expect.any(Object),
Expand Down Expand Up @@ -131,7 +144,7 @@ pluginTester({
title: 'appends the npm URL for errors thrown by node modules',
error: true,
code: `
import errorThrower from 'error-thrower.macro'
import errorThrower from 'babel-macros-test-error-thrower.macro'
errorThrower('hi')
`,
},
Expand All @@ -140,7 +153,7 @@ pluginTester({
'appends the npm URL for errors thrown by node modules with a slash',
error: true,
code: `
import errorThrower from 'error-thrower/macro'
import errorThrower from 'babel-macros-test-error-thrower/macro'
errorThrower('hi')
`,
},
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MacroError extends Error {
}

function createMacro(macro) {
macroWrapper.isBabelMacro = true
return macroWrapper

function macroWrapper(options) {
Expand Down Expand Up @@ -118,7 +119,7 @@ function applyMacros({path, imports, source, state, babel}) {
}
// eslint-disable-next-line import/no-dynamic-require
const macro = require(requirePath)
if (macro.name !== 'macroWrapper') {
if (!macro.isBabelMacro) {
throw new Error(
// eslint-disable-next-line prefer-template
`The macro imported from "${source}" must be wrapped in "createMacro" ` +
Expand Down

0 comments on commit 945c8eb

Please sign in to comment.