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

@scaffdog/engine

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@scaffdog/engine - npm Package Compare versions

Comparing version 1.0.0-canary.0 to 1.0.0-canary.1

6

CHANGELOG.md

@@ -6,2 +6,8 @@ # Change Log

# [1.0.0-canary.1](https://github.com/cats-oss/scaffdog/compare/v1.0.0-canary.0...v1.0.0-canary.1) (2021-01-12)
### Features
- improve error message ([4b66723](https://github.com/cats-oss/scaffdog/commit/4b667232ecd6cef7e33a145e7d0f35e8afb2fb81))
# [1.0.0-canary.0](https://github.com/cats-oss/scaffdog/compare/v0.3.0...v1.0.0-canary.0) (2021-01-11)

@@ -8,0 +14,0 @@

18

lib/ast.d.ts

@@ -0,3 +1,5 @@

import type { SourceLocation } from '@scaffdog/types';
import type { Token } from './tokens';
export declare type Node = {
loc: SourceLocation;
toString(): string;

@@ -7,3 +9,4 @@ };

name: string;
constructor(name: string);
loc: SourceLocation;
constructor(name: string, loc: SourceLocation);
toString(): string;

@@ -13,3 +16,4 @@ }

value: string | number | boolean | undefined | null;
constructor(value: string | number | boolean | undefined | null);
loc: SourceLocation;
constructor(value: string | number | boolean | undefined | null, loc: SourceLocation);
toString(): string;

@@ -19,3 +23,4 @@ }

value: string;
constructor(value: string);
loc: SourceLocation;
constructor(value: string, loc: SourceLocation);
toString(): string;

@@ -25,2 +30,3 @@ }

ident: Ident;
loc: SourceLocation;
constructor(ident: Ident);

@@ -31,2 +37,3 @@ toString(): string;

literal: Literal;
loc: SourceLocation;
constructor(literal: Literal);

@@ -38,2 +45,3 @@ toString(): string;

property: Node;
loc: SourceLocation;
constructor(object: Node, property: Node);

@@ -45,3 +53,4 @@ toString(): string;

args: Node[];
constructor(name: string, args: Node[]);
loc: SourceLocation;
constructor(name: string, args: Node[], loc: SourceLocation);
toString(): string;

@@ -53,2 +62,3 @@ }

expressions: Node[];
loc: SourceLocation;
constructor(open: Token<'OPEN_TAG'>, close: Token<'CLOSE_TAG'>, expressions: Node[]);

@@ -55,0 +65,0 @@ isOpenTrim(): boolean;

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

class Ident {
constructor(name) {
constructor(name, loc) {
this.name = name;
this.loc = loc;
}

@@ -15,4 +16,5 @@ toString() {

class Literal {
constructor(value) {
constructor(value, loc) {
this.value = value;
this.loc = loc;
}

@@ -25,4 +27,5 @@ toString() {

class RawExpr {
constructor(value) {
constructor(value, loc) {
this.value = value;
this.loc = loc;
}

@@ -37,2 +40,3 @@ toString() {

this.ident = ident;
this.loc = ident.loc;
}

@@ -47,2 +51,3 @@ toString() {

this.literal = literal;
this.loc = literal.loc;
}

@@ -58,2 +63,6 @@ toString() {

this.property = property;
this.loc = {
start: object.loc.start,
end: property.loc.end,
};
}

@@ -66,5 +75,6 @@ toString() {

class CallExpr {
constructor(name, args) {
constructor(name, args, loc) {
this.name = name;
this.args = args;
this.loc = loc;
}

@@ -82,2 +92,6 @@ toString() {

this.expressions = expressions;
this.loc = {
start: open.loc.start,
end: open.loc.end,
};
}

@@ -84,0 +98,0 @@ isOpenTrim() {

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

const parser = new parser_1.Parser(tokenize_1.tokenize(input), input);
const compiler = new compiler_1.Compiler(context);
const compiler = new compiler_1.Compiler(context, input);
const ast = parser.parse();

@@ -12,0 +12,0 @@ const output = compiler.compile(ast);

@@ -5,3 +5,4 @@ import type { Context } from '@scaffdog/types';

private _context;
constructor(context: Context);
private _source;
constructor(context: Context, source: string);
compile(ast: Node[]): string;

@@ -13,2 +14,3 @@ private _compileTagExpr;

private _compileCallExpr;
private _error;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Compiler = void 0;
const error_1 = require("@scaffdog/error");
const is_plain_object_1 = require("is-plain-object");
const ast_1 = require("./ast");
class Compiler {
constructor(context) {
constructor(context, source) {
this._context = context;
this._source = source;
}

@@ -69,3 +71,3 @@ compile(ast) {

}
throw new Error(`"${expr.ident.name}" identifier does not exist.`);
throw this._error(`"${expr.ident.name}" identifier does not exist`, expr);
}

@@ -92,3 +94,3 @@ _compileMemberExpr(expr) {

default:
throw new Error(`[${expr.property.literal}] is invalid property`);
throw this._error(`[${expr.property.literal}] is invalid property`, expr.property);
}

@@ -100,3 +102,3 @@ }

if (object === null || property === null) {
throw new Error(`"${expr.toString()}" is invalid member access`);
throw this._error(`"${expr.toString()}" is invalid member access`, expr);
}

@@ -126,3 +128,3 @@ switch (typeof property) {

if (helper == null) {
throw new Error(`"${expr.name}" function does not exist.`);
throw this._error(`"${expr.name}" function does not exist`, expr);
}

@@ -142,7 +144,13 @@ const args = expr.args.map((expr) => {

}
throw new Error(`"${expr}" is invalid argument`);
throw this._error(`"${expr}" is invalid argument`, expr);
});
return helper(this._context, ...args);
}
_error(msg, node) {
return error_1.error(msg, {
source: this._source,
loc: node.loc,
});
}
}
exports.Compiler = Compiler;

@@ -7,3 +7,6 @@ "use strict";

const tokens_1 = require("./tokens");
const eofToken = tokens_1.createToken('EOF', null, { line: 0, column: 0 }, { line: 0, column: 0 });
const eofToken = tokens_1.createToken('EOF', null, {
start: { line: 0, column: 0 },
end: { line: 0, column: 0 },
});
class Parser {

@@ -27,3 +30,3 @@ constructor(tokens, source) {

case 'STRING':
ast.push(new ast_1.RawExpr(this._current.literal));
ast.push(new ast_1.RawExpr(this._current.literal, this._current.loc));
break;

@@ -72,3 +75,3 @@ }

case 'OPEN_BRACKET': {
const object = new ast_1.IdentExpr(new ast_1.Ident(this._current.literal));
const object = new ast_1.IdentExpr(new ast_1.Ident(this._current.literal, this._current.loc));
this._bump();

@@ -80,6 +83,6 @@ expressions.push(this._parseMemberExpr(object));

if (passedPipe) {
expressions.push(new ast_1.CallExpr(this._current.literal, []));
expressions.push(new ast_1.CallExpr(this._current.literal, [], this._current.loc));
}
else {
expressions.push(new ast_1.IdentExpr(new ast_1.Ident(this._current.literal)));
expressions.push(new ast_1.IdentExpr(new ast_1.Ident(this._current.literal, this._current.loc)));
}

@@ -93,3 +96,3 @@ }

case 'NUMBER':
expressions.push(new ast_1.LiteralExpr(new ast_1.Literal(this._current.literal)));
expressions.push(new ast_1.LiteralExpr(new ast_1.Literal(this._current.literal, this._current.loc)));
break;

@@ -129,6 +132,6 @@ case 'PIPE':

case 'NUMBER':
property = new ast_1.LiteralExpr(new ast_1.Literal(this._current.literal));
property = new ast_1.LiteralExpr(new ast_1.Literal(this._current.literal, this._current.loc));
break;
case 'IDENT':
property = new ast_1.IdentExpr(new ast_1.Ident(this._current.literal));
property = new ast_1.IdentExpr(new ast_1.Ident(this._current.literal, this._current.loc));
break;

@@ -156,3 +159,3 @@ default:

case 'IDENT':
property = new ast_1.LiteralExpr(new ast_1.Literal(this._current.literal));
property = new ast_1.LiteralExpr(new ast_1.Literal(this._current.literal, this._current.loc));
break;

@@ -174,2 +177,3 @@ default:

const name = this._current.literal;
const loc = this._current.loc;
const args = [];

@@ -179,6 +183,6 @@ this._bump();

if (this._current.type === 'IDENT') {
args.push(new ast_1.IdentExpr(new ast_1.Ident(this._current.literal)));
args.push(new ast_1.IdentExpr(new ast_1.Ident(this._current.literal, this._current.loc)));
}
else {
args.push(new ast_1.LiteralExpr(new ast_1.Literal(this._current.literal)));
args.push(new ast_1.LiteralExpr(new ast_1.Literal(this._current.literal, this._current.loc)));
}

@@ -188,3 +192,3 @@ switch (this._next.type) {

case 'PIPE':
return new ast_1.CallExpr(name, args);
return new ast_1.CallExpr(name, args, loc);
default:

@@ -199,4 +203,3 @@ this._bump();

source: this._source,
start: token.start,
end: token.end,
loc: token.loc,
});

@@ -203,0 +206,0 @@ }

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

source: input,
start: loc,
end: loc,
loc,
});

@@ -39,4 +38,8 @@ }

source: input,
start: last != null ? last.start : { line: 1, column: 1 },
end: last != null ? last.end : { line: 1, column: 1 },
loc: last != null
? last.loc
: {
start: { line: 1, column: 1 },
end: { line: 1, column: 1 },
},
});

@@ -47,8 +50,7 @@ };

source: input,
start: token.start,
end: token.end,
loc: token.loc,
});
};
const parseNumeric = (value) => Number(value);
const tokenizeUsingEsprima = ({ source, input, loc }) => {
const tokenizeUsingEsprima = ({ source, input, pos }) => {
try {

@@ -58,35 +60,41 @@ return esprima.tokenize(input, { loc: true });

catch (e) {
const p = {
line: pos.line + e.lineNumber - 1,
column: pos.column + e.index - input.length,
};
unexpected(source, {
line: loc.line + e.lineNumber - 1,
column: loc.column + e.index - input.length,
start: p,
end: p,
});
}
};
const tokenizeInTag = ({ source, input, loc }) => {
const tokenizeInTag = ({ source, input, pos }) => {
const output = [];
const tokens = tokenizeUsingEsprima({ source, input, loc });
const tokens = tokenizeUsingEsprima({ source, input, pos });
const size = input.length;
const length = tokens.length;
let pos = 0;
let i = 0;
const endOfSource = (index) => index + 1 > length;
const lookahead = () => (endOfSource(pos + 1) ? null : tokens[pos + 1]);
while (!endOfSource(pos)) {
const token = tokens[pos];
const start = {
line: loc.line - (token.loc.start.line - 1),
column: loc.column - size + token.loc.start.column,
const lookahead = () => (endOfSource(i + 1) ? null : tokens[i + 1]);
while (!endOfSource(i)) {
const token = tokens[i];
const loc = {
start: {
line: pos.line - (token.loc.start.line - 1),
column: pos.column - size + token.loc.start.column,
},
end: {
line: pos.line - (token.loc.end.line - 1),
column: pos.column - size + token.loc.end.column - 1,
},
};
const end = {
line: loc.line - (token.loc.end.line - 1),
column: loc.column - size + token.loc.end.column - 1,
};
switch (token.type) {
case 'Null':
output.push(tokens_1.createToken('NULL', null, start, end));
output.push(tokens_1.createToken('NULL', null, loc));
break;
case 'String':
output.push(tokens_1.createToken('STRING', token.value.slice(1, token.value.length - 1), start, end));
output.push(tokens_1.createToken('STRING', token.value.slice(1, token.value.length - 1), loc));
break;
case 'Boolean':
output.push(tokens_1.createToken('BOOLEAN', token.value === 'true', start, end));
output.push(tokens_1.createToken('BOOLEAN', token.value === 'true', loc));
break;

@@ -97,3 +105,3 @@ case 'Numeric':

}
output.push(tokens_1.createToken('NUMBER', parseNumeric(token.value), start, end));
output.push(tokens_1.createToken('NUMBER', parseNumeric(token.value), loc));
break;

@@ -103,6 +111,6 @@ case 'Identifier':

case 'undefined':
output.push(tokens_1.createToken('UNDEFINED', undefined, start, end));
output.push(tokens_1.createToken('UNDEFINED', undefined, loc));
break;
default:
output.push(tokens_1.createToken('IDENT', token.value, start, end));
output.push(tokens_1.createToken('IDENT', token.value, loc));
}

@@ -117,9 +125,12 @@ break;

output.push(tokens_1.createToken('NUMBER', parseNumeric(`${token.value}${next.value}`), {
line: loc.line - (next.loc.start.line - 1),
column: loc.column - size + next.loc.start.column - 1,
}, {
line: loc.line - (next.loc.end.line - 1),
column: loc.column - size + next.loc.end.column - 1,
start: {
line: pos.line - (next.loc.start.line - 1),
column: pos.column - size + next.loc.start.column - 1,
},
end: {
line: pos.line - (next.loc.end.line - 1),
column: pos.column - size + next.loc.end.column - 1,
},
}));
pos++;
i++;
}

@@ -131,12 +142,12 @@ else {

case '|':
output.push(tokens_1.createToken('PIPE', '|', start, end));
output.push(tokens_1.createToken('PIPE', '|', loc));
break;
case '.':
output.push(tokens_1.createToken('DOT', '.', start, end));
output.push(tokens_1.createToken('DOT', '.', loc));
break;
case '[':
output.push(tokens_1.createToken('OPEN_BRACKET', '[', start, end));
output.push(tokens_1.createToken('OPEN_BRACKET', '[', loc));
break;
case ']':
output.push(tokens_1.createToken('CLOSE_BRACKET', ']', start, end));
output.push(tokens_1.createToken('CLOSE_BRACKET', ']', loc));
break;

@@ -150,3 +161,3 @@ default:

}
pos++;
i++;
}

@@ -158,25 +169,31 @@ return output;

const length = source.length;
const loc = { line: 1, column: 1 };
const pos = { line: 1, column: 1 };
const output = [];
const buffer = [];
let bufLoc = null;
const buf = [];
let bufPos = null;
let inTag = false;
let pos = 0;
let i = 0;
const endOfSource = (index) => index + 1 > length;
const lookahead = (n = 1) => (endOfSource(pos + n) ? '' : source[pos + n]);
const buf2str = () => buffer.join('');
const lookahead = (n = 1) => (endOfSource(i + n) ? '' : source[i + n]);
const buf2str = () => buf.join('');
const consumeBuffer = () => {
if (buffer.length > 0) {
output.push(tokens_1.createToken('STRING', buf2str(), bufLoc != null
? bufLoc
: {
line: 1,
column: 1,
}, { ...loc, column: loc.column - 1 }));
buffer.length = 0;
bufLoc = null;
if (buf.length > 0) {
output.push(tokens_1.createToken('STRING', buf2str(), {
start: bufPos != null
? bufPos
: {
line: 1,
column: 1,
},
end: {
...pos,
column: pos.column - 1,
},
}));
buf.length = 0;
bufPos = null;
}
};
while (!endOfSource(pos)) {
const str = source[pos];
while (!endOfSource(i)) {
const str = source[i];
switch (str) {

@@ -190,21 +207,27 @@ case '{':

consumeBuffer();
pos++;
loc.column++;
i++;
pos.column++;
if (lookahead() === '-') {
output.push(tokens_1.createToken('OPEN_TAG', '{{-', {
...loc,
column: loc.column - 1,
}, {
...loc,
column: loc.column + 1,
start: {
...pos,
column: pos.column - 1,
},
end: {
...pos,
column: pos.column + 1,
},
}));
pos++;
loc.column++;
i++;
pos.column++;
}
else {
output.push(tokens_1.createToken('OPEN_TAG', '{{', {
...loc,
column: loc.column - 1,
}, {
...loc,
start: {
...pos,
column: pos.column - 1,
},
end: {
...pos,
},
}));

@@ -214,3 +237,3 @@ }

else {
buffer.push(str);
buf.push(str);
}

@@ -220,5 +243,10 @@ break;

if (lookahead() === '}' && lookahead(2) === '}') {
const close = tokens_1.createToken('CLOSE_TAG', '-}}', { ...loc }, {
...loc,
column: loc.column + 2,
const close = tokens_1.createToken('CLOSE_TAG', '-}}', {
start: {
...pos,
},
end: {
...pos,
column: pos.column + 2,
},
});

@@ -229,8 +257,8 @@ if (!inTag) {

inTag = false;
output.push(...tokenizeInTag({ source: input, input: buf2str(), loc }), close);
pos += 2;
buffer.length = 0;
output.push(...tokenizeInTag({ source: input, input: buf2str(), pos }), close);
i += 2;
buf.length = 0;
}
else {
buffer.push(str);
buf.push(str);
}

@@ -240,5 +268,10 @@ break;

if (lookahead() === '}') {
const close = tokens_1.createToken('CLOSE_TAG', '}}', { ...loc }, {
...loc,
column: loc.column + 1,
const close = tokens_1.createToken('CLOSE_TAG', '}}', {
start: {
...pos,
},
end: {
...pos,
column: pos.column + 1,
},
});

@@ -249,20 +282,20 @@ if (!inTag) {

inTag = false;
output.push(...tokenizeInTag({ source: input, input: buf2str(), loc }), close);
pos++;
loc.column++;
buffer.length = 0;
output.push(...tokenizeInTag({ source: input, input: buf2str(), pos }), close);
i++;
pos.column++;
buf.length = 0;
}
else {
buffer.push(str);
buf.push(str);
}
break;
default:
buffer.push(str);
buf.push(str);
}
if (str === '\n') {
loc.line++;
loc.column = 0;
pos.line++;
pos.column = 0;
}
loc.column++;
pos++;
pos.column++;
i++;
}

@@ -269,0 +302,0 @@ if (inTag) {

@@ -0,1 +1,2 @@

import type { SourceLocation } from '@scaffdog/types';
export declare type TokenType = 'ILLEGAL' | 'EOF' | 'NULL' | 'UNDEFINED' | 'BOOLEAN' | 'STRING' | 'NUMBER' | 'IDENT' | 'DOT' | 'OPEN_BRACKET' | 'CLOSE_BRACKET' | 'PIPE' | 'OPEN_TAG' | 'CLOSE_TAG';

@@ -18,13 +19,8 @@ export declare type TokenMap = {

};
export declare type Loc = {
line: number;
column: number;
};
export declare type Token<T extends TokenType> = {
type: T;
literal: TokenMap[T];
start: Loc;
end: Loc;
loc: SourceLocation;
};
export declare type AnyToken = Token<'ILLEGAL'> | Token<'EOF'> | Token<'NULL'> | Token<'UNDEFINED'> | Token<'BOOLEAN'> | Token<'STRING'> | Token<'NUMBER'> | Token<'IDENT'> | Token<'DOT'> | Token<'OPEN_BRACKET'> | Token<'CLOSE_BRACKET'> | Token<'PIPE'> | Token<'OPEN_TAG'> | Token<'CLOSE_TAG'>;
export declare const createToken: <T extends TokenType>(type: T, literal: TokenMap[T], start: Loc, end: Loc) => Token<T>;
export declare const createToken: <T extends TokenType>(type: T, literal: TokenMap[T], loc: SourceLocation) => Token<T>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createToken = void 0;
const createToken = (type, literal, start, end) => ({
const createToken = (type, literal, loc) => ({
type,
literal,
start,
end,
loc,
});
exports.createToken = createToken;
{
"name": "@scaffdog/engine",
"version": "1.0.0-canary.0",
"version": "1.0.0-canary.1",
"description": "A module of scaffdog template engine.",

@@ -32,3 +32,3 @@ "keywords": [

"dependencies": {
"@scaffdog/error": "1.0.0-canary.0",
"@scaffdog/error": "1.0.0-canary.1",
"change-case": "^4.1.2",

@@ -41,3 +41,3 @@ "dayjs": "^1.9.8",

"devDependencies": {
"@scaffdog/types": "1.0.0-canary.0",
"@scaffdog/types": "1.0.0-canary.1",
"@types/esprima": "^4.0.2"

@@ -48,3 +48,3 @@ },

},
"gitHead": "87ed8ec940b3cff7c1a5eedb49271e96b56c8978"
"gitHead": "b7221e4a15338cd73ba8c963edf32339b915b255"
}
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