Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add escape and corresponding typedefs. #15

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- [x] Recursively walk directory and parse the files in it.
- [x] Ability to define rules and call them on AST Nodes.
- [x] Retain state across visits.
- [ ] Ignore files that match certain patterns.
- [ ] Ignore files that match certain patterns.
- [ ] Add on-exit and on-enter rules.
5 changes: 4 additions & 1 deletion example/sample-project/bar.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
let x = [1, 2, 3 ,, 4];
let x2 = [,,,2, 3]

let template = `${xx} x`
// let template = `${xx} x`
let t2 = `12
3`;
t2 = 2;
let badTemplate = `bad template`;

Object.getOwnPropertyNames(globalThis).forEach(x => console.log(x));
8 changes: 2 additions & 6 deletions example/sample-project/foo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
var x = 1

module.exports = { foo: 2 };
console.log(x);
module.exports = { foo: 1 };
console.log(x);
let foo = 1;
console.log(bar);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@types/esquery": "^1.0.2",
"@types/estree": "^0.0.50",
"cli-color": "^2.0.1",
"escope": "^3.6.0",
"espree": "^9.3.0",
"esquery": "^1.4.0",
"glob": "^7.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function analyzeJS(filePath: string, code: string, visitor?: ASTVisitor)
const ast = parseJS(code);
const checks = checkDescriptors.map((desc: CheckDescriptor) => new Check(desc));
visitor = visitor || new ASTVisitor(filePath, code, checks);
visitor.visit(ast);
visitor.checkAST(ast);

// TODO (@injuly): Instead of logging them, return the reports array.
visitor.logReports();
Expand Down
8 changes: 6 additions & 2 deletions src/check.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import ESTree from 'estree';
import JsNodeNames from './util';
import { JsNodeNames } from './util';
import { NodeParentExtension } from './visitor/ast-visitor';
import VisitorContext from './visitor/visitor-context';

type NodeVisitorFunc<T extends ESTree.BaseNode> = (ctx: VisitorContext, node: T) => void;
type NodeVisitorFunc<T extends ESTree.BaseNode> = (
ctx: VisitorContext,
node: T & NodeParentExtension
) => void;

export type CheckDescriptor = {
cache: Record<string, any>;
Expand Down
1 change: 1 addition & 0 deletions src/checks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module.exports = [
require('./array-gaps'),
require('./multiple-exports'),
require('./useless-template'),
require('./undeclared-var'),
];
81 changes: 80 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,73 @@
export const ASTNode = {
ArrayExpression: 'ArrayExpression',
ArrayPattern: 'ArrayPattern',
ArrowFunctionExpression: 'ArrowFunctionExpression',
AssignmentExpression: 'AssignmentExpression',
AssignmentPattern: 'AssignmentPattern',
AwaitExpression: 'AwaitExpression',
BinaryExpression: 'BinaryExpression',
BlockStatement: 'BlockStatement',
BreakStatement: 'BreakStatement',
CallExpression: 'CallExpression',
CatchClause: 'CatchClause',
ChainExpression: 'ChainExpression',
ClassBody: 'ClassBody',
ClassDeclaration: 'ClassDeclaration',
ClassExpression: 'ClassExpression',
ConditionalExpression: 'ConditionalExpression',
ContinueStatement: 'ContinueStatement',
DebuggerStatement: 'DebuggerStatement',
DoWhileStatement: 'DoWhileStatement',
EmptyStatement: 'EmptyStatement',
ExportAllDeclaration: 'ExportAllDeclaration',
ExportDefaultDeclaration: 'ExportDefaultDeclaration',
ExportNamedDeclaration: 'ExportNamedDeclaration',
ExportSpecifier: 'ExportSpecifier',
ExpressionStatement: 'ExpressionStatement',
ForInStatement: 'ForInStatement',
ForOfStatement: 'ForOfStatement',
ForStatement: 'ForStatement',
FunctionDeclaration: 'FunctionDeclaration',
FunctionExpression: 'FunctionExpression',
Identifier: 'Identifier',
IfStatement: 'IfStatement',
ImportDeclaration: 'ImportDeclaration',
ImportDefaultSpecifier: 'ImportDefaultSpecifier',
ImportExpression: 'ImportExpression',
ImportNamespaceSpecifier: 'ImportNamespaceSpecifier',
ImportSpecifier: 'ImportSpecifier',
LabeledStatement: 'LabeledStatement',
Literal: 'Literal',
LogicalExpression: 'LogicalExpression',
MemberExpression: 'MemberExpression',
MetaProperty: 'MetaProperty',
MethodDefinition: 'MethodDefinition',
NewExpression: 'NewExpression',
ObjectExpression: 'ObjectExpression',
ObjectPattern: 'ObjectPattern',
Program: 'Program',
Property: 'Property',
RestElement: 'RestElement',
ReturnStatement: 'ReturnStatement',
SequenceExpression: 'SequenceExpression',
SpreadElement: 'SpreadElement',
Super: 'Super',
SwitchCase: 'SwitchCase',
SwitchStatement: 'SwitchStatement',
TaggedTemplateExpression: 'TaggedTemplateExpression',
TemplateElement: 'TemplateElement',
TemplateLiteral: 'TemplateLiteral',
ThisExpression: 'ThisExpression',
ThrowStatement: 'ThrowStatement',
TryStatement: 'TryStatement',
UnaryExpression: 'UnaryExpression',
UpdateExpression: 'UpdateExpression',
VariableDeclaration: 'VariableDeclaration',
VariableDeclarator: 'VariableDeclarator',
WhileStatement: 'WhileStatement',
WithStatement: 'WithStatement',
};

export const JsNodeNames = new Set([
'ArrayExpression',
'ArrayPattern',
Expand Down Expand Up @@ -68,4 +138,13 @@ export const JsNodeNames = new Set([
'WithStatement',
]);

export default JsNodeNames;
export function assert<T>(value: T, msg?: string): T {
msg = msg || 'assertion failed!';
if (!value) {
throw new Error(msg);
}
return value;
}

// A set of all global variables provided by Node.js runtime.
export const AllGlobalVars = new Set(Object.getOwnPropertyNames(globalThis));
Loading