Skip to content

Commit

Permalink
Typescript rewrite (#41)
Browse files Browse the repository at this point in the history
* Use typescript as a compiler for js

* Add tslint

* Rewrite waffle.js -> waffle.ts

* Rewrite utils.js -> utils.ts

* Rewrite link.js -> link.ts

* Rewrite compiler.js -> compiler.ts

* Rewrite matchers.js -> matchers.ts

* Rewrite wrappers in typescript

* Rewrite buildTestContracts.js -> buildTestContracts.ts

* Rewrite wrapper tests in typescript

* Rewrite compiler tests in typescript

* Rewrite fixture, link and utils tests in typescript

* Make build config extend default config
  • Loading branch information
sz-piotr authored and marekkirejczyk committed Jan 13, 2019
1 parent 395d550 commit f8211c7
Show file tree
Hide file tree
Showing 38 changed files with 637 additions and 1,160 deletions.
116 changes: 0 additions & 116 deletions .eslintrc.json

This file was deleted.

43 changes: 27 additions & 16 deletions lib/compiler.js → lib/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import fs from 'fs';
import path from 'path';
import defaultConfig from './config/defaultConfig';
import defaultConfig, {Config} from './config/config';
import {isDirectory, readFileContent, isWarningMessage} from './utils';
import createWrapper from './wrappers/createWrapper';
import {createWrapper, Wrapper} from './wrappers/createWrapper';

interface CompilerOptions {
wrapper?: Wrapper;
overrideProcess?: NodeJS.Process;
overrideConsole?: Console;
}

export default class Compiler {
constructor(config = {}, {wrapper = null, overrideProcess = process, overrideConsole = console} = {}) {
private config: Config;
private process: NodeJS.Process;
private console: Console;
private wrapper: Wrapper;

constructor(config: Partial<Config> = {}, options: CompilerOptions = {}) {
this.config = {...defaultConfig, ...config};
this.process = overrideProcess;
this.console = overrideConsole;
this.wrapper = wrapper || createWrapper(this.config);
this.process = options.overrideProcess;
this.console = options.overrideConsole;
this.wrapper = options.wrapper || createWrapper(this.config);
}

async findInputFiles(sourcesPath) {
public async findInputFiles(sourcesPath: string) {
const dirs = [sourcesPath];
const inputFiles = [];
const inputFiles: string[] = [];
while (dirs.length) {
const dir = dirs.pop();
const files = fs.readdirSync(dir);
Expand All @@ -30,7 +41,7 @@ export default class Compiler {
return inputFiles;
}

findImports(file) {
public findImports(file: string) {
const libPath = path.join(this.config.npmPath, file);
if (fs.existsSync(file)) {
const contents = readFileContent(file);
Expand All @@ -42,12 +53,12 @@ export default class Compiler {
return {error: `File not found: ${file}`};
}

async doCompile() {
public async doCompile() {
const sourcesFiles = await this.findInputFiles(this.config.sourcesPath);
return this.wrapper.compile(sourcesFiles, this.findImports.bind(this));
}

anyNonWarningErrors(errors) {
private anyNonWarningErrors(errors?: any[]) {
if (!errors) {
return false;
}
Expand All @@ -59,14 +70,14 @@ export default class Compiler {
return false;
}

toFormattedMessage(error) {
private toFormattedMessage(error: any) {
return typeof error === 'string' ? error : error.formattedMessage;
}

async compile() {
public async compile() {
const output = await this.doCompile();
if (output.errors) {
const errors = output.errors.map((error) => this.toFormattedMessage(error)).join('\n');
const errors = output.errors.map((error: any) => this.toFormattedMessage(error)).join('\n');
this.console.error(errors);
}
if (this.anyNonWarningErrors(output.errors)) {
Expand All @@ -77,11 +88,11 @@ export default class Compiler {
}
}

export async function compile(configPath, options = {}) {
export async function compile(configPath: string, options = {}) {
try {
let config = {};
if (configPath) {
const contents = fs.readFileSync(configPath);
const contents = fs.readFileSync(configPath, 'utf-8');
config = JSON.parse(contents);
}
const compiler = new Compiler(config, options);
Expand Down
16 changes: 16 additions & 0 deletions lib/config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface Config {
sourcesPath: string;
targetPath?: string;
npmPath: string;
compiler?: 'native' | 'dockerized-solc' | 'solcjs';
'docker-tag'?: string;
solcVersion?: string;
}

const defaultConfig: Config = {
sourcesPath: './contracts',
targetPath: './build',
npmPath: 'node_modules'
};

export default defaultConfig;
File renamed without changes.
7 changes: 0 additions & 7 deletions lib/config/defaultConfig.js

This file was deleted.

File renamed without changes.
24 changes: 20 additions & 4 deletions lib/link.js → lib/link.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import {utils} from 'ethers';

export const linkSolidity4 = (contract, libraryName, libraryAddress) => {
export interface LinkableContract {
evm: {
bytecode: {
object: any
}
};
}

export function linkSolidity4(
contract: LinkableContract,
libraryName: string,
libraryAddress: string
) {
const address = libraryAddress.replace('0x', '');
const libraryNamePrefix = libraryName.slice(0, 36);
const pattern = new RegExp(`_+${libraryNamePrefix}_+`, 'g');
if (!pattern.exec(contract.evm.bytecode.object)) {
throw new Error(`Can't link '${libraryName}'.`);
}
contract.evm.bytecode.object = contract.evm.bytecode.object.replace(pattern, address);
};
}

export const linkSolidity5 = (contract, libraryName, libraryAddress) => {
export function linkSolidity5(
contract: LinkableContract,
libraryName: string,
libraryAddress: string
) {
const address = libraryAddress.replace('0x', '');
const encodedLibraryName = utils
.solidityKeccak256(['string'], [libraryName])
Expand All @@ -21,4 +37,4 @@ export const linkSolidity5 = (contract, libraryName, libraryAddress) => {
throw new Error(`Can't link '${libraryName}'.`);
}
contract.evm.bytecode.object = bytecode.replace(pattern, address);
};
}

0 comments on commit f8211c7

Please sign in to comment.