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

linting for astwalker done

parent 0bdf8214
......@@ -2,7 +2,8 @@
"extends": "../../.eslintrc",
"rules": {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/prefer-namespace-keyword": "off"
"@typescript-eslint/prefer-namespace-keyword": "off",
"no-unused-vars": "off"
},
"ignorePatterns": ["!**/*"]
}
This diff is collapsed.
import { isAstNode, isYulAstNode, AstWalker } from './astWalker';
import { AstNode, LineColPosition, LineColRange, Location } from "./types";
import { util } from "@remix-project/remix-lib";
import { isAstNode, isYulAstNode, AstWalker } from './astWalker'
import { AstNode, LineColPosition, LineColRange, Location } from './types'
import { util } from '@remix-project/remix-lib'
export declare interface SourceMappings {
// eslint-disable-next-line @typescript-eslint/no-misused-new
......@@ -12,12 +12,12 @@ export declare interface SourceMappings {
*
* @param offset The character offset to convert.
*/
export function lineColPositionFromOffset(offset: number, lineBreaks: Array<number>): LineColPosition {
let line: number = util.findLowerBound(offset, lineBreaks);
export function lineColPositionFromOffset (offset: number, lineBreaks: Array<number>): LineColPosition {
let line: number = util.findLowerBound(offset, lineBreaks)
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>{
line: line + 1,
character: (offset - beginColumn) + 1
......@@ -30,11 +30,11 @@ export function lineColPositionFromOffset(offset: number, lineBreaks: Array<numb
*
* @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) {
return sourceLocationFromSrc(astNode.src)
}
return null;
return null
}
/**
......@@ -45,7 +45,7 @@ export function sourceLocationFromAstNode(astNode: AstNode): Location | null {
* @param src A solc "src" field.
* @returns {Location}
*/
export function sourceLocationFromSrc(src: string): Location {
export function sourceLocationFromSrc (src: string): Location {
const split = src.split(':')
return <Location>{
start: parseInt(split[0], 10),
......@@ -59,20 +59,19 @@ export function sourceLocationFromSrc(src: string): Location {
* includng "src' information.
*/
export class SourceMappings {
readonly source: string;
readonly lineBreaks: Array<number>;
constructor(source: string) {
this.source = source;
constructor (source: string) {
this.source = source
// Create a list of line offsets which will be used to map between
// 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)) {
lineBreaks.push(pos)
}
this.lineBreaks = lineBreaks;
this.lineBreaks = lineBreaks
};
/**
......@@ -81,23 +80,23 @@ export class SourceMappings {
* @param astNodeType Type of node to return or null.
* @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 found: Array<AstNode> = [];
const found: Array<AstNode> = []
const callback = function(node: AstNode): boolean {
const nodeLocation = sourceLocationFromAstNode(node);
const callback = function (node: AstNode): boolean {
const nodeLocation = sourceLocationFromAstNode(node)
if (nodeLocation &&
nodeLocation.start == position.start &&
nodeLocation.length == position.length) {
nodeLocation.start === position.start &&
nodeLocation.length === position.length) {
if (!astNodeType || astNodeType === node.nodeType) {
found.push(node)
}
}
return true;
return true
}
astWalker.walkFull(ast, callback);
return found;
astWalker.walkFull(ast, callback)
return found
}
/**
......@@ -106,25 +105,25 @@ export class SourceMappings {
* @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.
*/
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()
let found = null;
let found = null
/* FIXME: Looking at AST walker code,
I don't understand a need to return a boolean. */
const callback = function(node: AstNode) {
const nodeLocation = sourceLocationFromAstNode(node);
const callback = function (node: AstNode) {
const nodeLocation = sourceLocationFromAstNode(node)
if (nodeLocation &&
nodeLocation.start == sourceLocation.start &&
nodeLocation.length == sourceLocation.length) {
if (astNodeType == undefined || astNodeType === node.nodeType) {
found = node;
nodeLocation.start === sourceLocation.start &&
nodeLocation.length === sourceLocation.length) {
if (astNodeType === undefined || astNodeType === node.nodeType) {
found = node
}
}
return true;
return true
}
astWalker.walkFull(ast, callback);
return found;
astWalker.walkFull(ast, callback)
return found
}
/**
......@@ -132,8 +131,8 @@ export class SourceMappings {
*
* @param src Solc "src" object containing attributes {source} and {length}.
*/
srcToLineColumnRange(src: string): LineColRange {
const sourceLocation = sourceLocationFromSrc(src);
srcToLineColumnRange (src: string): LineColRange {
const sourceLocation = sourceLocationFromSrc(src)
if (sourceLocation.start >= 0 && sourceLocation.length >= 0) {
return <LineColRange>{
start: lineColPositionFromOffset(sourceLocation.start, this.lineBreaks),
......@@ -146,5 +145,4 @@ export class SourceMappings {
}
}
}
}
......@@ -3,7 +3,7 @@
export interface Location {
start: 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.
......@@ -31,7 +31,7 @@ export interface Node {
export interface AstNode {
/* The following fields are essential, and indicates an that object
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;
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