Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Use fast-solidity-parser #175

Merged
merged 5 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions packages/discovery/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @l2beat/discovery

## 0.47.0

### Minor Changes

- Use fast-solidity-parser

## 0.46.11

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/discovery/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@l2beat/discovery",
"description": "L2Beat discovery - engine & tooling utilized for keeping an eye on L2s",
"version": "0.46.11",
"version": "0.47.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
Expand All @@ -21,7 +21,7 @@
"dependencies": {
"@l2beat/backend-tools": "^0.5.1",
"@l2beat/discovery-types": "^0.8.1",
"@solidity-parser/parser": "^0.18.0",
"@mradomski/fast-solidity-parser": "0.1.1",
"chalk": "^4.1.2",
"deep-diff": "^1.0.2",
"dotenv": "^16.0.3",
Expand Down
27 changes: 16 additions & 11 deletions packages/discovery/src/flatten/ParsedFilesManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { assert } from '@l2beat/backend-tools'
import { parse } from '@solidity-parser/parser'
// eslint-disable-next-line import/no-unresolved
import type * as AST from '@solidity-parser/parser/dist/src/ast-types'
import type * as AST from '@mradomski/fast-solidity-parser'
import { parse } from '@mradomski/fast-solidity-parser'
import * as posix from 'path'

import { getASTIdentifiers } from './getASTIdentifiers'
Expand Down Expand Up @@ -67,6 +66,7 @@ export interface Remapping {
}

export interface ParsedFile extends FileContent {
normalizedPath: string
rootASTNode: ParseResult

topLevelDeclarations: TopLevelDeclaration[]
Expand All @@ -88,13 +88,17 @@ export class ParsedFilesManager {
const result = new ParsedFilesManager()
const remappings = decodeRemappings(remappingStrings)

result.files = files.map(({ path, content }) => ({
path: resolveRemappings(path, remappings),
content,
rootASTNode: parse(content, { range: true }),
topLevelDeclarations: [],
importDirectives: [],
}))
result.files = files.map(({ path, content }) => {
const remappedPath = resolveRemappings(path, remappings)
return {
path: remappedPath,
normalizedPath: posix.normalize(remappedPath),
content,
rootASTNode: parse(content, { range: true }),
topLevelDeclarations: [],
importDirectives: [],
}
})

// Pass 1: Find all contract declarations
for (const file of result.files) {
Expand Down Expand Up @@ -402,9 +406,10 @@ export class ParsedFilesManager {
? posix.join(posix.dirname(fromFile.path), importPath)
: importPath

const normalizedPath = posix.normalize(resolvedPath)
const matchingFile = findOne(
this.files,
(f) => posix.normalize(f.path) === posix.normalize(resolvedPath),
(f) => f.normalizedPath === normalizedPath,
)
assert(
matchingFile !== undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/discovery/src/flatten/flattenStartingFrom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ describe(flattenStartingFrom.name, () => {

const ROOT_PARSED_FILE: Omit<ParsedFile, 'rootASTNode'> = {
path: 'path',
normalizedPath: 'path',
content: ROOT_FILE_SOURCE,
topLevelDeclarations: [ROOT_CONTRACT, LIBRARY_CONTRACT],
importDirectives: [],
Expand Down
2 changes: 1 addition & 1 deletion packages/discovery/src/flatten/getASTIdentifiers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse } from '@solidity-parser/parser'
import { parse } from '@mradomski/fast-solidity-parser'
import { expect } from 'earl'

import { getASTIdentifiers } from './getASTIdentifiers'
Expand Down
6 changes: 1 addition & 5 deletions packages/discovery/src/flatten/getASTIdentifiers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// TODO(radomski): The parser does not expose the AST types for SOME reason.
// Either we ignore this error or we fork the parser and expose the types.
/* eslint-disable import/no-unresolved */
import type * as AST from '@solidity-parser/parser/dist/src/ast-types'
/* eslint-enable */
import type * as AST from '@mradomski/fast-solidity-parser'

export function getASTIdentifiers(baseNode: AST.BaseASTNode | null): string[] {
if (baseNode === null) {
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,11 @@
semver "^7.3.5"
tar "^6.1.11"

"@mradomski/[email protected]":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@mradomski/fast-solidity-parser/-/fast-solidity-parser-0.1.1.tgz#9c38003731bec09c473f54d2677a82e548a00c0b"
integrity sha512-BnX+UKswinYed/yFgH9zwYNNBvUXJu32vJkxwnkZuPcV/YR9TDzMgjAOhiq8Vj77wDcfHHhyA0/MdIMhkd92PQ==

"@nodelib/[email protected]":
version "2.1.5"
resolved "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz"
Expand Down Expand Up @@ -877,11 +882,6 @@
dependencies:
"@sinonjs/commons" "^3.0.0"

"@solidity-parser/parser@^0.18.0":
version "0.18.0"
resolved "https://registry.yarnpkg.com/@solidity-parser/parser/-/parser-0.18.0.tgz#8e77a02a09ecce957255a2f48c9a7178ec191908"
integrity sha512-yfORGUIPgLck41qyN7nbwJRAx17/jAIXCTanHOJZhB6PJ1iAk/84b/xlsVKFSyNyLXIj0dhppoE0+CRws7wlzA==

"@tootallnate/once@1":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82"
Expand Down
Loading