Commit 336ff739 authored by aniket-engg's avatar aniket-engg

linting for astwalker done

parent 0bdf8214
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
"extends": "../../.eslintrc", "extends": "../../.eslintrc",
"rules": { "rules": {
"@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/prefer-namespace-keyword": "off" "@typescript-eslint/prefer-namespace-keyword": "off",
"no-unused-vars": "off"
}, },
"ignorePatterns": ["!**/*"] "ignorePatterns": ["!**/*"]
} }
This diff is collapsed.
import { isAstNode, isYulAstNode, AstWalker } from './astWalker'; import { isAstNode, isYulAstNode, AstWalker } from './astWalker'
import { AstNode, LineColPosition, LineColRange, Location } from "./types"; import { AstNode, LineColPosition, LineColRange, Location } from './types'
import { util } from "@remix-project/remix-lib"; import { util } from '@remix-project/remix-lib'
export declare interface SourceMappings { export declare interface SourceMappings {
// eslint-disable-next-line @typescript-eslint/no-misused-new // eslint-disable-next-line @typescript-eslint/no-misused-new
...@@ -12,12 +12,12 @@ export declare interface SourceMappings { ...@@ -12,12 +12,12 @@ export declare interface SourceMappings {
* *
* @param offset The character offset to convert. * @param offset The character offset to convert.
*/ */
export function lineColPositionFromOffset(offset: number, lineBreaks: Array<number>): LineColPosition { export function lineColPositionFromOffset (offset: number, lineBreaks: Array<number>): LineColPosition {
let line: number = util.findLowerBound(offset, lineBreaks); let line: number = util.findLowerBound(offset, lineBreaks)
if (lineBreaks[line] !== offset) { if (lineBreaks[line] !== offset) {
line += 1; line += 1
} }
const beginColumn = line === 0 ? 0 : (lineBreaks[line - 1] + 1); const beginColumn = line === 0 ? 0 : (lineBreaks[line - 1] + 1)
return <LineColPosition>{ return <LineColPosition>{
line: line + 1, line: line + 1,
character: (offset - beginColumn) + 1 character: (offset - beginColumn) + 1
...@@ -30,11 +30,11 @@ export function lineColPositionFromOffset(offset: number, lineBreaks: Array<numb ...@@ -30,11 +30,11 @@ export function lineColPositionFromOffset(offset: number, lineBreaks: Array<numb
* *
* @param astNode The object to convert. * @param astNode The object to convert.
*/ */
export function sourceLocationFromAstNode(astNode: AstNode): Location | null { export function sourceLocationFromAstNode (astNode: AstNode): Location | null {
if (isAstNode(astNode) && isYulAstNode(astNode) && astNode.src) { if (isAstNode(astNode) && isYulAstNode(astNode) && astNode.src) {
return sourceLocationFromSrc(astNode.src) return sourceLocationFromSrc(astNode.src)
} }
return null; return null
} }
/** /**
...@@ -45,7 +45,7 @@ export function sourceLocationFromAstNode(astNode: AstNode): Location | null { ...@@ -45,7 +45,7 @@ export function sourceLocationFromAstNode(astNode: AstNode): Location | null {
* @param src A solc "src" field. * @param src A solc "src" field.
* @returns {Location} * @returns {Location}
*/ */
export function sourceLocationFromSrc(src: string): Location { export function sourceLocationFromSrc (src: string): Location {
const split = src.split(':') const split = src.split(':')
return <Location>{ return <Location>{
start: parseInt(split[0], 10), start: parseInt(split[0], 10),
...@@ -59,20 +59,19 @@ export function sourceLocationFromSrc(src: string): Location { ...@@ -59,20 +59,19 @@ export function sourceLocationFromSrc(src: string): Location {
* includng "src' information. * includng "src' information.
*/ */
export class SourceMappings { export class SourceMappings {
readonly source: string; readonly source: string;
readonly lineBreaks: Array<number>; readonly lineBreaks: Array<number>;
constructor(source: string) { constructor (source: string) {
this.source = source; this.source = source
// Create a list of line offsets which will be used to map between // Create a list of line offsets which will be used to map between
// character offset and line/column positions. // character offset and line/column positions.
const lineBreaks: Array<number> = []; const lineBreaks: Array<number> = []
for (let pos = source.indexOf('\n'); pos >= 0; pos = source.indexOf('\n', pos + 1)) { for (let pos = source.indexOf('\n'); pos >= 0; pos = source.indexOf('\n', pos + 1)) {
lineBreaks.push(pos) lineBreaks.push(pos)
} }
this.lineBreaks = lineBreaks; this.lineBreaks = lineBreaks
}; };
/** /**
...@@ -81,23 +80,23 @@ export class SourceMappings { ...@@ -81,23 +80,23 @@ export class SourceMappings {
* @param astNodeType Type of node to return or null. * @param astNodeType Type of node to return or null.
* @param position Character offset where AST node should be located. * @param position Character offset where AST node should be located.
*/ */
nodesAtPosition(astNodeType: string | null, position: Location, ast: AstNode): Array<AstNode> { nodesAtPosition (astNodeType: string | null, position: Location, ast: AstNode): Array<AstNode> {
const astWalker = new AstWalker() const astWalker = new AstWalker()
const found: Array<AstNode> = []; const found: Array<AstNode> = []
const callback = function(node: AstNode): boolean { const callback = function (node: AstNode): boolean {
const nodeLocation = sourceLocationFromAstNode(node); const nodeLocation = sourceLocationFromAstNode(node)
if (nodeLocation && if (nodeLocation &&
nodeLocation.start == position.start && nodeLocation.start === position.start &&
nodeLocation.length == position.length) { nodeLocation.length === position.length) {
if (!astNodeType || astNodeType === node.nodeType) { if (!astNodeType || astNodeType === node.nodeType) {
found.push(node) found.push(node)
} }
} }
return true; return true
} }
astWalker.walkFull(ast, callback); astWalker.walkFull(ast, callback)
return found; return found
} }
/** /**
...@@ -106,25 +105,25 @@ export class SourceMappings { ...@@ -106,25 +105,25 @@ export class SourceMappings {
* @param astNodeType nodeType that a found ASTNode must be. Use "null" if any ASTNode can match. * @param astNodeType nodeType that a found ASTNode must be. Use "null" if any ASTNode can match.
* @param sourceLocation "src" location that the AST node must match. * @param sourceLocation "src" location that the AST node must match.
*/ */
findNodeAtSourceLocation(astNodeType: string | undefined, sourceLocation: Location, ast: AstNode | null): AstNode | null { findNodeAtSourceLocation (astNodeType: string | undefined, sourceLocation: Location, ast: AstNode | null): AstNode | null {
const astWalker = new AstWalker() const astWalker = new AstWalker()
let found = null; let found = null
/* FIXME: Looking at AST walker code, /* FIXME: Looking at AST walker code,
I don't understand a need to return a boolean. */ I don't understand a need to return a boolean. */
const callback = function(node: AstNode) { const callback = function (node: AstNode) {
const nodeLocation = sourceLocationFromAstNode(node); const nodeLocation = sourceLocationFromAstNode(node)
if (nodeLocation && if (nodeLocation &&
nodeLocation.start == sourceLocation.start && nodeLocation.start === sourceLocation.start &&
nodeLocation.length == sourceLocation.length) { nodeLocation.length === sourceLocation.length) {
if (astNodeType == undefined || astNodeType === node.nodeType) { if (astNodeType === undefined || astNodeType === node.nodeType) {
found = node; found = node
} }
} }
return true; return true
} }
astWalker.walkFull(ast, callback); astWalker.walkFull(ast, callback)
return found; return found
} }
/** /**
...@@ -132,8 +131,8 @@ export class SourceMappings { ...@@ -132,8 +131,8 @@ export class SourceMappings {
* *
* @param src Solc "src" object containing attributes {source} and {length}. * @param src Solc "src" object containing attributes {source} and {length}.
*/ */
srcToLineColumnRange(src: string): LineColRange { srcToLineColumnRange (src: string): LineColRange {
const sourceLocation = sourceLocationFromSrc(src); const sourceLocation = sourceLocationFromSrc(src)
if (sourceLocation.start >= 0 && sourceLocation.length >= 0) { if (sourceLocation.start >= 0 && sourceLocation.length >= 0) {
return <LineColRange>{ return <LineColRange>{
start: lineColPositionFromOffset(sourceLocation.start, this.lineBreaks), start: lineColPositionFromOffset(sourceLocation.start, this.lineBreaks),
...@@ -146,5 +145,4 @@ export class SourceMappings { ...@@ -146,5 +145,4 @@ export class SourceMappings {
} }
} }
} }
} }
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
export interface Location { export interface Location {
start: number; start: number;
length: number; length: number;
file: number; // Would it be clearer to call this a file index? file: number; // Would it be clearer to call this a file index?
} }
// This is intended to be compatibile with VScode's Position. // This is intended to be compatibile with VScode's Position.
...@@ -31,7 +31,7 @@ export interface Node { ...@@ -31,7 +31,7 @@ export interface Node {
export interface AstNode { export interface AstNode {
/* The following fields are essential, and indicates an that object /* The following fields are essential, and indicates an that object
is an AST node. */ is an AST node. */
id: number; // This is unique across all nodes in an AST tree id: number; // This is unique across all nodes in an AST tree
nodeType: string; nodeType: string;
src: string; src: string;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment