diff --git a/libs/remix-ui/solidity-compiler/src/lib/logic/flattenerUtilities.ts b/libs/remix-ui/solidity-compiler/src/lib/logic/flattenerUtilities.ts index 8df959cb4e..0f8defb4b8 100644 --- a/libs/remix-ui/solidity-compiler/src/lib/logic/flattenerUtilities.ts +++ b/libs/remix-ui/solidity-compiler/src/lib/logic/flattenerUtilities.ts @@ -1,15 +1,16 @@ import type { CompilationSource, AstNode } from '@remix-project/remix-solidity' -const IMPORT_SOLIDITY_REGEX = /^\s*import(\s+).*$/gm; -const SPDX_SOLIDITY_REGEX = /^\s*\/\/ SPDX-License-Identifier:.*$/gm; +const IMPORT_SOLIDITY_REGEX = /^\s*import(\s+).*$/gm +const SPDX_SOLIDITY_REGEX = /^\s*\/\/ SPDX-License-Identifier:.*$/gm +const PRAGMA_SOLIDITY_REGEX = /^pragma\s+.*$/gm type Visited = { [key: string]: number } export function getDependencyGraph(ast: { [name: string]: CompilationSource }, target: string, remappings: string[], order: string[]) { - const graph = tsort(); - const visited = {}; - visited[target] = 1; - _traverse(graph, visited, ast, target, remappings, order); - return graph; + const graph = tsort() + const visited = {} + visited[target] = 1 + _traverse(graph, visited, ast, target, remappings, order) + return graph } export function concatSourceFiles(files: any[], sources: any, order: string[]) { @@ -20,8 +21,9 @@ export function concatSourceFiles(files: any[], sources: any, order: string[]) { const source = sources[file].content const sourceWithoutImport = source.replace(IMPORT_SOLIDITY_REGEX, '') const sourceWithoutSPDX = sourceWithoutImport.replace(SPDX_SOLIDITY_REGEX, '') + const sourceWithoutDuplicatePragma = sourceWithoutSPDX.replace(PRAGMA_SOLIDITY_REGEX, '') concat += `\n// File: ${file}\n\n` - concat += sourceWithoutSPDX + concat += sourceWithoutDuplicatePragma } } }) @@ -31,15 +33,15 @@ export function concatSourceFiles(files: any[], sources: any, order: string[]) { function _traverse(graph: Graph, visited: Visited, ast: { [name: string]: CompilationSource }, name: string, remappings: string[], order: string[]) { let currentAst = null currentAst = ast[name].ast - const dependencies = _getDependencies(currentAst); + const dependencies = _getDependencies(currentAst) for (const dependency of dependencies) { - const path = resolve(name, dependency, remappings); + const path = resolve(name, dependency, remappings) if (path in visited) { continue; // fixes wrong ordering of source in flattened file } - visited[path] = 1; - graph.add(name, path); - _traverse(graph, visited, ast, path, remappings, order); + visited[path] = 1 + graph.add(name, path) + _traverse(graph, visited, ast, path, remappings, order) } order.push(name) }