remix-astwalker linting done

pull/8/head
aniket-engg 4 years ago
parent dafbe6f1fe
commit 83cc0cb71a
  1. 7
      libs/remix-astwalker/.eslintrc
  2. 32
      libs/remix-astwalker/src/astWalker.ts
  3. 11
      libs/remix-astwalker/src/sourceMappings.ts
  4. 4
      libs/remix-astwalker/src/types.ts
  5. 34
      libs/remix-astwalker/tsconfig.json
  6. 13
      libs/remix-astwalker/tsconfig.lib.json
  7. 12
      workspace.json

@ -0,0 +1,7 @@
{
"extends": "../../.eslintrc",
"rules": {
"@typescript-eslint/no-explicit-any": "off"
},
"ignorePatterns": ["!**/*"]
}

@ -9,7 +9,7 @@ const isObject = function(obj: any): boolean {
return obj != null && obj.constructor.name === "Object" return obj != null && obj.constructor.name === "Object"
} }
export function isAstNode(node: Object): boolean { export function isAstNode(node: Record<string, unknown>): boolean {
return ( return (
isObject(node) && isObject(node) &&
'id' in node && 'id' in node &&
@ -35,7 +35,7 @@ export function isAstNode(node: Object): boolean {
export class AstWalker extends EventEmitter { export class AstWalker extends EventEmitter {
manageCallback( manageCallback(
node: AstNodeLegacy | AstNode, node: AstNodeLegacy | AstNode,
callback: Object | Function callback: Record<string, unknown> | Function // eslint-disable-line @typescript-eslint/ban-types
): any { ): any {
// FIXME: we shouldn't be doing this callback determination type on each AST node, // FIXME: we shouldn't be doing this callback determination type on each AST node,
// since the callback function is set once per walk. // since the callback function is set once per walk.
@ -58,7 +58,8 @@ export class AstWalker extends EventEmitter {
} }
} }
} }
walk(ast: AstNodeLegacy | AstNode, callback?: Function | Object) { // eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/explicit-module-boundary-types
walk(ast: AstNodeLegacy | AstNode, callback?: Function | Record<string, unknown>) {
if (callback) { if (callback) {
if (callback instanceof Function) { if (callback instanceof Function) {
callback = Object({ "*": callback }); callback = Object({ "*": callback });
@ -74,8 +75,8 @@ export class AstWalker extends EventEmitter {
(<AstNodeLegacy>ast).children && (<AstNodeLegacy>ast).children &&
(<AstNodeLegacy>ast).children.length > 0 (<AstNodeLegacy>ast).children.length > 0
) { ) {
for (let k in (<AstNodeLegacy>ast).children) { for (const k in (<AstNodeLegacy>ast).children) {
let child = (<AstNodeLegacy>ast).children[k]; const child = (<AstNodeLegacy>ast).children[k];
this.walk(child, callback); this.walk(child, callback);
} }
} }
@ -85,8 +86,8 @@ export class AstWalker extends EventEmitter {
(<AstNode>ast).nodes && (<AstNode>ast).nodes &&
(<AstNode>ast).nodes.length > 0 (<AstNode>ast).nodes.length > 0
) { ) {
for (let k in (<AstNode>ast).nodes) { for (const k in (<AstNode>ast).nodes) {
let child = (<AstNode>ast).nodes[k]; const child = (<AstNode>ast).nodes[k];
this.walk(child, callback); this.walk(child, callback);
} }
} }
@ -97,8 +98,8 @@ export class AstWalker extends EventEmitter {
(<AstNodeLegacy>ast).children && (<AstNodeLegacy>ast).children &&
(<AstNodeLegacy>ast).children.length > 0 (<AstNodeLegacy>ast).children.length > 0
) { ) {
for (let k in (<AstNodeLegacy>ast).children) { for (const k in (<AstNodeLegacy>ast).children) {
let child = (<AstNodeLegacy>ast).children[k]; const child = (<AstNodeLegacy>ast).children[k];
this.emit("node", child); this.emit("node", child);
this.walk(child); this.walk(child);
} }
@ -106,8 +107,8 @@ export class AstWalker extends EventEmitter {
} }
if (<AstNode>ast) { if (<AstNode>ast) {
if ((<AstNode>ast).nodes && (<AstNode>ast).nodes.length > 0) { if ((<AstNode>ast).nodes && (<AstNode>ast).nodes.length > 0) {
for (let k in (<AstNode>ast).nodes) { for (const k in (<AstNode>ast).nodes) {
let child = (<AstNode>ast).nodes[k]; const child = (<AstNode>ast).nodes[k];
this.emit("node", child); this.emit("node", child);
this.walk(child); this.walk(child);
} }
@ -115,18 +116,18 @@ export class AstWalker extends EventEmitter {
} }
} }
} }
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/explicit-module-boundary-types
walkFullInternal(ast: AstNode, callback: Function) { walkFullInternal(ast: AstNode, callback: Function) {
if (isAstNode(ast)) { if (isAstNode(ast)) {
// console.log(`XXX id ${ast.id}, nodeType: ${ast.nodeType}, src: ${ast.src}`); // console.log(`XXX id ${ast.id}, nodeType: ${ast.nodeType}, src: ${ast.src}`);
callback(ast); callback(ast);
for (let k of Object.keys(ast)) { for (const k of Object.keys(ast)) {
// Possible optimization: // Possible optimization:
// if (k in ['id', 'src', 'nodeType']) continue; // if (k in ['id', 'src', 'nodeType']) continue;
const astItem = ast[k]; const astItem = ast[k];
if (Array.isArray(astItem)) { if (Array.isArray(astItem)) {
for (let child of astItem) { for (const child of astItem) {
if (child) { if (child) {
this.walkFullInternal(child, callback); this.walkFullInternal(child, callback);
} }
@ -139,12 +140,13 @@ export class AstWalker extends EventEmitter {
} }
// Normalizes parameter callback and calls walkFullInternal // Normalizes parameter callback and calls walkFullInternal
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
walkFull(ast: AstNode, callback: any) { walkFull(ast: AstNode, callback: any) {
if (!isAstNode(ast)) throw new TypeError("first argument should be an ast"); if (!isAstNode(ast)) throw new TypeError("first argument should be an ast");
return this.walkFullInternal(ast, callback); return this.walkFullInternal(ast, callback);
} }
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/explicit-module-boundary-types
walkAstList(sourcesList: Node, cb?: Function) { walkAstList(sourcesList: Node, cb?: Function) {
if (cb) { if (cb) {
if (sourcesList.ast) { if (sourcesList.ast) {

@ -3,6 +3,7 @@ import { AstNode, LineColPosition, LineColRange, Location } from "./types";
import { util } from "remix-lib"; import { util } from "remix-lib";
export declare interface SourceMappings { export declare interface SourceMappings {
// eslint-disable-next-line @typescript-eslint/no-misused-new
new(): SourceMappings; new(): SourceMappings;
} }
@ -67,8 +68,8 @@ export class SourceMappings {
// 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.
let lineBreaks: Array<number> = []; const lineBreaks: Array<number> = [];
for (var 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;
@ -82,10 +83,10 @@ export class SourceMappings {
*/ */
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()
let found: Array<AstNode> = []; const found: Array<AstNode> = [];
const callback = function(node: AstNode): boolean { const callback = function(node: AstNode): boolean {
let 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) {
@ -111,7 +112,7 @@ export class SourceMappings {
/* 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) {
let 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) {

@ -37,7 +37,7 @@ export interface AstNode {
src: string; src: string;
absolutePath?: string; absolutePath?: string;
exportedSymbols?: Object; exportedSymbols?: Record<string, unknown>;
nodes?: Array<AstNode>; nodes?: Array<AstNode>;
literals?: Array<string>; literals?: Array<string>;
file?: string; file?: string;
@ -63,7 +63,7 @@ export interface AstNodeAtt {
constant?: boolean; constant?: boolean;
name?: string; name?: string;
public?: boolean; public?: boolean;
exportedSymbols?: Object; exportedSymbols?: Record<string, unknown>;
argumentTypes?: null; argumentTypes?: null;
absolutePath?: string; absolutePath?: string;
[x: string]: any; [x: string]: any;

@ -1,31 +1,7 @@
{ {
"include": ["src"], "extends": "../../tsconfig.json",
"exclude": ["node_modules", "src/@types" ],
"compilerOptions": { "compilerOptions": {
/* Basic Options */ "types": ["node"],
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ },
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ "include": ["**/*.ts"]
"lib": ["dom", "es2018"], /* Specify library files to be included in the compilation. */ }
"declaration": true, /* Generates corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
/* Module Resolution Options */
"baseUrl": "./src", /* Base directory to resolve non-absolute module names. */
"paths": { "remix-tests": ["./"] }, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
"typeRoots": ["./@types", "node_modules/@types"], /* List of folders to include type definitions from. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"types": [
"node"
],
/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"allowSyntheticDefaultImports": true,
// Disables strictNullChecks
"strictNullChecks": false
}
}

@ -0,0 +1,13 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../dist/out-tsc",
"declaration": true,
"rootDir": "./src",
"types": ["node"]
},
"exclude": ["**/*.spec.ts"],
"include": ["**/*.ts"]
}

@ -117,14 +117,14 @@
"schematics": {}, "schematics": {},
"architect": { "architect": {
"lint": { "lint": {
"builder": "@nrwl/workspace:run-commands", "builder": "@nrwl/linter:lint",
"options": { "options": {
"commands": [ "linter": "eslint",
{ "config": "libs/remix-astwalker/.eslintrc",
"command": "./../../node_modules/.bin/npm-run-all lint" "tsConfig": [
} "libs/remix-astwalker/tsconfig.lib.json"
], ],
"cwd": "libs/remix-astwalker" "exclude": ["**/node_modules/**", "libs/remix-astwalker/tests/**/*"]
} }
}, },
"test": { "test": {

Loading…
Cancel
Save