add regex to fix duplicate pragma lines in flattened contract

pull/4814/head
Joseph Izang 8 months ago
parent 1ee0820e12
commit 2813fad8b2
  1. 28
      libs/remix-ui/solidity-compiler/src/lib/logic/flattenerUtilities.ts

@ -1,15 +1,16 @@
import type { CompilationSource, AstNode } from '@remix-project/remix-solidity' import type { CompilationSource, AstNode } from '@remix-project/remix-solidity'
const IMPORT_SOLIDITY_REGEX = /^\s*import(\s+).*$/gm; const IMPORT_SOLIDITY_REGEX = /^\s*import(\s+).*$/gm
const SPDX_SOLIDITY_REGEX = /^\s*\/\/ SPDX-License-Identifier:.*$/gm; const SPDX_SOLIDITY_REGEX = /^\s*\/\/ SPDX-License-Identifier:.*$/gm
const PRAGMA_SOLIDITY_REGEX = /^pragma\s+.*$/gm
type Visited = { [key: string]: number } type Visited = { [key: string]: number }
export function getDependencyGraph(ast: { [name: string]: CompilationSource }, target: string, remappings: string[], order: string[]) { export function getDependencyGraph(ast: { [name: string]: CompilationSource }, target: string, remappings: string[], order: string[]) {
const graph = tsort(); const graph = tsort()
const visited = {}; const visited = {}
visited[target] = 1; visited[target] = 1
_traverse(graph, visited, ast, target, remappings, order); _traverse(graph, visited, ast, target, remappings, order)
return graph; return graph
} }
export function concatSourceFiles(files: any[], sources: any, order: string[]) { 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 source = sources[file].content
const sourceWithoutImport = source.replace(IMPORT_SOLIDITY_REGEX, '') const sourceWithoutImport = source.replace(IMPORT_SOLIDITY_REGEX, '')
const sourceWithoutSPDX = sourceWithoutImport.replace(SPDX_SOLIDITY_REGEX, '') const sourceWithoutSPDX = sourceWithoutImport.replace(SPDX_SOLIDITY_REGEX, '')
const sourceWithoutDuplicatePragma = sourceWithoutSPDX.replace(PRAGMA_SOLIDITY_REGEX, '')
concat += `\n// File: ${file}\n\n` 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[]) { function _traverse(graph: Graph, visited: Visited, ast: { [name: string]: CompilationSource }, name: string, remappings: string[], order: string[]) {
let currentAst = null let currentAst = null
currentAst = ast[name].ast currentAst = ast[name].ast
const dependencies = _getDependencies(currentAst); const dependencies = _getDependencies(currentAst)
for (const dependency of dependencies) { for (const dependency of dependencies) {
const path = resolve(name, dependency, remappings); const path = resolve(name, dependency, remappings)
if (path in visited) { if (path in visited) {
continue; // fixes wrong ordering of source in flattened file continue; // fixes wrong ordering of source in flattened file
} }
visited[path] = 1; visited[path] = 1
graph.add(name, path); graph.add(name, path)
_traverse(graph, visited, ast, path, remappings, order); _traverse(graph, visited, ast, path, remappings, order)
} }
order.push(name) order.push(name)
} }

Loading…
Cancel
Save