From 1e8e2176e79027e71a8b91cf63d910e06774fc65 Mon Sep 17 00:00:00 2001 From: Sergey Bekrin Date: Thu, 5 Oct 2017 14:12:29 +0200 Subject: [PATCH 1/3] fix(require): fix requiring of non-toplevel modules --- src/index.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index f2d5d52..ea84ea4 100644 --- a/src/index.js +++ b/src/index.js @@ -48,9 +48,7 @@ function prevalPlugin({types: t, template, transformFromAst}) { } const string = path.get('quasi').evaluate().value if (!string) { - throw new Error( - 'Unable to determine the value of your preval string', - ) + throw new Error('Unable to determine the value of your preval string') } const replacement = getReplacement({string, filename}) path.replaceWith(replacement) @@ -121,7 +119,7 @@ function prevalPlugin({types: t, template, transformFromAst}) { } return result.value }) - const absolutePath = p.join(p.dirname(filename), source.node.value) + const absolutePath = p.resolve(p.dirname(filename), source.node.value) try { // allow for transpilation of required modules require('babel-register') @@ -147,7 +145,10 @@ function prevalPlugin({types: t, template, transformFromAst}) { } function isPrevalComment(comment) { - const normalisedComment = comment.value.trim().split(' ')[0].trim() + const normalisedComment = comment.value + .trim() + .split(' ')[0] + .trim() return ( normalisedComment.startsWith('preval') || normalisedComment.startsWith('@preval') @@ -171,7 +172,7 @@ function looksLike(a, b) { function isPrimitive(val) { // eslint-disable-next-line - return val == null || /^[sbn]/.test(typeof val); + return val == null || /^[sbn]/.test(typeof val) } /* From 2b978fbfbef3c35cb5b0fee0e555e9fc1c9719ef Mon Sep 17 00:00:00 2001 From: Sergey Bekrin Date: Thu, 5 Oct 2017 14:32:37 +0200 Subject: [PATCH 2/3] chore(readme): update contributors table --- .all-contributorsrc | 7 +++++++ README.md | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index d339dce..3caee64 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -86,6 +86,13 @@ "contributions": [ "doc" ] + }, + { + "login": "sergeybekrin", + "name": "Sergey Bekrin", + "avatar_url": "https://avatars0.githubusercontent.com/u/9248479?v=4", + "profile": "http://bekrin.me", + "contributions": [] } ] } diff --git a/README.md b/README.md index d4a4402..d1ab756 100644 --- a/README.md +++ b/README.md @@ -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-8-orange.svg?style=flat-square)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-9-orange.svg?style=flat-square)](#contributors) [![PRs Welcome][prs-badge]][prs] [![Donate][donate-badge]][donate] [![Code of Conduct][coc-badge]][coc] @@ -340,7 +340,7 @@ Thanks goes to these people ([emoji key][emojis]): | [
Kent C. Dodds](https://kentcdodds.com)
[💻](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") | [
Matt Phillips](http://mattphillips.io)
[💻](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") | [
Philip Oliver](https://twitter.com/philipodev)
[🐛](https://github.com/kentcdodds/babel-plugin-preval/issues?q=author%3Aphilipodev "Bug reports") | [
Sorin Davidoi](https://toot.cafe/@sorin)
[🐛](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") | [
Luke Herrington](https://github.com/infiniteluke)
[💡](#example-infiniteluke "Examples") | [
Lufty Wiranda](http://instagram.com/luftywiranda13)
[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=luftywiranda13 "Code") | [
Oscar](http://obartra.github.io)
[💻](https://github.com/kentcdodds/babel-plugin-preval/commits?author=obartra "Code") [⚠️](https://github.com/kentcdodds/babel-plugin-preval/commits?author=obartra "Tests") | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | -| [
pro-nasa](https://github.com/pro-nasa)
[📖](https://github.com/kentcdodds/babel-plugin-preval/commits?author=pro-nasa "Documentation") | +| [
pro-nasa](https://github.com/pro-nasa)
[📖](https://github.com/kentcdodds/babel-plugin-preval/commits?author=pro-nasa "Documentation") | [
Sergey Bekrin](http://bekrin.me)
| This project follows the [all-contributors][all-contributors] specification. From 2c304ccd95e0e62aca383f3b8210a81f754b6301 Mon Sep 17 00:00:00 2001 From: Sergey Bekrin Date: Sun, 8 Oct 2017 17:47:08 +0200 Subject: [PATCH 3/3] feat(test): add test for absolute path resolution --- src/__tests__/__snapshots__/index.js.snap | 36 ++++++++++++------- .../fixtures/nested/absolute-path.js | 3 ++ src/__tests__/index.js | 1 + 3 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 src/__tests__/fixtures/nested/absolute-path.js diff --git a/src/__tests__/__snapshots__/index.js.snap b/src/__tests__/__snapshots__/index.js.snap index 98795b0..01c492f 100644 --- a/src/__tests__/__snapshots__/index.js.snap +++ b/src/__tests__/__snapshots__/index.js.snap @@ -198,6 +198,16 @@ const x = { "a": function a() { exports[`20. preval 1`] = ` +const x = preval.require("../__tests__/fixtures/nested/absolute-path") + + ↓ ↓ ↓ ↓ ↓ ↓ + +const x = "/src/__tests__/fixtures/nested/absolute-path.js"; + +`; + +exports[`21. preval 1`] = ` + const x = preval.require("./fixtures/identity", SOME_UNKNOWN_VARIABLE) ↓ ↓ ↓ ↓ ↓ ↓ @@ -206,7 +216,7 @@ Error: /src/__tests__/index.js: preval cannot determine the value `; -exports[`21. preval 1`] = ` +exports[`22. preval 1`] = ` const x = preval.require("./fixtures/compute-one", "should not be here...") @@ -216,7 +226,7 @@ Error: /src/__tests__/index.js: \`preval.require\`-ed module (./fi `; -exports[`22. preval 1`] = ` +exports[`23. preval 1`] = ` // @preval module.exports = 1 + 2 - 1 - 1 @@ -228,7 +238,7 @@ module.exports = 1; `; -exports[`23. preval 1`] = ` +exports[`24. preval 1`] = ` // @preval const ten = 9 + 1 @@ -241,7 +251,7 @@ module.exports = 50; `; -exports[`24. preval 1`] = ` +exports[`25. preval 1`] = ` // @flow // @preval @@ -255,7 +265,7 @@ module.exports = 1; `; -exports[`25. preval 1`] = ` +exports[`26. preval 1`] = ` // @preval // @flow @@ -269,7 +279,7 @@ module.exports = 1; `; -exports[`26. preval 1`] = ` +exports[`27. preval 1`] = ` // @preval const name = 'Bob Hope' @@ -283,7 +293,7 @@ module.exports = ["Bob", "Hope"]; `; -exports[`27. preval 1`] = ` +exports[`28. preval 1`] = ` // @preval const name = 'Bob Hope' @@ -298,7 +308,7 @@ module.exports = { "first": "Bob", "last": "Hope" }; `; -exports[`28. preval 1`] = ` +exports[`29. preval 1`] = ` // @preval module.exports = require("./fixtures/compute-one") @@ -310,7 +320,7 @@ module.exports = 1; `; -exports[`29. preval 1`] = ` +exports[`30. preval 1`] = ` // @preval module.exports = require("./fixtures/identity")('hello world') @@ -322,7 +332,7 @@ module.exports = "hello world"; `; -exports[`30. preval 1`] = ` +exports[`31. preval 1`] = ` // @preval const id = require("./fixtures/identity") @@ -342,7 +352,7 @@ module.exports = 4; `; -exports[`31. preval 1`] = ` +exports[`32. preval 1`] = ` // @preval const fs = require('fs') @@ -355,7 +365,7 @@ module.exports = "# fixture\\n\\nThis is some file thing...\\n"; `; -exports[`32. preval 1`] = ` +exports[`33. preval 1`] = ` // @preval function fib(x) { @@ -370,7 +380,7 @@ module.exports = 55; `; -exports[`34. preval 1`] = ` +exports[`35. preval 1`] = ` // @preval let smth = {} diff --git a/src/__tests__/fixtures/nested/absolute-path.js b/src/__tests__/fixtures/nested/absolute-path.js new file mode 100644 index 0000000..a9e13df --- /dev/null +++ b/src/__tests__/fixtures/nested/absolute-path.js @@ -0,0 +1,3 @@ +// preval.require would either resolve an absolute path or fail to resolve this +// module at all +module.exports = __filename diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 205d88b..1e316cc 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -53,6 +53,7 @@ pluginTester({ 'const x = preval.require("./fixtures/compute-one")', 'const x = preval.require("./fixtures/identity", 3)', 'const x = preval.require("./fixtures/multiple-functions")', + 'const x = preval.require("../__tests__/fixtures/nested/absolute-path")', error( 'const x = preval.require("./fixtures/identity", SOME_UNKNOWN_VARIABLE)', ),