Skip to content

Commit

Permalink
Merge pull request #91 from solidity-parser/updating-dependencies
Browse files Browse the repository at this point in the history
Updating dependencies
  • Loading branch information
fvictorio authored Aug 21, 2023
2 parents 4b1ec7a + d358a53 commit a3228b5
Show file tree
Hide file tree
Showing 12 changed files with 3,443 additions and 2,822 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"semi": false,
"singleQuote": true
"singleQuote": true,
"trailingComma": "es5"
}
6,114 changes: 3,358 additions & 2,756 deletions package-lock.json

Large diffs are not rendered by default.

46 changes: 23 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,34 @@
"antlr4ts": "^0.5.0-alpha.4"
},
"devDependencies": {
"@types/chai": "^4.2.16",
"@types/mocha": "^8.2.2",
"@types/node": "^14.14.41",
"@typescript-eslint/eslint-plugin": "^4.9.0",
"@typescript-eslint/parser": "^4.9.0",
"antlr4": "^4.9.0",
"@types/chai": "^4.3.5",
"@types/mocha": "^10.0.1",
"@types/node": "^20.5.0",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@typescript-eslint/parser": "^6.4.0",
"antlr4": "^4.9.2",
"antlr4ts-cli": "^0.5.0-alpha.4",
"assert": "^2.0.0",
"chai": "^4.2.0",
"chai": "^4.3.7",
"esbuild": "^0.11.13",
"esbuild-register": "^2.5.0",
"eslint": "^7.15.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-node": "^9.2.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"karma": "^6.3.2",
"karma-chrome-launcher": "^3.1.0",
"eslint": "^8.47.0",
"eslint-plugin-import": "^2.28.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-standard": "^5.0.0",
"karma": "^6.4.2",
"karma-chrome-launcher": "^3.2.0",
"karma-mocha": "^2.0.1",
"mocha": "^6.2.0",
"nyc": "^14.1.1",
"prettier": "^2.2.1",
"puppeteer": "^9.0.0",
"shx": "^0.3.3",
"ts-node": "^9.1.1",
"typescript": "^4.1.2",
"util": "^0.12.3",
"yarn": "^1.17.3"
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"prettier": "^3.0.2",
"puppeteer": "^21.0.3",
"shx": "^0.3.4",
"ts-node": "^10.9.1",
"typescript": "^5.1.6",
"util": "^0.12.5",
"yarn": "^1.22.19"
},
"nyc": {
"extension": [
Expand Down
26 changes: 14 additions & 12 deletions src/ASTBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type ASTBuilderNode = AST.ASTNode & WithMeta

export class ASTBuilder
extends AbstractParseTreeVisitor<ASTBuilderNode>
implements SolidityVisitor<ASTBuilderNode | ASTBuilderNode[]> {
implements SolidityVisitor<ASTBuilderNode | ASTBuilderNode[]>
{
public result: AST.SourceUnit | null = null
private _currentContract?: string

Expand All @@ -40,7 +41,7 @@ export class ASTBuilder
}

aggregateResult() {
return ({ type: '' } as unknown) as AST.ASTNode & WithMeta
return { type: '' } as unknown as AST.ASTNode & WithMeta
}

public visitSourceUnit(ctx: SP.SourceUnitContext): AST.SourceUnit & WithMeta {
Expand Down Expand Up @@ -552,7 +553,8 @@ export class ASTBuilder
}
} else {
// using { } for ...
const usingForObjectDirectives = usingForObjectCtx.usingForObjectDirective()
const usingForObjectDirectives =
usingForObjectCtx.usingForObjectDirective()
const functions: string[] = []
const operators: Array<string | null> = []

Expand Down Expand Up @@ -1386,7 +1388,7 @@ export class ASTBuilder
const fragments = ctx
.stringLiteral()!
.StringLiteralFragment()
.map((stringLiteralFragmentCtx: any) => {
.map((stringLiteralFragmentCtx) => {
let text = this._toText(stringLiteralFragmentCtx)!

const isUnicode = text.slice(0, 7) === 'unicode'
Expand Down Expand Up @@ -1545,7 +1547,7 @@ export class ASTBuilder
}
return [symbolIdentifier, aliasIdentifier] as [
AST.Identifier,
AST.Identifier | null
AST.Identifier | null,
]
})
} else {
Expand Down Expand Up @@ -1590,19 +1592,17 @@ export class ASTBuilder
}

public buildEventParameterList(ctx: SP.EventParameterListContext) {
return ctx.eventParameter().map((paramCtx: any) => {
return ctx.eventParameter().map((paramCtx) => {
const type = this.visit(paramCtx.typeName())
let name = null
if (paramCtx.identifier()) {
name = this._toText(paramCtx.identifier())
}
const identifier = paramCtx.identifier()
const name = identifier ? this._toText(identifier) : null

return {
type: 'VariableDeclaration',
typeName: type,
name,
isStateVar: false,
isIndexed: !!paramCtx.IndexedKeyword(0),
isIndexed: !!paramCtx.IndexedKeyword(),
}
})
}
Expand All @@ -1616,7 +1616,9 @@ export class ASTBuilder
public visitParameterList(
ctx: SP.ParameterListContext
): (AST.VariableDeclaration & WithMeta)[] {
return ctx.parameter().map((paramCtx: any) => this.visitParameter(paramCtx))
return ctx
.parameter()
.map((paramCtx) => this.visitParameter(paramCtx))
}

public visitInlineAssemblyStatement(ctx: SP.InlineAssemblyStatementContext) {
Expand Down
13 changes: 7 additions & 6 deletions src/ast-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ export const astNodeTypes = [
'CatchClause',
'FileLevelConstant',
'AssemblyMemberAccess',
'TypeDefinition'
'TypeDefinition',
] as const

export type ASTNodeTypeString = typeof astNodeTypes[number]
export type ASTNodeTypeString = (typeof astNodeTypes)[number]

export interface PragmaDirective extends BaseASTNode {
type: 'PragmaDirective'
Expand Down Expand Up @@ -156,7 +156,7 @@ export interface UsingForDeclaration extends BaseASTNode {
// will be the defined operator, or null if it's just an attached function
operators: Array<string | null>
libraryName: string | null
isGlobal: boolean;
isGlobal: boolean
}
export interface StructDefinition extends BaseASTNode {
type: 'StructDefinition'
Expand Down Expand Up @@ -508,7 +508,7 @@ export const binaryOpValues = [
'|',
'|=',
] as const
export type BinOp = typeof binaryOpValues[number]
export type BinOp = (typeof binaryOpValues)[number]

export const unaryOpValues = [
'-',
Expand All @@ -520,7 +520,7 @@ export const unaryOpValues = [
'delete',
'!',
] as const
export type UnaryOp = typeof unaryOpValues[number]
export type UnaryOp = (typeof unaryOpValues)[number]

export interface BinaryOperation extends BaseASTNode {
type: 'BinaryOperation'
Expand Down Expand Up @@ -736,7 +736,8 @@ function checkTypes() {
assignAstNodeTypeStringExit = astVisitorEnterKeyExit
assignAstNodeTypeStringExit = astVisitorExitKey

let assignAstVisitorEnterKeyExit: `${keyof ASTVisitorEnter}:exit` = astNodeTypeExit
let assignAstVisitorEnterKeyExit: `${keyof ASTVisitorEnter}:exit` =
astNodeTypeExit
assignAstVisitorEnterKeyExit = astNodeTypeStringExit
assignAstVisitorEnterKeyExit = astVisitorExitKey

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./parser";
export * from './parser'

export type { ParseOptions } from "./types";
export type { ParseOptions } from './types'
38 changes: 24 additions & 14 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { ANTLRInputStream, CommonTokenStream } from 'antlr4ts'

import { SolidityLexer } from './antlr/SolidityLexer'
import { SolidityParser } from './antlr/SolidityParser'
import { ASTNode, astNodeTypes, ASTNodeTypeString, ASTVisitor, SourceUnit } from './ast-types'
import {
ASTNode,
astNodeTypes,
ASTNodeTypeString,
ASTVisitor,
SourceUnit,
} from './ast-types'
import { ASTBuilder } from './ASTBuilder'
import ErrorListener from './ErrorListener'
import { buildTokenList } from './tokens'
Expand Down Expand Up @@ -43,10 +49,7 @@ export function tokenize(input: string, options: TokenizeOptions = {}): any {
return buildTokenList(lexer.getAllTokens(), options)
}

export function parse(
input: string,
options: ParseOptions = {}
): ParseResult {
export function parse(input: string, options: ParseOptions = {}): ParseResult {
const inputStream = new ANTLRInputStream(input)
const lexer = new SolidityLexer(inputStream)
const tokenStream = new CommonTokenStream(lexer)
Expand Down Expand Up @@ -95,16 +98,23 @@ function _isASTNode(node: unknown): node is ASTNode {
return false
}

const nodeAsAny: any = node
const nodeAsASTNode = node as ASTNode

if (Object.prototype.hasOwnProperty.call(nodeAsAny, 'type') && typeof nodeAsAny.type === "string") {
return astNodeTypes.includes(nodeAsAny.type)
if (
Object.prototype.hasOwnProperty.call(nodeAsASTNode, 'type') &&
typeof nodeAsASTNode.type === 'string'
) {
return astNodeTypes.includes(nodeAsASTNode.type)
}

return false;
return false
}

export function visit(node: unknown, visitor: ASTVisitor, nodeParent?: ASTNode): void {
export function visit(
node: unknown,
visitor: ASTVisitor,
nodeParent?: ASTNode
): void {
if (Array.isArray(node)) {
node.forEach((child) => visit(child, visitor, nodeParent))
}
Expand All @@ -114,8 +124,8 @@ export function visit(node: unknown, visitor: ASTVisitor, nodeParent?: ASTNode):
let cont = true

if (visitor[node.type] !== undefined) {
// TODO can we avoid this `as any`
cont = visitor[node.type]!(node as any, nodeParent)
// TODO can we avoid this `as never`
cont = visitor[node.type]!(node as never, nodeParent)
}

if (cont === false) return
Expand All @@ -129,7 +139,7 @@ export function visit(node: unknown, visitor: ASTVisitor, nodeParent?: ASTNode):

const selector = (node.type + ':exit') as `${ASTNodeTypeString}:exit`
if (visitor[selector] !== undefined) {
// TODO can we avoid this `as any`
visitor[selector]!(node as any, nodeParent)
// TODO can we avoid this `as never`
visitor[selector]!(node as never, nodeParent)
}
}
2 changes: 1 addition & 1 deletion src/tokens-string.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This is an indirect file to import the tokens string
// It needs to be a js file so that tsc doesn't complain

if (typeof BROWSER !== "undefined") {
if (typeof BROWSER !== 'undefined') {
module.exports = require('./antlr/Solidity.tokens')
} else {
module.exports = require('fs')
Expand Down
7 changes: 5 additions & 2 deletions src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function getTokenTypeMap() {
return tokens
.split('\n')
.map((line) => rsplit(line, '='))
.reduce((acum: any, [value, key]) => {
.reduce((acum: { [key: number]: string }, [value, key]) => {
acum[parseInt(key, 10)] = normalizeTokenType(value)
return acum
}, {})
Expand All @@ -81,7 +81,10 @@ export function buildTokenList(
if (options.loc === true) {
node.loc = {
start: { line: token.line, column: token.charPositionInLine },
end: { line: token.line, column: token.charPositionInLine + (token.text?.length ?? 0) },
end: {
line: token.line,
column: token.charPositionInLine + (token.text?.length ?? 0),
},
}
}
return node
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Token as Antlr4TsToken } from "antlr4ts";
import { Token as Antlr4TsToken } from 'antlr4ts'
export interface Node {
type: string
}

export type AntlrToken = Antlr4TsToken;
export type AntlrToken = Antlr4TsToken

export interface TokenizeOptions {
range?: boolean
Expand Down
4 changes: 3 additions & 1 deletion test/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ describe('AST', () => {
})

it('defining two operators', function () {
const ast = parseNode('using { add as +, sub as - } for Fixed18 global;')
const ast = parseNode(
'using { add as +, sub as - } for Fixed18 global;'
)
assert.deepEqual(ast, {
type: 'UsingForDeclaration',
isGlobal: true,
Expand Down
4 changes: 2 additions & 2 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('#parse', function () {

it('supports tolerant mode', function () {
const source = 'contract {'
const root: any = parser.parse(source, { tolerant: true })
assert.isAbove(root.errors.length, 0)
const root = parser.parse(source, { tolerant: true })
assert.isAbove(root.errors!.length, 0)
})

it('supports loc', function () {
Expand Down

0 comments on commit a3228b5

Please sign in to comment.