Comparing version
@@ -0,1 +1,6 @@ | ||
## Version 0.10.0 | ||
- Add resolving of imports from `node_modules` (e.g., `import "openzeppelin-solidity/contracts/math/Math.sol"`). | ||
- Fix `tolerant`, `noFlatten`, and `noPreprocessor` options not being inherited | ||
by imported dependencies. | ||
## Version 0.9.3 | ||
@@ -2,0 +7,0 @@ - Fix `--output` command line option failing. See https://github.com/merklejerk/solpp/pull/2. |
{ | ||
"name": "solpp", | ||
"version": "0.9.3", | ||
"version": "0.10.0", | ||
"description": "A solidity preprocessor and flattener CLI and library", | ||
@@ -26,6 +26,8 @@ "repository": "https://github.com/merklejerk/solpp", | ||
"mz": "^2.7.0", | ||
"resolve": "^1.10.0", | ||
"semver": "^5.6.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^5.2.0" | ||
"mocha": "^5.2.0", | ||
"openzeppelin-solidity": "1.12.0" | ||
}, | ||
@@ -32,0 +34,0 @@ "keywords": [ |
@@ -60,8 +60,11 @@  | ||
```js | ||
```solidity | ||
// solpp will inline this file and any of its dependencies. | ||
import './MyLibrary.sol'; | ||
// If you have a file in node_modules, solpp can grab that too. | ||
import 'openzeppelin-solidity/contracts/math/Math.sol'; | ||
// solpp can also do the same with URLs! | ||
import 'https://raw.githubusercontent.com/OpenZeppelin/openzeppelin-solidity/v2.0.0/contracts/token/ERC20/ERC20.sol'; | ||
contract MyContract { | ||
@@ -275,3 +278,3 @@ // Define and use a symbol. | ||
**Example** | ||
```js | ||
```solidity | ||
// Single line comment directive. | ||
@@ -303,3 +306,3 @@ // #def LDEF All this text is the value of LDEF. | ||
**Input** | ||
```js | ||
```solidity | ||
// Define a symbol | ||
@@ -317,3 +320,3 @@ // #def MY_EXPR 1 + 1 | ||
**Result** | ||
```js | ||
```solidity | ||
// Define a symbol | ||
@@ -343,3 +346,3 @@ // Expand it. | ||
**Input** | ||
```js | ||
```solidity | ||
// Define a macro | ||
@@ -362,3 +365,3 @@ // #def MY_MACRO(x) (x / 2) + 1 | ||
**Result** | ||
```js | ||
```solidity | ||
// Define a macro | ||
@@ -387,3 +390,3 @@ // Expand it. | ||
**Input** | ||
```js | ||
```solidity | ||
// Expanding a symbol. | ||
@@ -399,3 +402,3 @@ // #def SYM_1 foo / 2 | ||
**Result** | ||
```js | ||
```solidity | ||
// Expanding a symbol. | ||
@@ -424,3 +427,3 @@ uint256 v = 1 + foo / 2; | ||
**Input** | ||
```js | ||
```solidity | ||
// Evaluating a symbol. | ||
@@ -440,3 +443,3 @@ // #def SYM_1 5 * 2 | ||
**Result** | ||
```js | ||
```solidity | ||
// Evaluating a symbol. | ||
@@ -458,3 +461,3 @@ uint256 v = 1 + 10; | ||
**Input** | ||
```js | ||
```solidity | ||
// Compute the sqrt of 2 as parts per million. | ||
@@ -466,3 +469,3 @@ // Here we use the builtin function 'int' to make the result an integer. | ||
**Result** | ||
```js | ||
```solidity | ||
// Compute the sqrt of 2 as parts per million. | ||
@@ -488,3 +491,3 @@ // Here we use the builtin function 'int' to make the result an integer. | ||
**Input** | ||
```js | ||
```solidity | ||
// #if true | ||
@@ -514,3 +517,3 @@ // This block will always render. | ||
**Result** (with `EXT_SYM_2='foobar'`) | ||
```js | ||
```solidity | ||
// This block will always render. | ||
@@ -537,3 +540,3 @@ uint256 x = 100; | ||
**Input** | ||
```js | ||
```solidity | ||
// Calculate the summation of 0...4 | ||
@@ -547,3 +550,3 @@ uint256 sum = 0; | ||
**Result** | ||
```js | ||
```solidity | ||
// Calculate the summation of 0...4 | ||
@@ -550,0 +553,0 @@ uint256 sum = 0; |
@@ -199,6 +199,9 @@ const _ = require('lodash'); | ||
async _includeImport(path, loc) { | ||
const resolved = await this.resolver(path, this.cwd, this.name); | ||
if (!resolved) { | ||
throw new Error(`Cannot resolve import "${path}": ` + | ||
this._createLocationString(loc)); | ||
let resolved; | ||
try { | ||
resolved = await this.resolver(path, this.cwd, this.name); | ||
} catch (err) { | ||
const msg = `Cannot resolve import "${path}" at ` + | ||
this._createLocationString(loc) + `: ${err.message}`; | ||
throw new Error(msg); | ||
} | ||
@@ -223,3 +226,6 @@ if (resolved.name in this.cache) // Already included. | ||
depth: this.depth + 1, | ||
pragmas: this.pragmas | ||
pragmas: this.pragmas, | ||
noFlatten: this.noFlatten, | ||
noPreprocessor: this.noPreprocessor, | ||
tolerant: this.tolerant | ||
}); | ||
@@ -226,0 +232,0 @@ return oven.transform(code); |
@@ -5,9 +5,24 @@ 'use strict' | ||
const axios = require('axios'); | ||
const promisify = require('util').promisify; | ||
const {URL} = require('url'); | ||
const {dirname, isAbsolute, resolve} = require('path'); | ||
const { | ||
dirname, | ||
isAbsolute, | ||
resolve:resolvePath, | ||
sep:pathSeparator | ||
} = require('path'); | ||
const resolveModuleFile = promisify(require('resolve')); | ||
const URL_TEST = /^https?:\/\//i; | ||
const URL_PATH_TEST = /^https?:\/\//i; | ||
const RELATIVE_PATH_TEST = createRelativePathTest(); | ||
function isURL(path, cwd, from) { | ||
if (URL_TEST.test(path) || URL_TEST.test(cwd)) | ||
function createRelativePathTest() { | ||
if (pathSeparator === '\\') | ||
pathSeparator = '\\\\'; | ||
const regex = `^[.]+${pathSeparator}.+$`; | ||
return new RegExp(regex, 'i'); | ||
} | ||
function isURLPath(path, cwd, from) { | ||
if (URL_PATH_TEST.test(path) || URL_PATH_TEST.test(cwd)) | ||
return true; | ||
@@ -17,2 +32,8 @@ return false; | ||
function isRelativePath(path, cwd, from) { | ||
if (_.isNil(from)) | ||
return true; | ||
return !URL_PATH_TEST.test(cwd) && RELATIVE_PATH_TEST.test(path); | ||
} | ||
function dirnameURL(url) { | ||
@@ -26,5 +47,5 @@ const r = new URL(url); | ||
function joinURLPath(cwd, path) { | ||
if (URL_TEST.test(path)) | ||
if (URL_PATH_TEST.test(path)) | ||
return path; | ||
if (!URL_TEST.test(cwd)) | ||
if (!URL_PATH_TEST.test(cwd)) | ||
throw new Error(`Cannot resolve URL segment "${path}" from "${cwd}"`); | ||
@@ -44,3 +65,3 @@ let {origin, pathname} = new URL(cwd); | ||
async function URLResolver(path, cwd, from) { | ||
async function urlResolver(path, cwd, from) { | ||
const url = joinURLPath(cwd, path); | ||
@@ -60,15 +81,30 @@ const dir = dirnameURL(url); | ||
async function resolver(path, cwd, from) { | ||
if (isURL(path, cwd, from)) | ||
return URLResolver(path, cwd, from); | ||
if (!isAbsolute(path)) | ||
path = resolve(cwd, path); | ||
const code = await fs.readFile(path, 'utf-8'); | ||
async function relativeResolver(path, cwd, from) { | ||
const resolved = resolvePath(cwd, path); | ||
const code = await fs.readFile(resolved, 'utf-8'); | ||
return { | ||
code: code, | ||
name: path, | ||
cwd: dirname(path) | ||
name: resolved, | ||
cwd: dirname(resolved) | ||
}; | ||
} | ||
async function moduleResolver(path, cwd, from) { | ||
const resolved = await resolveModuleFile(path, {basedir: dirname(cwd)}); | ||
const code = await fs.readFile(resolved, 'utf-8'); | ||
return { | ||
code: code, | ||
name: resolved, | ||
cwd: dirname(resolved) | ||
}; | ||
} | ||
async function resolver(path, cwd, from) { | ||
if (isURLPath(path, cwd, from)) | ||
return urlResolver(path, cwd, from); | ||
if (isRelativePath(path, cwd, from)) | ||
return relativeResolver(path, cwd, from); | ||
return moduleResolver(path, cwd, from); | ||
} | ||
module.exports = resolver; |
414809
0.41%8073
0.47%705
0.43%9
12.5%2
100%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed