-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(macros): add macros (undocumented pre-release feature)
This adds alpha support for [babel-macros](https://github.com/kentcdodds/babel-macros) ref: kentcdodds/babel-plugin-macros#1 Usage: ```javascript import preval from 'babel-plugin-preval/macros' const x = preval`module.exports = Math.random()` ```
- Loading branch information
1 parent
42397e6
commit 985de49
Showing
8 changed files
with
3,585 additions
and
576 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,27 +9,21 @@ | |
"commitmsg": "opt --in commit-msg --exec \"validate-commit-msg\"", | ||
"precommit": "lint-staged && opt --in pre-commit --exec \"npm start validate\"" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"keywords": [ | ||
"babel", | ||
"babel-plugin", | ||
"eval", | ||
"precompile" | ||
], | ||
"files": ["dist"], | ||
"keywords": ["babel", "babel-plugin", "eval", "precompile"], | ||
"author": "Kent C. Dodds <[email protected]> (http://kentcdodds.com/)", | ||
"license": "MIT", | ||
"dependencies": { | ||
"babel-core": "^6.25.0", | ||
"babylon": "^6.17.4", | ||
"require-from-string": "^1.2.1" | ||
}, | ||
"devDependencies": { | ||
"all-contributors-cli": "^4.3.0", | ||
"ast-pretty-print": "^2.0.0", | ||
"babel-cli": "^6.24.1", | ||
"babel-core": "^6.25.0", | ||
"babel-jest": "^20.0.3", | ||
"babel-macros": "0.3.0", | ||
"babel-plugin-tester": "^3.2.0", | ||
"babel-plugin-transform-class-properties": "^6.24.1", | ||
"babel-plugin-transform-inline-environment-variables": "0.1.1", | ||
|
@@ -50,17 +44,11 @@ | |
"validate-commit-msg": "^2.12.1" | ||
}, | ||
"lint-staged": { | ||
"*.js": [ | ||
"prettier-eslint --write --print-width=80", | ||
"git add" | ||
] | ||
"*.js": ["prettier-eslint --write --print-width=80", "git add"] | ||
}, | ||
"jest": { | ||
"testEnvironment": "node", | ||
"testPathIgnorePatterns": [ | ||
"/node_modules/", | ||
"/fixtures/" | ||
], | ||
"testPathIgnorePatterns": ["/node_modules/", "/fixtures/"], | ||
"coverageThreshold": { | ||
"global": { | ||
"branches": 100, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`1. as tag 1`] = ` | ||
import preval from '../macros' | ||
const x = preval\`module.exports = require('./fixtures/compute-one')\` | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
const x = 1; | ||
`; | ||
|
||
exports[`2. as function 1`] = ` | ||
const myPreval = require('../macros') | ||
const x = myPreval(\` | ||
module.exports = require('./fixtures/identity')({sayHi: () => 'hi'}) | ||
\`) | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
const x = { "sayHi": function sayHi() { | ||
return 'hi'; | ||
} }; | ||
`; | ||
|
||
exports[`3. as jsx 1`] = ` | ||
const Preval = require('../macros') | ||
const ui = ( | ||
<Preval> | ||
const fs = require('fs') | ||
module.exports = fs.readFileSync(require.resolve('./fixtures/fixture1.md'), 'utf8') | ||
</Preval> | ||
) | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
const ui = <div>"# fixture\\n\\nThis is some file thing...\\n"</div>; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import path from 'path' | ||
import pluginTester from 'babel-plugin-tester' | ||
import plugin from 'babel-macros' | ||
|
||
const projectRoot = path.join(__dirname, '../../') | ||
|
||
expect.addSnapshotSerializer({ | ||
print(val) { | ||
return val.split(projectRoot).join('<PROJECT_ROOT>/') | ||
}, | ||
test(val) { | ||
return typeof val === 'string' | ||
}, | ||
}) | ||
|
||
pluginTester({ | ||
plugin, | ||
snapshot: true, | ||
tests: withFilename([ | ||
{ | ||
title: 'as tag', | ||
code: ` | ||
import preval from '../macros' | ||
const x = preval\`module.exports = require('./fixtures/compute-one')\` | ||
`, | ||
}, | ||
{ | ||
title: 'as function', | ||
code: ` | ||
const myPreval = require('../macros') | ||
const x = myPreval(\` | ||
module.exports = require('./fixtures/identity')({sayHi: () => 'hi'}) | ||
\`) | ||
`, | ||
}, | ||
{ | ||
title: 'as jsx', | ||
code: ` | ||
const Preval = require('../macros') | ||
const ui = ( | ||
<Preval> | ||
const fs = require('fs') | ||
module.exports = fs.readFileSync(require.resolve('./fixtures/fixture1.md'), 'utf8') | ||
</Preval> | ||
) | ||
`, | ||
}, | ||
]), | ||
}) | ||
|
||
/* | ||
* This adds the filename to each test so you can do require/import relative | ||
* to this test file. | ||
*/ | ||
function withFilename(tests) { | ||
return tests.map(t => { | ||
const test = {babelOptions: {filename: __filename}} | ||
if (typeof t === 'string') { | ||
test.code = t | ||
} else { | ||
Object.assign(test, t) | ||
test.babelOptions.parserOpts = test.babelOptions.parserOpts || {} | ||
} | ||
Object.assign(test.babelOptions.parserOpts, { | ||
// add the jsx plugin to all tests because why not? | ||
plugins: ['jsx'], | ||
}) | ||
return test | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const babel = require('babel-core') | ||
const requireFromString = require('require-from-string') | ||
const objectToAST = require('./object-to-ast') | ||
|
||
module.exports = getReplacement | ||
|
||
function getReplacement({string: stringToPreval, filename}) { | ||
const {code: transpiled} = babel.transform(stringToPreval, { | ||
filename, | ||
}) | ||
const val = requireFromString(transpiled, filename) | ||
return objectToAST(val) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// const printAST = require('ast-pretty-print') | ||
const getReplacement = require('./get-replacement') | ||
|
||
// this implements the babel-macros v0.2.0 API | ||
module.exports = {asTag, asFunction, asJSX} | ||
|
||
function asTag(quasiPath, {file: {opts: {filename}}}) { | ||
const string = quasiPath.parentPath.get('quasi').evaluate().value | ||
quasiPath.parentPath.replaceWith( | ||
getReplacement({ | ||
string, | ||
filename, | ||
}), | ||
) | ||
} | ||
|
||
function asFunction(argumentsPaths, {file: {opts: {filename}}}) { | ||
const string = argumentsPaths[0].evaluate().value | ||
argumentsPaths[0].parentPath.replaceWith( | ||
getReplacement({ | ||
string, | ||
filename, | ||
}), | ||
) | ||
} | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
function asJSX({attributes, children}, {file: {opts: {filename}}}) { | ||
// It's a shame you cannot use evaluate() with JSX | ||
const string = children[0].node.value | ||
children[0].replaceWith( | ||
getReplacement({ | ||
string, | ||
filename, | ||
}), | ||
) | ||
const { | ||
parentPath: {node: {openingElement, closingElement}}, | ||
} = children[0] | ||
openingElement.name.name = 'div' | ||
closingElement.name.name = 'div' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const babylon = require('babylon') | ||
|
||
module.exports = objectToAST | ||
|
||
function objectToAST(object) { | ||
const stringified = stringify(object) | ||
const fileNode = babylon.parse(`var x = ${stringified}`) | ||
return fileNode.program.body[0].declarations[0].init | ||
} | ||
|
||
function stringify(object) { | ||
return JSON.stringify(object, stringifyReplacer).replace( | ||
/"__FUNCTION_START__(.*)__FUNCTION_END__"/g, | ||
functionReplacer, | ||
) | ||
function stringifyReplacer(key, value) { | ||
if (typeof value === 'function') { | ||
return `__FUNCTION_START__${value.toString()}__FUNCTION_END__` | ||
} | ||
return value | ||
} | ||
function functionReplacer(match, p1) { | ||
return p1.replace(/\\"/g, '"').replace(/\\n/g, '\n') | ||
} | ||
} |