1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 04:52:26 -05:00

Refactor binary ast

This commit is contained in:
Marvin Hagemeister 2024-12-12 22:16:49 +01:00
parent 76c1275200
commit c83f722039
6 changed files with 976 additions and 738 deletions

View file

@ -288,7 +288,153 @@ const AstType = {
JSXText: 104,
};
const AstNodeById = Object.keys(AstType);
const AstTypeName = Object.keys(AstType);
// Keep in sync with Rust
const AstProp = [
// Base
"parent",
"range",
"type",
"_InternalFlags", // Internal
// Node
"alternate",
"argument",
"arguments",
"async",
"attributes",
"await",
"block",
"body",
"callee",
"cases",
"children",
"closingElement",
"closingFragment",
"computed",
"consequent",
"declarations",
"declare",
"definite",
"delegate",
"discrimininant",
"elements",
"expression",
"expressions",
"finalizer",
"flags",
"generator",
"handler",
"id",
"init",
"key",
"kind",
"label",
"left",
"members",
"meta",
"method",
"name",
"namespace",
"object",
"openingElement",
"openingFragment",
"operator",
"optional",
"params",
"pattern",
"prefix",
"properties",
"property",
"quasi",
"quasis",
"raw",
"returnType",
"right",
"selfClosing",
"shorthand",
"source",
"specifiers",
"tag",
"tail",
"test",
"typeAnnotation",
"typeArguments",
"typeParameters",
"update",
"value",
];
const AST_PROP_PARENT = AstProp.indexOf("parent");
const AST_PROP_TYPE = AstProp.indexOf("type");
const AST_PROP_RANGE = AstProp.indexOf("range");
const AST_PROP_INTERNAL_FLAGS = AstProp.indexOf("_InternalFlags");
const AST_PROP_ATTRIBUTE = AstProp.indexOf("attribute");
/**
* @param {AstContext} ctx
* @param {number} offset
* @param {number} propId
*/
function readValue(ctx, offset, propId) {
const { buf } = ctx;
// FIXME
if (propId === AST_PROP_TYPE) {
const type = buf[offset];
return AstTypeName[type];
} else if (propId === AST_PROP_RANGE) {
const start = readU32(buf, offset + 1 + 4);
const end = readU32(buf, offset + 1 + 4 + 4);
return [start, end];
} else if (propId === AST_PROP_PARENT) {
const id = readU32(buf, offset + 1);
if (id === 0) return null;
const target = ctx.idTable[id];
return new Node(ctx, target);
} else if (propId === AST_PROP_INTERNAL_FLAGS) {
//
} else {
const type = buf[offset];
// type + parentId + SpanLo + SpanHi
offset += 1 + 4 + 4 + 4;
// Check for arrays
if (propId === AST_PROP_ATTRIBUTE && type === AstType.JSXOpeningElement) {
}
//
const target = 0;
return new Node(ctx, target);
}
}
class Node {
#ctx;
#offset;
static {
for (let i = 0; i < AstProp.length; i++) {
const name = AstProp[i];
Object.defineProperty(this, name, {
get() {
return readValue(this.#ctx, this.#offset, i);
},
});
}
}
/**
* @param {AstContext} ctx
* @param {number} offset
*/
constructor(ctx, offset) {
this.#ctx = ctx;
this.#offset = offset;
}
}
/**
* @param {AstContext} ctx
@ -306,29 +452,8 @@ function createChildNodes(ctx, ids) {
return out;
}
class BaseNode {
#ctx;
#parentId;
get parent() {
return /** @type {*} */ (createAstNode(
this.#ctx,
this.#parentId,
));
}
/**
* @param {AstContext} ctx
* @param {number} parentId
*/
constructor(ctx, parentId) {
this.#ctx = ctx;
this.#parentId = parentId;
}
}
/** @implements {Deno.Program} */
class Program extends BaseNode {
class Program extends Node {
type = /** @type {const} */ ("Program");
range;
get body() {
@ -356,7 +481,7 @@ class Program extends BaseNode {
// Declarations
/** @implements {Deno.FunctionDeclaration} */
class FunctionDeclaration extends BaseNode {
class FunctionDeclaration extends Node {
type = /** @type {const} */ ("FunctionDeclaration");
range;
@ -426,7 +551,7 @@ class FunctionDeclaration extends BaseNode {
}
/** @implements {Deno.VariableDeclaration} */
class VariableDeclaration extends BaseNode {
class VariableDeclaration extends Node {
type = /** @type {const} */ ("VariableDeclaration");
range;
get declarations() {
@ -460,7 +585,7 @@ class VariableDeclaration extends BaseNode {
}
/** @implements {Deno.VariableDeclarator} */
class VariableDeclarator extends BaseNode {
class VariableDeclarator extends Node {
type = /** @type {const} */ ("VariableDeclarator");
range;
@ -498,7 +623,7 @@ class VariableDeclarator extends BaseNode {
// Statements
/** @implements {Deno.BlockStatement} */
class BlockStatement extends BaseNode {
class BlockStatement extends Node {
type = /** @type {const} */ ("BlockStatement");
get body() {
return createChildNodes(this.#ctx, this.#childIds);
@ -523,7 +648,7 @@ class BlockStatement extends BaseNode {
}
/** @implements {Deno.BreakStatement} */
class BreakStatement extends BaseNode {
class BreakStatement extends Node {
type = /** @type {const} */ ("BreakStatement");
get label() {
if (this.#labelId === 0) return null;
@ -553,7 +678,7 @@ class BreakStatement extends BaseNode {
}
/** @implements {Deno.ContinueStatement} */
class ContinueStatement extends BaseNode {
class ContinueStatement extends Node {
type = /** @type {const} */ ("ContinueStatement");
range;
get label() {
@ -581,7 +706,7 @@ class ContinueStatement extends BaseNode {
}
/** @implements {Deno.DebuggerStatement} */
class DebuggerStatement extends BaseNode {
class DebuggerStatement extends Node {
type = /** @type {const} */ ("DebuggerStatement");
range;
@ -597,7 +722,7 @@ class DebuggerStatement extends BaseNode {
}
/** @implements {Deno.DoWhileStatement} */
class DoWhileStatement extends BaseNode {
class DoWhileStatement extends Node {
type = /** @type {const} */ ("DoWhileStatement");
range;
get test() {
@ -635,7 +760,7 @@ class DoWhileStatement extends BaseNode {
}
/** @implements {Deno.ExpressionStatement} */
class ExpressionStatement extends BaseNode {
class ExpressionStatement extends Node {
type = /** @type {const} */ ("ExpressionStatement");
range;
get expression() {
@ -664,7 +789,7 @@ class ExpressionStatement extends BaseNode {
}
/** @implements {Deno.ForInStatement} */
class ForInStatement extends BaseNode {
class ForInStatement extends Node {
type = /** @type {const} */ ("ForInStatement");
range;
get left() {
@ -711,7 +836,7 @@ class ForInStatement extends BaseNode {
}
/** @implements {Deno.ForOfStatement} */
class ForOfStatement extends BaseNode {
class ForOfStatement extends Node {
type = /** @type {const} */ ("ForOfStatement");
range;
get left() {
@ -762,7 +887,7 @@ class ForOfStatement extends BaseNode {
}
/** @implements {Deno.ForStatement} */
class ForStatement extends BaseNode {
class ForStatement extends Node {
type = /** @type {const} */ ("ForStatement");
range;
get init() {
@ -822,7 +947,7 @@ class ForStatement extends BaseNode {
}
/** @implements {Deno.IfStatement} */
class IfStatement extends BaseNode {
class IfStatement extends Node {
type = /** @type {const} */ ("IfStatement");
range;
get test() {
@ -870,7 +995,7 @@ class IfStatement extends BaseNode {
}
/** @implements {Deno.LabeledStatement} */
class LabeledStatement extends BaseNode {
class LabeledStatement extends Node {
type = /** @type {const} */ ("LabeledStatement");
range;
get label() {
@ -908,7 +1033,7 @@ class LabeledStatement extends BaseNode {
}
/** @implements {Deno.ReturnStatement} */
class ReturnStatement extends BaseNode {
class ReturnStatement extends Node {
type = /** @type {const} */ ("ReturnStatement");
range;
get argument() {
@ -938,7 +1063,7 @@ class ReturnStatement extends BaseNode {
}
/** @implements {Deno.SwitchStatement} */
class SwitchStatement extends BaseNode {
class SwitchStatement extends Node {
type = /** @type {const} */ ("SwitchStatement");
range;
get discriminant() {
@ -973,7 +1098,7 @@ class SwitchStatement extends BaseNode {
}
/** @implements {Deno.SwitchCase} */
class SwitchCase extends BaseNode {
class SwitchCase extends Node {
type = /** @type {const} */ ("SwitchCase");
range;
get test() {
@ -1008,7 +1133,7 @@ class SwitchCase extends BaseNode {
}
/** @implements {Deno.ThrowStatement} */
class ThrowStatement extends BaseNode {
class ThrowStatement extends Node {
type = /** @type {const} */ ("ThrowStatement");
range;
get argument() {
@ -1037,7 +1162,7 @@ class ThrowStatement extends BaseNode {
}
/** @implements {Deno.TryStatement} */
class TryStatement extends BaseNode {
class TryStatement extends Node {
type = /** @type {const} */ ("TryStatement");
range;
get block() {
@ -1086,7 +1211,7 @@ class TryStatement extends BaseNode {
}
/** @implements {Deno.WhileStatement} */
class WhileStatement extends BaseNode {
class WhileStatement extends Node {
type = /** @type {const} */ ("WhileStatement");
range;
get test() {
@ -1161,7 +1286,7 @@ class WithStatement {
// Expressions
/** @implements {Deno.ArrayExpression} */
class ArrayExpression extends BaseNode {
class ArrayExpression extends Node {
type = /** @type {const} */ ("ArrayExpression");
range;
get elements() {
@ -1187,7 +1312,7 @@ class ArrayExpression extends BaseNode {
}
/** @implements {Deno.ArrowFunctionExpression} */
class ArrowFunctionExpression extends BaseNode {
class ArrowFunctionExpression extends Node {
type = /** @type {const} */ ("ArrowFunctionExpression");
range;
async = false;
@ -1262,7 +1387,7 @@ class ArrowFunctionExpression extends BaseNode {
}
/** @implements {Deno.AssignmentExpression} */
class AssignmentExpression extends BaseNode {
class AssignmentExpression extends Node {
type = /** @type {const} */ ("AssignmentExpression");
range;
get left() {
@ -1347,7 +1472,7 @@ function getAssignOperator(n) {
}
/** @implements {Deno.AwaitExpression} */
class AwaitExpression extends BaseNode {
class AwaitExpression extends Node {
type = /** @type {const} */ ("AwaitExpression");
range;
get argument() {
@ -1375,7 +1500,7 @@ class AwaitExpression extends BaseNode {
}
/** @implements {Deno.UpdateExpression} */
class UpdateExpression extends BaseNode {
class UpdateExpression extends Node {
type = /** @type {const} */ ("UpdateExpression");
range;
get argument() {
@ -1408,7 +1533,7 @@ class UpdateExpression extends BaseNode {
}
/** @implements {Deno.BinaryExpression} */
class BinaryExpression extends BaseNode {
class BinaryExpression extends Node {
type = /** @type {const} */ ("BinaryExpression");
range;
get left() {
@ -1504,7 +1629,7 @@ function getBinaryOperator(n) {
}
/** @implements {Deno.CallExpression} */
class CallExpression extends BaseNode {
class CallExpression extends Node {
type = /** @type {const} */ ("CallExpression");
range;
get callee() {
@ -1550,7 +1675,7 @@ class CallExpression extends BaseNode {
}
/** @implements {Deno.ChainExpression} */
class ChainExpression extends BaseNode {
class ChainExpression extends Node {
type = /** @type {const} */ ("ChainExpression");
range;
@ -1576,7 +1701,7 @@ class ChainExpression extends BaseNode {
}
/** @implements {Deno.ConditionalExpression} */
class ConditionalExpression extends BaseNode {
class ConditionalExpression extends Node {
type = /** @type {const} */ ("ConditionalExpression");
range;
get test() {
@ -1623,7 +1748,7 @@ class ConditionalExpression extends BaseNode {
}
/** @implements {Deno.FunctionExpression} */
class FunctionExpression extends BaseNode {
class FunctionExpression extends Node {
type = /** @type {const} */ ("FunctionExpression");
range;
@ -1696,7 +1821,7 @@ class FunctionExpression extends BaseNode {
}
/** @implements {Deno.Identifier} */
class Identifier extends BaseNode {
class Identifier extends Node {
type = /** @type {const} */ ("Identifier");
range;
name = "";
@ -1716,7 +1841,7 @@ class Identifier extends BaseNode {
}
/** @implements {Deno.LogicalExpression} */
class LogicalExpression extends BaseNode {
class LogicalExpression extends Node {
type = /** @type {const} */ ("LogicalExpression");
range;
get left() {
@ -1772,7 +1897,7 @@ function getLogicalOperator(n) {
}
/** @implements {Deno.MemberExpression} */
class MemberExpression extends BaseNode {
class MemberExpression extends Node {
type = /** @type {const} */ ("MemberExpression");
range;
get object() {
@ -1850,7 +1975,7 @@ class MetaProperty {
}
/** @implements {Deno.NewExpression} */
class NewExpression extends BaseNode {
class NewExpression extends Node {
type = /** @type {const} */ ("NewExpression");
range;
get arguments() {
@ -1885,7 +2010,7 @@ class NewExpression extends BaseNode {
}
/** @implements {Deno.ObjectExpression} */
class ObjectExpression extends BaseNode {
class ObjectExpression extends Node {
type = /** @type {const} */ ("ObjectExpression");
range;
get properties() {
@ -1911,7 +2036,7 @@ class ObjectExpression extends BaseNode {
}
/** @implements {Deno.ParenthesisExpression} */
class ParenthesisExpression extends BaseNode {
class ParenthesisExpression extends Node {
type = /** @type {const} */ ("ParenthesisExpression");
range;
#ctx;
@ -1937,7 +2062,7 @@ class ParenthesisExpression extends BaseNode {
}
/** @implements {Deno.PrivateIdentifier} */
class PrivateIdentifier extends BaseNode {
class PrivateIdentifier extends Node {
type = /** @type {const} */ ("PrivateIdentifier");
range;
#ctx;
@ -1959,7 +2084,7 @@ class PrivateIdentifier extends BaseNode {
}
/** @implements {Deno.Property} */
class Property extends BaseNode {
class Property extends Node {
type = /** @type {const} */ ("Property");
range;
#ctx;
@ -2001,7 +2126,7 @@ class Property extends BaseNode {
}
/** @implements {Deno.RestElement} */
class RestElement extends BaseNode {
class RestElement extends Node {
type = /** @type {const} */ ("RestElement");
range;
#ctx;
@ -2034,7 +2159,7 @@ class RestElement extends BaseNode {
}
/** @implements {Deno.SequenceExpression} */
class SequenceExpression extends BaseNode {
class SequenceExpression extends Node {
type = /** @type {const} */ ("SequenceExpression");
range;
#ctx;
@ -2060,7 +2185,7 @@ class SequenceExpression extends BaseNode {
}
/** @implements {Deno.SpreadElement} */
class SpreadElement extends BaseNode {
class SpreadElement extends Node {
type = /** @type {const} */ ("SpreadElement");
range;
#ctx;
@ -2086,7 +2211,7 @@ class SpreadElement extends BaseNode {
}
/** @implements {Deno.Super} */
class Super extends BaseNode {
class Super extends Node {
type = /** @type {const} */ ("Super");
range;
@ -2102,7 +2227,7 @@ class Super extends BaseNode {
}
/** @implements {Deno.TaggedTemplateExpression} */
class TaggedTemplateExpression extends BaseNode {
class TaggedTemplateExpression extends Node {
type = /** @type {const} */ ("TaggedTemplateExpression");
range;
@ -2144,7 +2269,7 @@ class TaggedTemplateExpression extends BaseNode {
}
/** @implements {Deno.UnaryExpression} */
class UnaryExpression extends BaseNode {
class UnaryExpression extends Node {
type = /** @type {const} */ ("UnaryExpression");
range;
@ -2197,7 +2322,7 @@ function getUnaryOperator(n) {
}
/** @implements {Deno.YieldExpression} */
class YieldExpression extends BaseNode {
class YieldExpression extends Node {
type = /** @type {const} */ ("YieldExpression");
range;
get argument() {
@ -2232,7 +2357,7 @@ class YieldExpression extends BaseNode {
// Literals
/** @implements {Deno.BooleanLiteral} */
class BooleanLiteral extends BaseNode {
class BooleanLiteral extends Node {
type = /** @type {const} */ ("BooleanLiteral");
range;
value = false;
@ -2251,7 +2376,7 @@ class BooleanLiteral extends BaseNode {
}
/** @implements {Deno.BigIntLiteral} */
class BigIntLiteral extends BaseNode {
class BigIntLiteral extends Node {
type = /** @type {const} */ ("BigIntLiteral");
range;
value;
@ -2270,7 +2395,7 @@ class BigIntLiteral extends BaseNode {
}
/** @implements {Deno.NullLiteral} */
class NullLiteral extends BaseNode {
class NullLiteral extends Node {
type = /** @type {const} */ ("NullLiteral");
range;
value = null;
@ -2287,7 +2412,7 @@ class NullLiteral extends BaseNode {
}
/** @implements {Deno.NumericLiteral} */
class NumericLiteral extends BaseNode {
class NumericLiteral extends Node {
type = /** @type {const} */ ("NumericLiteral");
range;
value = 0;
@ -2306,7 +2431,7 @@ class NumericLiteral extends BaseNode {
}
/** @implements {Deno.RegExpLiteral} */
class RegExpLiteral extends BaseNode {
class RegExpLiteral extends Node {
type = /** @type {const} */ ("RegExpLiteral");
range;
pattern = "";
@ -2329,7 +2454,7 @@ class RegExpLiteral extends BaseNode {
}
/** @implements {Deno.StringLiteral} */
class StringLiteral extends BaseNode {
class StringLiteral extends Node {
type = /** @type {const} */ ("StringLiteral");
range;
value = "";
@ -2348,7 +2473,7 @@ class StringLiteral extends BaseNode {
}
/** @implements {Deno.TemplateLiteral} */
class TemplateLiteral extends BaseNode {
class TemplateLiteral extends Node {
type = /** @type {const} */ ("TemplateLiteral");
range;
@ -2381,7 +2506,7 @@ class TemplateLiteral extends BaseNode {
}
/** @implements {Deno.TemplateElement} */
class TemplateElement extends BaseNode {
class TemplateElement extends Node {
type = /** @type {const} */ ("TemplateElement");
range;
@ -2412,7 +2537,7 @@ class TemplateElement extends BaseNode {
// Patterns
/** @implements {Deno.AssignmentPattern} */
class AssignmentPattern extends BaseNode {
class AssignmentPattern extends Node {
type = /** @type {const} */ ("AssignmentPattern");
range;
@ -2446,7 +2571,7 @@ class AssignmentPattern extends BaseNode {
}
/** @implements {Deno.ArrayPattern} */
class ArrayPattern extends BaseNode {
class ArrayPattern extends Node {
type = /** @type {const} */ ("ArrayPattern");
range;
@ -2485,7 +2610,7 @@ class ArrayPattern extends BaseNode {
}
/** @implements {Deno.ObjectPattern} */
class ObjectPattern extends BaseNode {
class ObjectPattern extends Node {
type = /** @type {const} */ ("ObjectPattern");
range;
@ -2526,7 +2651,7 @@ class ObjectPattern extends BaseNode {
// JSX
/** @implements {Deno.JSXAttribute} */
class JSXAttribute extends BaseNode {
class JSXAttribute extends Node {
type = /** @type {const} */ ("JSXAttribute");
range;
get name() {
@ -2565,7 +2690,7 @@ class JSXAttribute extends BaseNode {
}
/** @implements {Deno.JSXClosingElement} */
class JSXClosingElement extends BaseNode {
class JSXClosingElement extends Node {
type = /** @type {const} */ ("JSXClosingElement");
range;
get name() {
@ -2594,7 +2719,7 @@ class JSXClosingElement extends BaseNode {
}
/** @implements {Deno.JSXClosingFragment} */
class JSXClosingFragment extends BaseNode {
class JSXClosingFragment extends Node {
type = /** @type {const} */ ("JSXClosingFragment");
range;
@ -2611,7 +2736,7 @@ class JSXClosingFragment extends BaseNode {
}
/** @implements {Deno.JSXElement} */
class JSXElement extends BaseNode {
class JSXElement extends Node {
type = /** @type {const} */ ("JSXElement");
range;
get children() {
@ -2656,7 +2781,7 @@ class JSXElement extends BaseNode {
}
/** @implements {Deno.JSXEmptyExpression} */
class JSXEmptyExpression extends BaseNode {
class JSXEmptyExpression extends Node {
type = /** @type {const} */ ("JSXEmptyExpression");
range;
@ -2672,7 +2797,7 @@ class JSXEmptyExpression extends BaseNode {
}
/** @implements {Deno.JSXExpressionContainer} */
class JSXExpressionContainer extends BaseNode {
class JSXExpressionContainer extends Node {
type = /** @type {const} */ ("JSXExpressionContainer");
range;
get expression() {
@ -2701,7 +2826,7 @@ class JSXExpressionContainer extends BaseNode {
}
/** @implements {Deno.JSXFragment} */
class JSXFragment extends BaseNode {
class JSXFragment extends Node {
type = /** @type {const} */ ("JSXFragment");
range;
get children() {
@ -2746,7 +2871,7 @@ class JSXFragment extends BaseNode {
}
/** @implements {Deno.JSXIdentifier} */
class JSXIdentifier extends BaseNode {
class JSXIdentifier extends Node {
type = /** @type {const} */ ("JSXIdentifier");
range;
name;
@ -2766,7 +2891,7 @@ class JSXIdentifier extends BaseNode {
}
/** @implements {Deno.JSXMemberExpression} */
class JSXMemberExpression extends BaseNode {
class JSXMemberExpression extends Node {
type = /** @type {const} */ ("JSXMemberExpression");
range;
get object() {
@ -2804,7 +2929,7 @@ class JSXMemberExpression extends BaseNode {
}
/** @implements {Deno.JSXNamespacedName} */
class JSXNamespacedName extends BaseNode {
class JSXNamespacedName extends Node {
type = /** @type {const} */ ("JSXNamespacedName");
range;
get name() {
@ -2842,7 +2967,7 @@ class JSXNamespacedName extends BaseNode {
}
/** @implements {Deno.JSXOpeningElement} */
class JSXOpeningElement extends BaseNode {
class JSXOpeningElement extends Node {
type = /** @type {const} */ ("JSXOpeningElement");
range;
get attributes() {
@ -2880,7 +3005,7 @@ class JSXOpeningElement extends BaseNode {
}
/** @implements {Deno.JSXOpeningFragment} */
class JSXOpeningFragment extends BaseNode {
class JSXOpeningFragment extends Node {
type = /** @type {const} */ ("JSXOpeningFragment");
range;
@ -2897,7 +3022,7 @@ class JSXOpeningFragment extends BaseNode {
}
/** @implements {Deno.JSXSpreadAttribute} */
class JSXSpreadAttribute extends BaseNode {
class JSXSpreadAttribute extends Node {
type = /** @type {const} */ ("JSXSpreadAttribute");
range;
@ -2927,7 +3052,7 @@ class JSXSpreadAttribute extends BaseNode {
}
/** @implements {Deno.JSXText} */
class JSXText extends BaseNode {
class JSXText extends Node {
type = /** @type {const} */ ("JSXText");
range;

626
cli/tools/lint/ast_buf.rs Normal file
View file

@ -0,0 +1,626 @@
use deno_ast::{
swc::common::{Span, DUMMY_SP},
view::AssignOp,
};
use indexmap::IndexMap;
pub enum Flag {
ProgramModule,
FnAsync,
FnGenerator,
FnDeclare,
FnOptional,
MemberComputed,
MemberOptional,
PropShorthand,
PropComputed,
PropGetter,
PropSetter,
PropMethod,
VarVar,
VarConst,
VarLet,
VarDeclare,
ExportType,
TplTail,
ForAwait,
LogicalOr,
LogicalAnd,
LogicalNullishCoalescin,
JSXSelfClosing,
BinEqEq,
BinNotEq,
BinEqEqEq,
BinNotEqEq,
BinLt,
BinLtEq,
BinGt,
BinGtEq,
BinLShift,
BinRShift,
BinZeroFillRShift,
BinAdd,
BinSub,
BinMul,
BinDiv,
BinMod,
BinBitOr,
BinBitXor,
BinBitAnd,
BinIn,
BinInstanceOf,
BinExp,
UnaryMinus,
UnaryPlus,
UnaryBang,
UnaryTilde,
UnaryTypeOf,
UnaryVoid,
UnaryDelete,
UpdatePrefix,
UpdatePlusPlus,
UpdateMinusMinus,
YieldDelegate,
ParamOptional,
ClassDeclare,
ClassAbstract,
ClassConstructor,
ClassMethod,
ClassPublic,
ClassProtected,
ClassPrivate,
TsDeclare,
TsConst,
TsTrue,
TsPlus,
TsMinus,
TsReadonly,
}
pub fn assign_op_to_flag(m: AssignOp) -> u8 {
match m {
AssignOp::Assign => 0,
AssignOp::AddAssign => 1,
AssignOp::SubAssign => 2,
AssignOp::MulAssign => 3,
AssignOp::DivAssign => 4,
AssignOp::ModAssign => 5,
AssignOp::LShiftAssign => 6,
AssignOp::RShiftAssign => 7,
AssignOp::ZeroFillRShiftAssign => 8,
AssignOp::BitOrAssign => 9,
AssignOp::BitXorAssign => 10,
AssignOp::BitAndAssign => 11,
AssignOp::ExpAssign => 12,
AssignOp::AndAssign => 13,
AssignOp::OrAssign => 14,
AssignOp::NullishAssign => 15,
}
}
impl From<Flag> for u8 {
fn from(m: Flag) -> u8 {
match m {
Flag::ProgramModule => 0b00000001,
Flag::FnAsync => 0b00000001,
Flag::FnGenerator => 0b00000010,
Flag::FnDeclare => 0b00000100,
Flag::FnOptional => 0b00001000,
Flag::MemberComputed => 0b00000001,
Flag::MemberOptional => 0b00000010,
Flag::PropShorthand => 0b00000001,
Flag::PropComputed => 0b00000010,
Flag::PropGetter => 0b00000100,
Flag::PropSetter => 0b00001000,
Flag::PropMethod => 0b00010000,
Flag::VarVar => 0b00000001,
Flag::VarConst => 0b00000010,
Flag::VarLet => 0b00000100,
Flag::VarDeclare => 0b00001000,
Flag::ExportType => 0b000000001,
Flag::TplTail => 0b000000001,
Flag::ForAwait => 0b000000001,
Flag::LogicalOr => 0b000000001,
Flag::LogicalAnd => 0b000000010,
Flag::LogicalNullishCoalescin => 0b000000100,
Flag::JSXSelfClosing => 0b000000001,
Flag::BinEqEq => 1,
Flag::BinNotEq => 2,
Flag::BinEqEqEq => 3,
Flag::BinNotEqEq => 4,
Flag::BinLt => 5,
Flag::BinLtEq => 6,
Flag::BinGt => 7,
Flag::BinGtEq => 8,
Flag::BinLShift => 9,
Flag::BinRShift => 10,
Flag::BinZeroFillRShift => 11,
Flag::BinAdd => 12,
Flag::BinSub => 13,
Flag::BinMul => 14,
Flag::BinDiv => 15,
Flag::BinMod => 16,
Flag::BinBitOr => 17,
Flag::BinBitXor => 18,
Flag::BinBitAnd => 19,
Flag::BinIn => 20,
Flag::BinInstanceOf => 21,
Flag::BinExp => 22,
Flag::UnaryMinus => 1,
Flag::UnaryPlus => 2,
Flag::UnaryBang => 3,
Flag::UnaryTilde => 4,
Flag::UnaryTypeOf => 5,
Flag::UnaryVoid => 6,
Flag::UnaryDelete => 7,
Flag::UpdatePrefix => 0b000000001,
Flag::UpdatePlusPlus => 0b000000010,
Flag::UpdateMinusMinus => 0b000000100,
Flag::YieldDelegate => 1,
Flag::ParamOptional => 1,
Flag::ClassDeclare => 0b000000001,
Flag::ClassAbstract => 0b000000010,
Flag::ClassConstructor => 0b000000100,
Flag::ClassMethod => 0b000001000,
Flag::ClassPublic => 0b001000000,
Flag::ClassProtected => 0b010000000,
Flag::ClassPrivate => 0b10000000,
Flag::TsDeclare => 0b000000001,
Flag::TsConst => 0b000000010,
Flag::TsTrue => 0b000000100,
Flag::TsPlus => 0b000001000,
Flag::TsMinus => 0b000010000,
Flag::TsReadonly => 0b000100000,
}
}
}
// Keep in sync with JS
pub enum AstNode {
Invalid,
//
Program,
// Module declarations
Import,
ImportDecl,
ExportDecl,
ExportNamedDeclaration,
ExportDefaultDecl,
ExportDefaultExpr,
ExportAll,
TsImportEquals,
TsExportAssignment,
TsNamespaceExport,
// Decls
ClassDeclaration,
Fn,
Var,
Using,
TSInterface,
TsTypeAlias,
TSEnumDeclaration,
TsModule,
// Statements
Block,
Empty,
Debugger,
With,
Return,
Labeled,
Break,
Continue,
IfStatement,
Switch,
SwitchCase,
Throw,
TryStatement,
While,
DoWhileStatement,
ForStatement,
ForInStatement,
ForOfStatement,
Decl,
ExpressionStatement,
// Expressions
This,
Array,
Object,
FunctionExpression,
Unary,
UpdateExpression,
BinaryExpression,
Assign,
MemberExpression,
Super,
Cond,
CallExpression,
New,
Paren,
SequenceExpression,
Identifier,
TemplateLiteral,
TaggedTemplateExpression,
ArrowFunctionExpression,
ClassExpr,
YieldExpression,
MetaProp,
AwaitExpression,
LogicalExpression,
TSTypeAssertion,
TsConstAssertion,
TSNonNullExpression,
TSAsExpression,
TsInstantiation,
TsSatisfies,
PrivateIdentifier,
ChainExpression,
// Literals
StringLiteral,
Bool,
Null,
NumericLiteral,
BigIntLiteral,
RegExpLiteral,
// Custom
EmptyExpr,
Spread,
Property,
VariableDeclarator,
CatchClause,
RestElement,
ExportSpecifier,
TemplateElement,
MethodDefinition,
// Patterns
ArrayPattern,
AssignmentPattern,
ObjectPattern,
// JSX
JSXAttribute,
JSXClosingElement,
JSXClosingFragment,
JSXElement,
JSXEmptyExpression,
JSXExpressionContainer,
JSXFragment,
JSXIdentifier,
JSXMemberExpression,
JSXNamespacedName,
JSXOpeningElement,
JSXOpeningFragment,
JSXSpreadAttribute,
JSXSpreadChild,
JSXText,
TSTypeAnnotation,
TSTypeParameterDeclaration,
TSTypeParameter,
TSEnumMember,
TSInterfaceBody,
TSInterfaceHeritage,
TSTypeReference,
TSThisType,
TSLiteralType,
TSInferType,
TSConditionalType,
TSUnionType,
TSIntersectionType,
TSMappedType,
TSTypeQuery,
TSTupleType,
TSFunctionType,
TsCallSignatureDeclaration,
TSAnyKeyword,
TSBigIntKeyword,
TSBooleanKeyword,
TSIntrinsicKeyword,
TSNeverKeyword,
TSNullKeyword,
TSNumberKeyword,
TSObjectKeyword,
TSStringKeyword,
TSSymbolKeyword,
TSUndefinedKeyword,
TSUnknownKeyword,
TSVoidKeyword,
}
impl From<AstNode> for u8 {
fn from(m: AstNode) -> u8 {
m as u8
}
}
pub enum AstProp {
// Base
Parent,
Range,
Type,
_InternalFlags, // Private
// Node
Alternate,
Argument,
Arguments,
Async,
Attributes,
Await,
Block,
Body,
Callee,
Cases,
Children,
ClosingElement,
ClosingFragment,
Computed,
Consequent,
Declarations,
Declare,
Definite,
Delegate,
Discrimininant,
Elements,
Expression,
Expressions,
Finalizer,
Flags,
Generator,
Handler,
Id,
Init,
Key,
Kind,
Label,
Left,
Members,
Meta,
Method,
Name,
Namespace,
Object,
OpeningElement,
OpeningFragment,
Operator,
Optional,
Params,
Pattern,
Prefix,
Properties,
Property,
Quasi,
Quasis,
Raw,
ReturnType,
Right,
SelfClosing,
Shorthand,
Source,
Specifiers,
Tag,
Tail,
Test,
TypeAnnotation,
TypeArguments,
TypeParameters,
Update,
Value,
}
impl From<AstProp> for u8 {
fn from(m: AstProp) -> u8 {
m as u8
}
}
const MASK_U32_1: u32 = 0b11111111_00000000_00000000_00000000;
const MASK_U32_2: u32 = 0b00000000_11111111_00000000_00000000;
const MASK_U32_3: u32 = 0b00000000_00000000_11111111_00000000;
const MASK_U32_4: u32 = 0b00000000_00000000_00000000_11111111;
pub fn append_u32(result: &mut Vec<u8>, value: u32) {
let v1: u8 = ((value & MASK_U32_1) >> 24) as u8;
let v2: u8 = ((value & MASK_U32_2) >> 16) as u8;
let v3: u8 = ((value & MASK_U32_3) >> 8) as u8;
let v4: u8 = (value & MASK_U32_4) as u8;
result.push(v1);
result.push(v2);
result.push(v3);
result.push(v4);
}
pub fn append_usize(result: &mut Vec<u8>, value: usize) {
let raw = u32::try_from(value).unwrap();
append_u32(result, raw);
}
pub fn write_usize(result: &mut [u8], value: usize, idx: usize) {
let raw = u32::try_from(value).unwrap();
let v1: u8 = ((raw & MASK_U32_1) >> 24) as u8;
let v2: u8 = ((raw & MASK_U32_2) >> 16) as u8;
let v3: u8 = ((raw & MASK_U32_3) >> 8) as u8;
let v4: u8 = (raw & MASK_U32_4) as u8;
result[idx] = v1;
result[idx + 1] = v2;
result[idx + 2] = v3;
result[idx + 3] = v4;
}
#[derive(Debug, Clone)]
pub struct FlagValue(u8);
impl FlagValue {
pub fn new() -> Self {
Self(0)
}
pub fn set(&mut self, flag: Flag) {
let value: u8 = flag.into();
self.0 |= value;
}
}
#[derive(Debug)]
pub struct StringTable {
id: usize,
table: IndexMap<String, usize>,
}
impl StringTable {
pub fn new() -> Self {
Self {
id: 0,
table: IndexMap::new(),
}
}
pub fn insert(&mut self, s: &str) -> usize {
if let Some(id) = self.table.get(s) {
return *id;
}
let id = self.id;
self.id += 1;
self.table.insert(s.to_string(), id);
id
}
pub fn serialize(&mut self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
append_u32(&mut result, self.table.len() as u32);
// Assume that it's sorted by id
for (s, _id) in &self.table {
let bytes = s.as_bytes();
append_u32(&mut result, bytes.len() as u32);
result.append(&mut bytes.to_vec());
}
// eprintln!("Serialized string table: {:#?}", result);
result
}
}
#[derive(Debug)]
pub struct SerializeCtx {
pub id: usize,
pub id_to_offset: IndexMap<usize, usize>,
pub buf: Vec<u8>,
pub str_table: StringTable,
}
impl SerializeCtx {
pub fn new() -> Self {
let mut ctx = Self {
id: 0,
id_to_offset: IndexMap::new(),
buf: vec![],
str_table: StringTable::new(),
};
ctx.str_table.insert("");
// Placeholder node
ctx.push_node(AstNode::Invalid, 0, &DUMMY_SP);
ctx
}
pub fn next_id(&mut self) -> usize {
let id = self.id;
self.id += 1;
id
}
pub fn write_u8(&mut self, value: u8) {
self.buf.push(value);
}
pub fn write_node(
&mut self,
id: usize,
kind: AstNode,
parent_id: usize,
span: &Span,
) {
self.id_to_offset.insert(id, self.buf.len());
let kind_value: u8 = kind.into();
self.buf.push(kind_value);
append_usize(&mut self.buf, parent_id);
// Span
append_u32(&mut self.buf, span.lo.0);
append_u32(&mut self.buf, span.hi.0);
}
pub fn write_ids<I>(&mut self, prop: AstProp, ids: I)
where
I: IntoIterator<Item = usize>,
{
self.buf.push(prop.into());
let mut count = 0;
let idx = self.buf.len();
append_usize(&mut self.buf, 0);
for id in ids {
append_usize(&mut self.buf, id);
count += 1;
}
write_usize(&mut self.buf, count, idx);
}
pub fn write_id(&mut self, id: usize) {
append_usize(&mut self.buf, id);
}
pub fn write_flags(&mut self, flags: &FlagValue) {
self.buf.push(AstProp::_InternalFlags.into());
self.buf.push(flags.0)
}
pub fn write_prop(&mut self, prop: AstProp, id: usize) {
self.buf.push(prop.into());
append_usize(&mut self.buf, id);
}
pub fn push_node(
&mut self,
kind: AstNode,
parent_id: usize,
span: &Span,
) -> usize {
let id = self.id;
self.id_to_offset.insert(id, self.buf.len());
self.id += 1;
self.write_node(id, kind, parent_id, span);
id
}
}

View file

@ -53,11 +53,12 @@ use crate::util::fs::canonicalize_path;
use crate::util::path::is_script_ext;
use crate::util::sync::AtomicFlag;
mod ast;
mod ast_buf;
mod linter;
mod plugins;
mod reporters;
mod rules;
mod swc;
pub use linter::CliLinter;
pub use linter::CliLinterOptions;

View file

@ -32,7 +32,7 @@ use crate::args::DenoSubcommand;
use crate::args::Flags;
use crate::args::LintFlags;
use crate::factory::CliFactory;
use crate::tools::lint::ast::serialize_ast_bin;
use crate::tools::lint::swc::serialize_ast_bin;
#[derive(Debug)]
pub enum PluginRunnerRequest {

File diff suppressed because it is too large Load diff

View file

@ -6379,7 +6379,7 @@ declare namespace Deno {
arguments: Array<Expression | SpreadElement>;
callee: Expression;
// FIXME
// typeArguments: TSTypeParameterInstantiation | null;
typeArguments: null;
}
export interface ObjectExpression extends BaseNode {