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

Commit 0230937

Browse files
Use fast-solidity-parser (#175)
* Use fast-solidity-parser * Update to v0.1.1 * Cache calls to posix.normalize() since it's expensive * Fix type error in test * Changeset
1 parent 35a0452 commit 0230937

File tree

7 files changed

+32
-24
lines changed

7 files changed

+32
-24
lines changed

packages/discovery/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @l2beat/discovery
22

3+
## 0.47.0
4+
5+
### Minor Changes
6+
7+
- Use fast-solidity-parser
8+
39
## 0.46.11
410

511
### Patch Changes

packages/discovery/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@l2beat/discovery",
33
"description": "L2Beat discovery - engine & tooling utilized for keeping an eye on L2s",
4-
"version": "0.46.11",
4+
"version": "0.47.0",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"bin": {
@@ -21,7 +21,7 @@
2121
"dependencies": {
2222
"@l2beat/backend-tools": "^0.5.1",
2323
"@l2beat/discovery-types": "^0.8.1",
24-
"@solidity-parser/parser": "^0.18.0",
24+
"@mradomski/fast-solidity-parser": "0.1.1",
2525
"chalk": "^4.1.2",
2626
"deep-diff": "^1.0.2",
2727
"dotenv": "^16.0.3",

packages/discovery/src/flatten/ParsedFilesManager.ts

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { assert } from '@l2beat/backend-tools'
2-
import { parse } from '@solidity-parser/parser'
3-
// eslint-disable-next-line import/no-unresolved
4-
import type * as AST from '@solidity-parser/parser/dist/src/ast-types'
2+
import type * as AST from '@mradomski/fast-solidity-parser'
3+
import { parse } from '@mradomski/fast-solidity-parser'
54
import * as posix from 'path'
65

76
import { getASTIdentifiers } from './getASTIdentifiers'
@@ -67,6 +66,7 @@ export interface Remapping {
6766
}
6867

6968
export interface ParsedFile extends FileContent {
69+
normalizedPath: string
7070
rootASTNode: ParseResult
7171

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

91-
result.files = files.map(({ path, content }) => ({
92-
path: resolveRemappings(path, remappings),
93-
content,
94-
rootASTNode: parse(content, { range: true }),
95-
topLevelDeclarations: [],
96-
importDirectives: [],
97-
}))
91+
result.files = files.map(({ path, content }) => {
92+
const remappedPath = resolveRemappings(path, remappings)
93+
return {
94+
path: remappedPath,
95+
normalizedPath: posix.normalize(remappedPath),
96+
content,
97+
rootASTNode: parse(content, { range: true }),
98+
topLevelDeclarations: [],
99+
importDirectives: [],
100+
}
101+
})
98102

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

409+
const normalizedPath = posix.normalize(resolvedPath)
405410
const matchingFile = findOne(
406411
this.files,
407-
(f) => posix.normalize(f.path) === posix.normalize(resolvedPath),
412+
(f) => f.normalizedPath === normalizedPath,
408413
)
409414
assert(
410415
matchingFile !== undefined,

packages/discovery/src/flatten/flattenStartingFrom.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ describe(flattenStartingFrom.name, () => {
6363

6464
const ROOT_PARSED_FILE: Omit<ParsedFile, 'rootASTNode'> = {
6565
path: 'path',
66+
normalizedPath: 'path',
6667
content: ROOT_FILE_SOURCE,
6768
topLevelDeclarations: [ROOT_CONTRACT, LIBRARY_CONTRACT],
6869
importDirectives: [],

packages/discovery/src/flatten/getASTIdentifiers.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parse } from '@solidity-parser/parser'
1+
import { parse } from '@mradomski/fast-solidity-parser'
22
import { expect } from 'earl'
33

44
import { getASTIdentifiers } from './getASTIdentifiers'

packages/discovery/src/flatten/getASTIdentifiers.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
// TODO(radomski): The parser does not expose the AST types for SOME reason.
2-
// Either we ignore this error or we fork the parser and expose the types.
3-
/* eslint-disable import/no-unresolved */
4-
import type * as AST from '@solidity-parser/parser/dist/src/ast-types'
5-
/* eslint-enable */
1+
import type * as AST from '@mradomski/fast-solidity-parser'
62

73
export function getASTIdentifiers(baseNode: AST.BaseASTNode | null): string[] {
84
if (baseNode === null) {

yarn.lock

+5-5
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,11 @@
821821
semver "^7.3.5"
822822
tar "^6.1.11"
823823

824+
"@mradomski/fast-solidity-parser@0.1.1":
825+
version "0.1.1"
826+
resolved "https://registry.yarnpkg.com/@mradomski/fast-solidity-parser/-/fast-solidity-parser-0.1.1.tgz#9c38003731bec09c473f54d2677a82e548a00c0b"
827+
integrity sha512-BnX+UKswinYed/yFgH9zwYNNBvUXJu32vJkxwnkZuPcV/YR9TDzMgjAOhiq8Vj77wDcfHHhyA0/MdIMhkd92PQ==
828+
824829
"@nodelib/fs.scandir@2.1.5":
825830
version "2.1.5"
826831
resolved "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz"
@@ -877,11 +882,6 @@
877882
dependencies:
878883
"@sinonjs/commons" "^3.0.0"
879884

880-
"@solidity-parser/parser@^0.18.0":
881-
version "0.18.0"
882-
resolved "https://registry.yarnpkg.com/@solidity-parser/parser/-/parser-0.18.0.tgz#8e77a02a09ecce957255a2f48c9a7178ec191908"
883-
integrity sha512-yfORGUIPgLck41qyN7nbwJRAx17/jAIXCTanHOJZhB6PJ1iAk/84b/xlsVKFSyNyLXIj0dhppoE0+CRws7wlzA==
884-
885885
"@tootallnate/once@1":
886886
version "1.1.2"
887887
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82"

0 commit comments

Comments
 (0)