@0x/sol-compiler
: Make source paths in output artifacts nicer if they're within modules.
`@0x/sol-compiler`: Makea Add v0.6 solidity test.
This commit is contained in:
@@ -315,9 +315,12 @@ function recursivelyGatherDependencySources(
|
|||||||
importRemappings: { [prefix: string]: string },
|
importRemappings: { [prefix: string]: string },
|
||||||
compiledImports: CompiledImports,
|
compiledImports: CompiledImports,
|
||||||
visitedAbsolutePaths: { [absPath: string]: boolean } = {},
|
visitedAbsolutePaths: { [absPath: string]: boolean } = {},
|
||||||
|
importRootDir?: string,
|
||||||
): void {
|
): void {
|
||||||
if (visitedAbsolutePaths[contractPath]) {
|
if (visitedAbsolutePaths[contractPath]) {
|
||||||
return;
|
return;
|
||||||
|
} else {
|
||||||
|
visitedAbsolutePaths[contractPath] = true;
|
||||||
}
|
}
|
||||||
const contractSource = sourcesByAbsolutePath[contractPath].content;
|
const contractSource = sourcesByAbsolutePath[contractPath].content;
|
||||||
const importStatementMatches = contractSource.match(/\nimport[^;]*;/g);
|
const importStatementMatches = contractSource.match(/\nimport[^;]*;/g);
|
||||||
@@ -334,25 +337,30 @@ function recursivelyGatherDependencySources(
|
|||||||
|
|
||||||
let importPath = importPathMatches[1];
|
let importPath = importPathMatches[1];
|
||||||
let absPath = importPath;
|
let absPath = importPath;
|
||||||
|
let _importRootDir = importRootDir;
|
||||||
if (importPath.startsWith('.')) {
|
if (importPath.startsWith('.')) {
|
||||||
absPath = path.join(contractFolder, importPath);
|
absPath = path.join(contractFolder, importPath);
|
||||||
// Express relative imports paths as paths from the root directory.
|
if (_importRootDir) {
|
||||||
importPath = path.relative(rootDir, absPath);
|
// If there's an `_importRootDir`, we're in a package, so express
|
||||||
if (!importPath.startsWith('.')) {
|
// the import path as within the package.
|
||||||
importPath = `./${importPath}`;
|
importPath = path.join(_importRootDir, importPath);
|
||||||
|
} else {
|
||||||
|
// Express relative imports paths as paths from the root directory.
|
||||||
|
importPath = path.relative(rootDir, absPath);
|
||||||
|
if (!importPath.startsWith('.')) {
|
||||||
|
importPath = `./${importPath}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (const [prefix, replacement] of Object.entries(importRemappings)) {
|
for (const [prefix, replacement] of Object.entries(importRemappings)) {
|
||||||
if (importPath.startsWith(prefix)) {
|
if (importPath.startsWith(prefix)) {
|
||||||
absPath = `${replacement}${importPath.substr(prefix.length)}`;
|
absPath = `${replacement}${importPath.substr(prefix.length)}`;
|
||||||
|
_importRootDir = path.dirname(importPath);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
compiledImports[importPath] = sourcesByAbsolutePath[absPath];
|
compiledImports[importPath] = sourcesByAbsolutePath[absPath];
|
||||||
visitedAbsolutePaths[absPath] = true;
|
|
||||||
|
|
||||||
recursivelyGatherDependencySources(
|
recursivelyGatherDependencySources(
|
||||||
absPath,
|
absPath,
|
||||||
rootDir,
|
rootDir,
|
||||||
@@ -360,6 +368,7 @@ function recursivelyGatherDependencySources(
|
|||||||
importRemappings,
|
importRemappings,
|
||||||
compiledImports,
|
compiledImports,
|
||||||
visitedAbsolutePaths,
|
visitedAbsolutePaths,
|
||||||
|
_importRootDir,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -26,7 +26,7 @@ describe('#Compiler', function(): void {
|
|||||||
it('should create a Compiler with empty opts', async () => {
|
it('should create a Compiler with empty opts', async () => {
|
||||||
const _compiler = new Compiler(); // tslint:disable-line no-unused-variable
|
const _compiler = new Compiler(); // tslint:disable-line no-unused-variable
|
||||||
});
|
});
|
||||||
it('should create an Exchange artifact with the correct unlinked binary', async () => {
|
it.only('should create an Exchange artifact with the correct unlinked binary', async () => {
|
||||||
compilerOpts.contracts = ['Exchange'];
|
compilerOpts.contracts = ['Exchange'];
|
||||||
|
|
||||||
const exchangeArtifactPath = `${artifactsDir}/Exchange.json`;
|
const exchangeArtifactPath = `${artifactsDir}/Exchange.json`;
|
||||||
@@ -114,4 +114,22 @@ describe('#Compiler', function(): void {
|
|||||||
expect(artifact).to.equal('EmptyContract.json');
|
expect(artifact).to.equal('EmptyContract.json');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
it('should compile a V0.6 contract', async () => {
|
||||||
|
compilerOpts.contracts = ['V6Contract'];
|
||||||
|
|
||||||
|
const artifactPath = `${artifactsDir}/V6Contract.json`;
|
||||||
|
if (fsWrapper.doesPathExistSync(artifactPath)) {
|
||||||
|
await fsWrapper.removeFileAsync(artifactPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
await new Compiler(compilerOpts).compileAsync();
|
||||||
|
|
||||||
|
const opts = {
|
||||||
|
encoding: 'utf8',
|
||||||
|
};
|
||||||
|
const exchangeArtifactString = await fsWrapper.readFileAsync(artifactPath, opts);
|
||||||
|
const exchangeArtifact: ContractArtifact = JSON.parse(exchangeArtifactString);
|
||||||
|
const bin = exchangeArtifact.compilerOutput.evm.bytecode.object;
|
||||||
|
expect(bin.slice(2)).to.length.to.be.gt(0);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
37
packages/sol-compiler/test/fixtures/contracts/V6Contract.sol
vendored
Normal file
37
packages/sol-compiler/test/fixtures/contracts/V6Contract.sol
vendored
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
Copyright 2020 ZeroEx Intl.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
pragma solidity ^0.6.4;
|
||||||
|
|
||||||
|
|
||||||
|
contract V6Contract {
|
||||||
|
|
||||||
|
uint256 private _privateNumber;
|
||||||
|
|
||||||
|
constructor(uint256 privateNumber) public {
|
||||||
|
_privateNumber = privateNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
fallback() external {
|
||||||
|
revert('nope');
|
||||||
|
}
|
||||||
|
|
||||||
|
receive() payable external {
|
||||||
|
// no-op
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user