Skip to content
This repository was archived by the owner on Sep 12, 2025. It is now read-only.

Commit 8df4471

Browse files
authored
Merge pull request #86 from 0xProject/feat/parse-imports-single-quotes
Feat: Add ability to parse single quote imports
2 parents 2ca3096 + 6f68b71 commit 8df4471

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

sol-compiler/src/utils/compiler.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,22 @@ export function getNormalizedErrMsg(errMsg: string): string {
9292
}
9393

9494
/**
95-
* Parses the contract source code and extracts the dendencies
95+
* Parses the contract source code and extracts the dependencies
9696
* @param source Contract source code
97-
* @return List of dependendencies
97+
* @return List of dependencies
9898
*/
9999
export function parseDependencies(contractSource: ContractSource): string[] {
100100
// TODO: Use a proper parser
101101
const source = contractSource.source;
102102
const sourceWithoutComments = stripComments(source);
103103
const IMPORT_REGEX = /(import\s)/;
104-
const DEPENDENCY_PATH_REGEX = /"([^"]+)"/; // Source: https://github.com/BlockChainCompany/soljitsu/blob/master/lib/shared.js
105104
const dependencies: string[] = [];
106105
const lines = sourceWithoutComments.split('\n');
107106
_.forEach(lines, line => {
108107
if (line.match(IMPORT_REGEX) !== null) {
109-
const dependencyMatch = line.match(DEPENDENCY_PATH_REGEX);
108+
const dependencyMatch =
109+
line.match(constants.DEPENDENCY_PATH_REGEX_DOUBLE_QUOTES) ??
110+
line.match(constants.DEPENDENCY_PATH_REGEX_SINGLE_QUOTES);
110111
if (dependencyMatch !== null) {
111112
let dependencyPath = dependencyMatch[1];
112113
if (dependencyPath.startsWith('.')) {
@@ -350,7 +351,9 @@ function recursivelyGatherDependencySources(
350351
const lastPathSeparatorPos = contractPath.lastIndexOf('/');
351352
const contractFolder = lastPathSeparatorPos === -1 ? '' : contractPath.slice(0, lastPathSeparatorPos + 1);
352353
for (const importStatementMatch of importStatementMatches) {
353-
const importPathMatches = importStatementMatch.match(/\"([^\"]*)\"/);
354+
const importPathMatches =
355+
importStatementMatch.match(constants.DEPENDENCY_PATH_REGEX_DOUBLE_QUOTES) ??
356+
importStatementMatch.match(constants.DEPENDENCY_PATH_REGEX_SINGLE_QUOTES);
354357
if (importPathMatches === null || importPathMatches.length === 0) {
355358
continue;
356359
}

sol-compiler/src/utils/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,6 @@ export const constants = {
6666
'0.1.2': 'soljson-v0.1.2+commit.d0d36e3.js',
6767
'0.1.1': 'soljson-v0.1.1+commit.6ff4cd6.js',
6868
},
69+
DEPENDENCY_PATH_REGEX_DOUBLE_QUOTES: /"([^"]+)"/,
70+
DEPENDENCY_PATH_REGEX_SINGLE_QUOTES: /'([^']+)'/,
6971
};

sol-compiler/test/compiler_utils_test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ describe('Compiler utils', () => {
7373
'zeppelin-solidity/contracts/token/ERC20/ERC20.sol',
7474
]);
7575
});
76+
it('correctly parses single quote dependencies', async () => {
77+
const path = `${__dirname}/fixtures/contracts/SingleQuoteImports.sol`;
78+
const source = await fsWrapper.readFileAsync(path, {
79+
encoding: 'utf8',
80+
});
81+
const dependencies = parseDependencies({ source, path, absolutePath: path });
82+
const expectedDependencies = ['SomeDependency.sol'];
83+
_.each(expectedDependencies, expectedDepdency => {
84+
const foundDependency = _.find(dependencies, dependency => _.endsWith(dependency, expectedDepdency));
85+
expect(foundDependency, `${expectedDepdency} not found`).to.not.be.undefined();
86+
});
87+
});
7688
// TODO: For now that doesn't work. This will work after we switch to a grammar-based parser
7789
it.skip('correctly parses commented out dependencies', async () => {
7890
const path = '';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pragma solidity >0.7.0;
2+
3+
import './SomeDependency.sol';
4+
5+
library SomeLibrary {
6+
7+
}

0 commit comments

Comments
 (0)