Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c4ac4cc
Implement partial transpilation
Amxx Sep 19, 2023
06f00f1
missing newlines
Amxx Sep 19, 2023
8064ec1
use getData to mark object that are not transpiled
Amxx Sep 19, 2023
890d8de
remove non-transpiled objects from files that are transpiled
Amxx Sep 19, 2023
48d3f61
fix
Amxx Sep 20, 2023
71e16a9
lint
Amxx Sep 20, 2023
f6cd41a
remove unecessary eslint-disable
Amxx Sep 20, 2023
4b19e44
declare TransformData.importPath in the fix-import-directives file
Amxx Sep 20, 2023
bfcb1e1
fix bug when a.local === undefined
Amxx Sep 20, 2023
9b831ea
refactor import for Initializable
Amxx Sep 20, 2023
46c62ab
update test cache
Amxx Sep 20, 2023
d216e47
change linting of the produced solidity code
Amxx Sep 20, 2023
904500b
Apply suggestions from code review
Amxx Sep 21, 2023
d4faaf8
Update src/transformations/rename-identifiers.ts
Amxx Sep 21, 2023
7f53af6
Rename importPath to importFromPeer
Amxx Sep 21, 2023
94c1892
move partial transpilation form class Transform to function transpile
Amxx Sep 21, 2023
8d5a339
add helper function excludeAndImportPathsForPeer
Amxx Sep 21, 2023
1bc7930
Update index.ts
Amxx Sep 21, 2023
2b95625
change exclude return
Amxx Sep 23, 2023
b12b69d
renames
frangio Sep 25, 2023
7d7cb3f
use for..of
frangio Sep 25, 2023
fd1302d
move variable definition inside
frangio Sep 25, 2023
0a3b6cf
Apply suggestions from code review
Amxx Sep 25, 2023
30ab602
resolve any imported node
Amxx Sep 25, 2023
be412df
lint
Amxx Sep 25, 2023
b806261
Switch node type
Amxx Sep 25, 2023
13ddd81
peer import constant variable declarations
Amxx Sep 25, 2023
e2456ab
Update peer-import.ts
Amxx Sep 25, 2023
3037c78
explicit comparaison against undefined
Amxx Sep 25, 2023
eb9a435
minimize change
Amxx Sep 25, 2023
d4a70dd
Update src/transformations/peer-import.ts
Amxx Sep 25, 2023
1f7c353
add assertion
frangio Sep 25, 2023
ea04008
add struct to test
frangio Sep 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move partial transpilation form class Transform to function transpile
  • Loading branch information
Amxx committed Sep 21, 2023
commit 94c1892cc7795e96ff025c9b6495d27091a5e620
4 changes: 2 additions & 2 deletions src/ast-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ASTResolver {

constructor(
readonly output: SolcOutput,
readonly exclude?: (source: string) => boolean,
readonly exclude?: (source: string) => boolean | 'soft',
) {
this.deref = astDereferencer(output);
}
Expand All @@ -28,7 +28,7 @@ export class ASTResolver {
resolveNode<T extends ExtendedNodeType>(nodeType: T, id: number): ExtendedNodeTypeMap[T] {
const { node, sourceUnit } = this.deref.withSourceUnit(nodeType, id);
const source = sourceUnit.absolutePath;
if (this.exclude?.(source)) {
if (this.exclude?.(source) == true) {
throw new Error(`Symbol #${id} was imported from an excluded file (${source})`);
} else {
return node;
Expand Down
35 changes: 32 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import fs from 'fs';
import { mapValues } from 'lodash';
import { minimatch } from 'minimatch';

import { findAll } from 'solidity-ast/utils';
import { Node } from 'solidity-ast/node';

import { matcher } from './utils/matcher';
import { renamePath, isRenamed } from './rename';
import { SolcOutput, SolcInput } from './solc/input-output';
import { Transform } from './transform';
import { Transform, TransformData } from './transform';
import { generateWithInit } from './generate-with-init';
import { findAlreadyInitializable } from './find-already-initializable';

Expand Down Expand Up @@ -76,10 +79,13 @@ export async function transpile(
paths: Paths,
options: TranspileOptions = {},
): Promise<OutputFile[]> {
const nodeData: { node: Node; data: Partial<TransformData> }[] = [];

const outputPaths = getExtraOutputPaths(paths, options);
const alreadyInitializable = findAlreadyInitializable(solcOutput, options.initializablePath);

const excludeSet = new Set([...alreadyInitializable, ...Object.values(outputPaths)]);
const peerExcludedSet = new Set<string>();
const excludeMatch = matcher(options.exclude ?? []);

const namespaceInclude = (source: string) => {
Expand All @@ -88,11 +94,34 @@ export async function transpile(
return namespaced && !namespaceExclude.some(p => minimatch(source, p));
};

if (options.peerProject !== undefined) {
for (const [source, { ast }] of Object.entries(solcOutput.sources)) {
const importFromPeer = path.join(options.peerProject, source);
const contracts = [...findAll('ContractDefinition', ast)];

// populate nodeData
nodeData.push(
...contracts
.filter(node => node.contractKind !== 'contract')
.map(node => ({ node, data: { importFromPeer } })),
);

// soft exclude files that have no
if (contracts.every(({ contractKind }) => contractKind !== 'contract')) {
peerExcludedSet.add(source);
}
}
}

const transform = new Transform(solcInput, solcOutput, {
exclude: source => excludeSet.has(source) || (excludeMatch(source) ?? isRenamed(source)),
peerProject: options.peerProject,
exclude: source =>
peerExcludedSet.has(source)
? 'soft'
: excludeSet.has(source) || (excludeMatch(source) ?? isRenamed(source)),
});

nodeData.forEach(({ node, data }) => Object.assign(transform.getData(node), data));

transform.apply(renameIdentifiers);
transform.apply(renameContractDefinition);
transform.apply(renameInheritdoc);
Expand Down
26 changes: 5 additions & 21 deletions src/transform.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { mapValues } from 'lodash';
import path from 'path';
import { SourceUnit } from 'solidity-ast';
import { findAll } from 'solidity-ast/utils';
import { Node } from 'solidity-ast/node';
import { SolcInput, SolcOutput } from './solc/input-output';
import { srcDecoder, SrcDecoder } from './solc/src-decoder';
Expand Down Expand Up @@ -46,8 +44,7 @@ interface TransformState {
}

interface TransformOptions {
exclude?: (source: string) => boolean;
peerProject?: string;
exclude?: (source: string) => boolean | 'soft';
}

export class Transform {
Expand All @@ -61,30 +58,17 @@ export class Transform {
readonly getLayout: LayoutGetter;
readonly resolver: ASTResolver;

constructor(input: SolcInput, output: SolcOutput, options?: TransformOptions) {
constructor(input: SolcInput, output: SolcOutput, options: TransformOptions = {}) {
this.decodeSrc = srcDecoder(output);
this.getLayout = layoutGetter(output);
this.resolver = new ASTResolver(output, options?.exclude);
this.resolver = new ASTResolver(output, options.exclude);

for (const source in input.sources) {
if (options?.exclude?.(source)) {
const excluded = options.exclude?.(source);
if (excluded === true || excluded === 'soft') {
continue;
}

if (options?.peerProject) {
const contracts = [...findAll('ContractDefinition', output.sources[source].ast)];

for (const noContract of contracts.filter(
({ contractKind }) => contractKind !== 'contract',
)) {
this.getData(noContract).importFromPeer = path.join(options.peerProject, source);
}

if (contracts.every(({ contractKind }) => contractKind !== 'contract')) {
continue;
}
}

const s = input.sources[source];
if (!('content' in s)) {
throw new Error(`Missing content for ${source}`);
Expand Down
4 changes: 3 additions & 1 deletion src/transformations/fix-import-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export function fixImportDirectives(withPeerProject?: boolean) {
imports[importPath] ??= [];
imports[importPath].push(
[
contract !== undefined && importFromPeer === undefined ? renameContract(a.foreign.name) : a.foreign.name,
contract !== undefined && importFromPeer === undefined
? renameContract(a.foreign.name)
: a.foreign.name,
[null, undefined, a.foreign.name].includes(a.local) ? '' : ` as ${a.local}`,
].join(''),
);
Expand Down