Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

kmdr-ast

Package Overview
Dependencies
Maintainers
1
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kmdr-ast - npm Package Compare versions

Comparing version 3.1.0 to 3.2.0

1

dist/astNode.d.ts
import { Node, NodePoint } from "./interfaces";
export default class ASTNode implements Node {
readonly childCount?: number;
readonly children?: Node[];

@@ -4,0 +5,0 @@ readonly endPosition: NodePoint;

3

dist/astNode.js

@@ -5,3 +5,4 @@ "use strict";

constructor(node, parent) {
const { children, endPosition, hasError, isMissing, isNamed, startPosition, type, text } = node;
const { childCount, children, endPosition, hasError, isMissing, isNamed, startPosition, type, text } = node;
this.childCount = childCount;
this.endPosition = endPosition;

@@ -8,0 +9,0 @@ this.hasError = hasError;

@@ -6,5 +6,7 @@ import { NodePoint } from "./interfaces";

export default class ASTNodePoint implements NodePoint {
static areEqualRanges(range1: ASTNodePoint[], range2: ASTNodePoint[]): boolean;
static deserializeRange(serialized: string): NodePoint[];
static isInRange(range: ASTNodePoint[], point: ASTNodePoint): boolean;
static rangeContainsRange(range: ASTNodePoint[], subrange: ASTNodePoint[]): boolean;
static areEqualRanges(range1: ASTNodePoint[], range2: ASTNodePoint[]): boolean;
static serializeRange(startPosition: ASTNodePoint, endPosition: ASTNodePoint): string;
column: number;

@@ -11,0 +13,0 @@ row: number;

@@ -12,2 +12,17 @@ "use strict";

}
static areEqualRanges(range1, range2) {
return (range1[0].row === range2[0].row &&
range1[0].column === range2[0].column &&
range1[1].row === range2[1].row &&
range1[1].column === range2[1].column);
}
static deserializeRange(serialized) {
const [start, end] = serialized.split("-");
const startPosition = start.split(",").map(n => parseInt(n, 10));
const endPosition = end.split(",").map(n => parseInt(n, 10));
return [
new ASTNodePoint({ row: startPosition[0], column: startPosition[1] }),
new ASTNodePoint({ row: endPosition[0], column: endPosition[1] })
];
}
static isInRange(range, point) {

@@ -26,7 +41,4 @@ const [startPosition, endPosition] = range;

}
static areEqualRanges(range1, range2) {
return (range1[0].row === range2[0].row &&
range1[0].column === range2[0].column &&
range1[1].row === range2[1].row &&
range1[1].column === range2[1].column);
static serializeRange(startPosition, endPosition) {
return `${startPosition.row},${startPosition.column}-${endPosition.row},${endPosition.column}`;
}

@@ -33,0 +45,0 @@ }

@@ -85,3 +85,3 @@ "use strict";

if (matches.length === 0) {
decoratedStrings.push(this.decorateText(text, node.type));
decoratedStrings.push(this.decorateText(text, node.type, node));
}

@@ -111,3 +111,3 @@ else {

columnInNode === matches[currentToken].endPosition.column - 1) {
decoratedStrings.push(this.decorateText(wordInRange, matches[currentToken].type, matches[currentToken]));
decoratedStrings.push(this.decorateText(wordInRange, matches[currentToken].type, node, matches[currentToken]));
wordInRange = "";

@@ -120,3 +120,3 @@ currentToken += 1;

if (inRange) {
decoratedStrings.push(this.decorateText(wordInRange, matches[currentToken].type, matches[currentToken]));
decoratedStrings.push(this.decorateText(wordInRange, matches[currentToken].type, node, matches[currentToken]));
wordInRange = "";

@@ -128,3 +128,3 @@ currentToken += 1;

// decoratedStrings.push(char);
decoratedStrings.push(this.decorateText(char, node.type));
decoratedStrings.push(this.decorateText(char, node.type, node));
}

@@ -135,6 +135,6 @@ }

}
decorateText(text, type, definition) {
decorateText(text, type, node, definition) {
switch (type) {
case "`":
return this.decorators.backtick(text);
return this.decorators.backtick(text, node);
case `"`:

@@ -141,0 +141,0 @@ return this.decorators.doubleQuotes(text);

@@ -7,2 +7,3 @@ import { ArgumentSchema, OptionSchema, ProgramSchema, SubcommandSchema } from "kmdr-parser";

export interface Node {
childCount?: number;
children?: Node[];

@@ -25,38 +26,38 @@ endPosition: NodePoint;

export interface Decorators<T extends Element | Text | string> {
arithmeticOperator(text: string, definition?: NodeDefinition): T;
backtick(text: string, definition?: NodeDefinition): T;
bitwiseOperator(text: string, definition?: NodeDefinition): T;
braces(text: string, definition?: NodeDefinition): T;
brackets(text: string, definition?: NodeDefinition): T;
command(text: string): T;
comment(text: string, definition?: NodeDefinition): T;
do(text: string, definition?: NodeDefinition): T;
doubleQuotes(text: string, definition?: NodeDefinition): T;
done(text: string, definition?: NodeDefinition): T;
else(text: string, definition?: NodeDefinition): T;
equal(text: string, definition?: NodeDefinition): T;
fi(text: string, definition?: NodeDefinition): T;
fileDescriptor(text: string, definition?: NodeDefinition): T;
fn(text: string, definition?: NodeDefinition): T;
for(text: string, definition?: NodeDefinition): T;
if(text: string, definition?: NodeDefinition): T;
in(text: string, definition?: NodeDefinition): T;
arithmeticOperator(text: string, node?: Node, definition?: NodeDefinition): T;
backtick(text: string, node?: Node, definition?: NodeDefinition): T;
bitwiseOperator(text: string, node?: Node, definition?: NodeDefinition): T;
braces(text: string, node?: Node, definition?: NodeDefinition): T;
brackets(text: string, node?: Node, definition?: NodeDefinition): T;
command(text: string, node?: Node): T;
comment(text: string, node?: Node, definition?: NodeDefinition): T;
do(text: string, node?: Node, definition?: NodeDefinition): T;
doubleQuotes(text: string, node?: Node, definition?: NodeDefinition): T;
done(text: string, node?: Node, definition?: NodeDefinition): T;
else(text: string, node?: Node, definition?: NodeDefinition): T;
equal(text: string, node?: Node, definition?: NodeDefinition): T;
fi(text: string, node?: Node, definition?: NodeDefinition): T;
fileDescriptor(text: string, node?: Node, definition?: NodeDefinition): T;
fn(text: string, node?: Node, definition?: NodeDefinition): T;
for(text: string, node?: Node, definition?: NodeDefinition): T;
if(text: string, node?: Node, definition?: NodeDefinition): T;
in(text: string, node?: Node, definition?: NodeDefinition): T;
newLine(): T;
option(text: string, definition?: NodeDefinition): T;
optionArg(text: string, definition?: NodeDefinition): T;
logicalOperator(text: string, definition?: NodeDefinition): T;
parens(test: string, definition?: NodeDefinition): T;
pipeline(text: string, definition?: NodeDefinition): T;
program(text: string, definition?: NodeDefinition): T;
redirect(text: string, definition?: NodeDefinition): T;
relationalOperator(text: string, definition?: NodeDefinition): T;
semicolon(text: string, definition?: NodeDefinition): T;
option(text: string, node?: Node, definition?: NodeDefinition): T;
optionArg(text: string, node?: Node, definition?: NodeDefinition): T;
logicalOperator(text: string, node?: Node, definition?: NodeDefinition): T;
parens(test: string, node?: Node, definition?: NodeDefinition): T;
pipeline(text: string, node?: Node, definition?: NodeDefinition): T;
program(text: string, node?: Node, definition?: NodeDefinition): T;
redirect(text: string, node?: Node, definition?: NodeDefinition): T;
relationalOperator(text: string, node?: Node, definition?: NodeDefinition): T;
semicolon(text: string, node?: Node, definition?: NodeDefinition): T;
space(): T;
subcommand(text: string, definition?: NodeDefinition): T;
testOperator(text: string, definition?: NodeDefinition): T;
then(text: string, definition?: NodeDefinition): T;
variableName(text: string, definition?: NodeDefinition): T;
while(text: string, definition?: NodeDefinition): T;
word(text: string, definition?: NodeDefinition): T;
subcommand(text: string, node?: Node, definition?: NodeDefinition): T;
testOperator(text: string, node?: Node, definition?: NodeDefinition): T;
then(text: string, node?: Node, definition?: NodeDefinition): T;
variableName(text: string, node?: Node, definition?: NodeDefinition): T;
while(text: string, node?: Node, definition?: NodeDefinition): T;
word(text: string, node?: Node, definition?: NodeDefinition): T;
}
//# sourceMappingURL=interfaces.d.ts.map
{
"name": "kmdr-ast",
"version": "3.1.0",
"version": "3.2.0",
"description": "Traverse the AST of an explanation by kmdr",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

import { Node, NodePoint } from "./interfaces";
export default class ASTNode implements Node {
public readonly childrenCount?: number;
public readonly childCount?: number;
public readonly children?: Node[];

@@ -17,3 +17,3 @@ public readonly endPosition: NodePoint;

const {
childrenCount,
childCount,
children,

@@ -29,3 +29,3 @@ endPosition,

this.childrenCount = childrenCount;
this.childCount = childCount;
this.endPosition = endPosition;

@@ -32,0 +32,0 @@ this.hasError = hasError;

@@ -8,2 +8,26 @@ import { NodePoint } from "./interfaces";

export default class ASTNodePoint implements NodePoint {
public static areEqualRanges(
range1: ASTNodePoint[],
range2: ASTNodePoint[]
): boolean {
return (
range1[0].row === range2[0].row &&
range1[0].column === range2[0].column &&
range1[1].row === range2[1].row &&
range1[1].column === range2[1].column
);
}
public static deserializeRange(serialized: string): NodePoint[] {
const [start, end] = serialized.split("-");
const startPosition = start.split(",").map(n => parseInt(n, 10));
const endPosition = end.split(",").map(n => parseInt(n, 10));
return [
new ASTNodePoint({ row: startPosition[0], column: startPosition[1] }),
new ASTNodePoint({ row: endPosition[0], column: endPosition[1] })
];
}
public static isInRange(range: ASTNodePoint[], point: ASTNodePoint): boolean {

@@ -32,12 +56,7 @@ const [startPosition, endPosition] = range;

public static areEqualRanges(
range1: ASTNodePoint[],
range2: ASTNodePoint[]
): boolean {
return (
range1[0].row === range2[0].row &&
range1[0].column === range2[0].column &&
range1[1].row === range2[1].row &&
range1[1].column === range2[1].column
);
public static serializeRange(
startPosition: ASTNodePoint,
endPosition: ASTNodePoint
): string {
return `${startPosition.row},${startPosition.column}-${endPosition.row},${endPosition.column}`;
}

@@ -44,0 +63,0 @@

@@ -122,3 +122,3 @@ import { ASTNode } from ".";

if (matches.length === 0) {
decoratedStrings.push(this.decorateText(text, node.type));
decoratedStrings.push(this.decorateText(text, node.type, node));
} else {

@@ -160,2 +160,3 @@ for (let column = 0; column < text.length; column++) {

matches[currentToken].type,
node,
matches[currentToken]

@@ -174,2 +175,3 @@ )

matches[currentToken].type,
node,
matches[currentToken]

@@ -184,3 +186,3 @@ )

// decoratedStrings.push(char);
decoratedStrings.push(this.decorateText(char, node.type));
decoratedStrings.push(this.decorateText(char, node.type, node));
}

@@ -196,2 +198,3 @@ }

type: string,
node: ASTNode,
definition?: NodeDefinition

@@ -201,3 +204,3 @@ ): R {

case "`":
return this.decorators.backtick(text);
return this.decorators.backtick(text, node);
case `"`:

@@ -204,0 +207,0 @@ return this.decorators.doubleQuotes(text);

@@ -14,3 +14,3 @@ import {

export interface Node {
childrenCount?: number;
childCount?: number;
children?: Node[];

@@ -35,37 +35,37 @@ endPosition: NodePoint;

export interface Decorators<T extends Element | Text | string> {
arithmeticOperator(text: string, definition?: NodeDefinition): T;
backtick(text: string, definition?: NodeDefinition): T;
bitwiseOperator(text: string, definition?: NodeDefinition): T;
braces(text: string, definition?: NodeDefinition): T;
brackets(text: string, definition?: NodeDefinition): T;
command(text: string): T;
comment(text: string, definition?: NodeDefinition): T;
do(text: string, definition?: NodeDefinition): T;
doubleQuotes(text: string, definition?: NodeDefinition): T;
done(text: string, definition?: NodeDefinition): T;
else(text: string, definition?: NodeDefinition): T;
equal(text: string, definition?: NodeDefinition): T;
fi(text: string, definition?: NodeDefinition): T;
fileDescriptor(text: string, definition?: NodeDefinition): T;
fn(text: string, definition?: NodeDefinition): T;
for(text: string, definition?: NodeDefinition): T;
if(text: string, definition?: NodeDefinition): T;
in(text: string, definition?: NodeDefinition): T;
arithmeticOperator(text: string, node?: Node, definition?: NodeDefinition): T;
backtick(text: string, node?: Node, definition?: NodeDefinition): T;
bitwiseOperator(text: string, node?: Node, definition?: NodeDefinition): T;
braces(text: string, node?: Node, definition?: NodeDefinition): T;
brackets(text: string, node?: Node, definition?: NodeDefinition): T;
command(text: string, node?: Node): T;
comment(text: string, node?: Node, definition?: NodeDefinition): T;
do(text: string, node?: Node, definition?: NodeDefinition): T;
doubleQuotes(text: string, node?: Node, definition?: NodeDefinition): T;
done(text: string, node?: Node, definition?: NodeDefinition): T;
else(text: string, node?: Node, definition?: NodeDefinition): T;
equal(text: string, node?: Node, definition?: NodeDefinition): T;
fi(text: string, node?: Node, definition?: NodeDefinition): T;
fileDescriptor(text: string, node?: Node, definition?: NodeDefinition): T;
fn(text: string, node?: Node, definition?: NodeDefinition): T;
for(text: string, node?: Node, definition?: NodeDefinition): T;
if(text: string, node?: Node, definition?: NodeDefinition): T;
in(text: string, node?: Node, definition?: NodeDefinition): T;
newLine(): T;
option(text: string, definition?: NodeDefinition): T;
optionArg(text: string, definition?: NodeDefinition): T;
logicalOperator(text: string, definition?: NodeDefinition): T;
parens(test: string, definition?: NodeDefinition): T;
pipeline(text: string, definition?: NodeDefinition): T;
program(text: string, definition?: NodeDefinition): T;
redirect(text: string, definition?: NodeDefinition): T;
relationalOperator(text: string, definition?: NodeDefinition): T;
semicolon(text: string, definition?: NodeDefinition): T;
option(text: string, node?: Node, definition?: NodeDefinition): T;
optionArg(text: string, node?: Node, definition?: NodeDefinition): T;
logicalOperator(text: string, node?: Node, definition?: NodeDefinition): T;
parens(test: string, node?: Node, definition?: NodeDefinition): T;
pipeline(text: string, node?: Node, definition?: NodeDefinition): T;
program(text: string, node?: Node, definition?: NodeDefinition): T;
redirect(text: string, node?: Node, definition?: NodeDefinition): T;
relationalOperator(text: string, node?: Node, definition?: NodeDefinition): T;
semicolon(text: string, node?: Node, definition?: NodeDefinition): T;
space(): T;
subcommand(text: string, definition?: NodeDefinition): T;
testOperator(text: string, definition?: NodeDefinition): T;
then(text: string, definition?: NodeDefinition): T;
variableName(text: string, definition?: NodeDefinition): T;
while(text: string, definition?: NodeDefinition): T;
word(text: string, definition?: NodeDefinition): T;
subcommand(text: string, node?: Node, definition?: NodeDefinition): T;
testOperator(text: string, node?: Node, definition?: NodeDefinition): T;
then(text: string, node?: Node, definition?: NodeDefinition): T;
variableName(text: string, node?: Node, definition?: NodeDefinition): T;
while(text: string, node?: Node, definition?: NodeDefinition): T;
word(text: string, node?: Node, definition?: NodeDefinition): T;
}

@@ -92,3 +92,24 @@ import ASTNodePoint from "../src/astNodePoint";

});
test("a range is serialized", () => {
const startPosition = new ASTNodePoint({ column: 0, row: 0 });
const endPosition = new ASTNodePoint({ column: 10, row: 0 });
expect(ASTNodePoint.serializeRange(startPosition, endPosition)).toEqual(
"0,0-0,10"
);
});
test("a range is deserialized", () => {
const serialized = "2,10-2,100";
const startPosition = new ASTNodePoint({ column: 10, row: 2 });
const endPosition = new ASTNodePoint({ column: 100, row: 2 });
expect(ASTNodePoint.deserializeRange(serialized)).toEqual([
startPosition,
endPosition
]);
});
});
});

@@ -1,77 +0,89 @@

import { Decorators, NodeDefinition } from "../src/interfaces";
import { Decorators, Node, NodeDefinition } from "../src/interfaces";
export default class ConsoleDecorators implements Decorators<string> {
public arithmeticOperator(text: string, definition?: NodeDefinition) {
public arithmeticOperator(
text: string,
node: Node,
definition?: NodeDefinition
) {
return `\u001b[97m${text}\u001b[0m`;
}
public backtick(text: string) {
public backtick(text: string, node: Node) {
return `\u001b[97m${text}\u001b[0m`;
}
public bitwiseOperator(text: string, definition?: NodeDefinition) {
public bitwiseOperator(
text: string,
node: Node,
definition?: NodeDefinition
) {
return `\u001b[97m${text}\u001b[0m`;
}
public braces(text: string) {
public braces(text: string, node: Node) {
return `\u001b[92m${text}\u001b[0m`;
}
public brackets(text: string) {
public brackets(text: string, node: Node) {
return `\u001b[92m${text}\u001b[0m`;
}
public command(text: string) {
public command(text: string, node: Node) {
return text;
}
public comment(text: string, definition?: NodeDefinition) {
public comment(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[90m${text}\u001b[0m`;
}
public do(text: string, definition?: NodeDefinition) {
public do(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[35m${text}\u001b[0m`;
}
public doubleQuotes(text: string, definition?: NodeDefinition) {
public doubleQuotes(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[35m${text}\u001b[0m`;
}
public done(text: string, definition?: NodeDefinition) {
public done(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[35m${text}\u001b[0m`;
}
public else(text: string, definition?: NodeDefinition) {
public else(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[35m${text}\u001b[0m`;
}
public equal(text: string, definition?: NodeDefinition) {
public equal(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[35m${text}\u001b[0m`;
}
public fi(text: string, definition?: NodeDefinition) {
public fi(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[35m${text}\u001b[0m`;
}
public fileDescriptor(text: string, definition?: NodeDefinition) {
public fileDescriptor(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[96m${text}\u001b[0m`;
}
public fn(text: string, definition?: NodeDefinition) {
public fn(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[35m${text}\u001b[0m`;
}
public for(text: string, definition?: NodeDefinition) {
public for(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[35m${text}\u001b[0m`;
}
public if(text: string, definition?: NodeDefinition) {
public if(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[35m${text}\u001b[0m`;
}
public in(text: string, definition?: NodeDefinition) {
public in(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[35m${text}\u001b[0m`;
}
public logicalOperator(text: string, definition?: NodeDefinition) {
public logicalOperator(
text: string,
node: Node,
definition?: NodeDefinition
) {
return `\u001b[34m${text}\u001b[0m`;

@@ -84,31 +96,35 @@ }

public option(text: string, definition?: NodeDefinition) {
public option(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[35m${text}\u001b[0m`;
}
public optionArg(text: string, definition?: NodeDefinition) {
public optionArg(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[31m${text}\u001b[0m`;
}
public parens(text: string) {
public parens(text: string, node: Node) {
return `\u001b[35m${text}\u001b[0m`;
}
public pipeline(text: string, definition?: NodeDefinition) {
public pipeline(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[91m${text}\u001b[0m`;
}
public program(text: string, definition?: NodeDefinition) {
public program(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[32;1m${text}\u001b[0m`;
}
public redirect(text: string, definition?: NodeDefinition) {
public redirect(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[34m${text}\u001b[0m`;
}
public relationalOperator(text: string, definition?: NodeDefinition) {
public relationalOperator(
text: string,
node: Node,
definition?: NodeDefinition
) {
return `\u001b[97m${text}\u001b[0m`;
}
public semicolon(text: string, definition?: NodeDefinition) {
public semicolon(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[94m${text}\u001b[0m`;

@@ -121,25 +137,25 @@ }

public subcommand(text: string, definition?: NodeDefinition) {
public subcommand(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[36;1m${text}\u001b[0m`;
}
public testOperator(text: string, definition?: NodeDefinition) {
public testOperator(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[97m${text}\u001b[0m`;
}
public then(text: string, definition?: NodeDefinition) {
public then(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[35m${text}\u001b[0m`;
}
public variableName(text: string, definition?: NodeDefinition) {
public variableName(text: string, node: Node, definition?: NodeDefinition) {
return `\u001b[36;1m${text}\u001b[0m`;
}
public while(text: string) {
public while(text: string, node: Node) {
return `\u001b[35m${text}\u001b[0m`;
}
public word(text: string, definition?: NodeDefinition) {
public word(text: string, node: Node, definition?: NodeDefinition) {
return text;
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc