Skip to content

Commit

Permalink
refactor(errors): use edge-error for consistent error stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 12, 2018
1 parent 418a918 commit 4894f4a
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"fs-extra": "^6.0.1",
"japa": "^1.0.6",
"japa-cli": "^1.0.1",
"mrm": "^1.2.0",
"mrm": "^1.2.1",
"nyc": "^12.0.2",
"pkg-ok": "^2.2.0",
"ts-node": "^7.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class Compiler implements ICompiler {

const { template, Presenter } = this.loader.resolve(templatePath, !inline)
const payload = {
template: new Parser(this.tags).parseTemplate(template, !inline),
template: new Parser(this.tags, { filename: templatePath }) .parseTemplate(template, !inline),
Presenter,
}

Expand Down
8 changes: 4 additions & 4 deletions src/Tags/Each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export class EachTag {
/**
* Returns the value and key names for the foreach loop
*/
private static _getLoopKeyValue (expression: any): [string, string] {
allowExpressions('each', expression, ['SequenceExpression', 'Identifier'])
private static _getLoopKeyValue (expression: any, filename: string): [string, string] {
allowExpressions('each', expression, ['SequenceExpression', 'Identifier'], filename)

if (expression.type === 'SequenceExpression') {
return [expression.expressions[0].name, expression.expressions[1].name]
Expand All @@ -48,9 +48,9 @@ export class EachTag {
const ast = parser.generateAst(token.properties.jsArg, token.lineno)
const expression = ast.body[0].expression

allowExpressions('each', expression, this.allowedExpressions)
allowExpressions('each', expression, this.allowedExpressions, parser.options.filename)

const [value, key] = this._getLoopKeyValue(expression.left)
const [value, key] = this._getLoopKeyValue(expression.left, parser.options.filename)
const rhs = this._getLoopSource(expression.right, parser)

const elseIndex = token
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/ElseIf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class ElseIfTag {
*/
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
const parsed = parser.parseJsArg(token.properties.jsArg, token.lineno)
disAllowExpressions('elseif', parsed, this.bannedExpressions)
disAllowExpressions('elseif', parsed, this.bannedExpressions, parser.options.filename)

/**
* Dedent block
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/If.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class IfTag {
*/
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
const parsed = parser.parseJsArg(token.properties.jsArg, token.lineno)
disAllowExpressions('if', parsed, this.bannedExpressions)
disAllowExpressions('if', parsed, this.bannedExpressions, parser.options.filename)

/**
* Start if block
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class IncludeTag {
*/
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
const parsed = parser.parseJsArg(token.properties.jsArg, token.lineno)
disAllowExpressions('include', parsed, this.bannedExpressions)
disAllowExpressions('include', parsed, this.bannedExpressions, parser.options.filename)

/**
* Include template. Since the partials can be a runtime value, we cannot inline
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Unless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class UnlessTag {
*/
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
const parsed = parser.parseJsArg(token.properties.jsArg, token.lineno)
disAllowExpressions('unless', parsed, this.bannedExpressions)
disAllowExpressions('unless', parsed, this.bannedExpressions, parser.options.filename)

/**
* Start if block
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Yield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class YieldTag {
*/
public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) {
const parsed = parser.parseJsArg(token.properties.jsArg, token.lineno)
allowExpressions('yield', parsed, this.allowedExpressions)
allowExpressions('yield', parsed, this.allowedExpressions, parser.options.filename)

/**
* Start if block
Expand Down
30 changes: 21 additions & 9 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* file that was distributed with this source code.
*/

import { UnAllowedExpressionException, TooManyArgumentsException } from '../Exceptions'
import { Parser } from 'edge-parser'
import { sep } from 'path'
import { EdgeError } from 'edge-error'

export class ObjectifyString {
private obj: string = ''
Expand All @@ -36,19 +36,27 @@ export class ObjectifyString {
* Validates the expression type to be part of the allowed
* expressions only
*/
export function allowExpressions (tag: string, expression: any, expressions: string[]) {
export function allowExpressions (tag: string, expression: any, expressions: string[], filename: string) {
if (expressions.indexOf(expression.type) === -1) {
throw UnAllowedExpressionException.invoke(tag, expression.type, expression.loc.start.line)
throw new EdgeError(`${expression.type} is not allowed for ${tag} tag.`, 'E_UNALLOWED_EXPRESSION', {
line: expression.loc.start.line,
col: expression.loc.start.column,
filename: filename,
})
}
}

/**
* Validates the expression type to not be part of the black
* listed expressions.
*/
export function disAllowExpressions (tag: string, expression: any, expressions: string[]) {
export function disAllowExpressions (tag: string, expression: any, expressions: string[], filename) {
if (expressions.indexOf(expression.type) > -1) {
throw UnAllowedExpressionException.invoke(tag, expression.type, expression.loc.start.line)
throw new EdgeError(`${expression.type} is not allowed for ${tag} tag.`, 'E_UNALLOWED_EXPRESSION', {
line: expression.loc.start.line,
col: expression.loc.start.column,
filename: filename,
})
}
}

Expand Down Expand Up @@ -117,7 +125,7 @@ export function parseSequenceExpression (expression: any, parser: Parser): [stri
* ```
*/
export function parseAsKeyValuePair (expression: any, parser: Parser, valueExpressions: string[]): [string, null | string] {
allowExpressions('slot', expression, ['Literal', 'SequenceExpression'])
allowExpressions('slot', expression, ['Literal', 'SequenceExpression'], parser.options.filename)

/**
* Return without counting props, value is a literal
Expand All @@ -131,13 +139,17 @@ export function parseAsKeyValuePair (expression: any, parser: Parser, valueExpre
* expression
*/
if (expression.expressions.length > 2) {
throw TooManyArgumentsException.invoke('slot', 2, expression.loc.start.line)
throw new EdgeError('Maximum of 2 arguments are allowed for slot tag', 'E_MAX_ARGUMENTS', {
line: expression.loc.start.line,
col: expression.loc.start.column,
filename: parser.options.filename,
})
}

allowExpressions('slot', expression.expressions[0], ['Literal'])
allowExpressions('slot', expression.expressions[0], ['Literal'], parser.options.filename)

if (valueExpressions.length) {
allowExpressions('slot', expression.expressions[1], valueExpressions)
allowExpressions('slot', expression.expressions[1], valueExpressions, parser.options.filename)
}

/**
Expand Down
27 changes: 14 additions & 13 deletions test/tags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test.group('If tag', (group) => {
})

test('raise errors on correct line with if tag', async (assert) => {
assert.plan(1)
assert.plan(2)

const templateContent = `Hello everyone!
We are writing a bad if condition
Expand All @@ -37,6 +37,7 @@ We are writing a bad if condition
try {
compiler.compile('foo', true)
} catch (error) {
assert.equal(error.message, 'Unexpected token ')
assert.equal(error.line, 4)
}
})
Expand All @@ -55,7 +56,7 @@ We are writing a bad if condition
compiler.compile('foo', true)
} catch (error) {
assert.equal(error.line, 4)
assert.equal(error.message, 'E_UNALLOWED_EXPRESSION: SequenceExpression is not allowed for if tag\n> More details: https://err.sh/poppinss/edge-errors/E_UNALLOWED_EXPRESSION')
assert.equal(error.message, 'SequenceExpression is not allowed for if tag.')
}
})

Expand Down Expand Up @@ -91,7 +92,7 @@ test.group('Include', (group) => {
try {
compiler.compile('foo', true)
} catch (error) {
assert.equal(error.line, 2)
assert.equal(error.stack.split('\n')[1], ` at (${join(viewsDir, 'foo.edge')}:2:4)`)
}
})

Expand All @@ -104,8 +105,8 @@ test.group('Include', (group) => {
try {
compiler.compile('foo', true)
} catch (error) {
assert.equal(error.line, 1)
assert.equal(error.message, 'E_UNALLOWED_EXPRESSION: SequenceExpression is not allowed for include tag\n> More details: https://err.sh/poppinss/edge-errors/E_UNALLOWED_EXPRESSION')
assert.equal(error.stack.split('\n')[1], ` at (${join(viewsDir, 'foo.edge')}:1:0)`)
assert.equal(error.message, 'SequenceExpression is not allowed for include tag.')
}
})
})
Expand All @@ -129,8 +130,8 @@ test.group('Component', (group) => {
try {
compiler.compile('foo', true)
} catch (error) {
assert.equal(error.line, 3)
assert.equal(error.message, 'E_UNALLOWED_EXPRESSION: Identifier is not allowed for slot tag\n> More details: https://err.sh/poppinss/edge-errors/E_UNALLOWED_EXPRESSION')
assert.equal(error.stack.split('\n')[1], ` at (${join(viewsDir, 'foo.edge')}:3:0)`)
assert.equal(error.message, 'Identifier is not allowed for slot tag.')
}
})

Expand All @@ -148,8 +149,8 @@ test.group('Component', (group) => {
try {
compiler.compile('foo', true)
} catch (error) {
assert.equal(error.line, 3)
assert.equal(error.message, 'E_MAX_ARGUMENTS: Maximum of 2 arguments are allowed for slot tag\n> More details: https://err.sh/poppinss/edge-errors/E_MAX_ARGUMENTS')
assert.equal(error.stack.split('\n')[1], ` at (${join(viewsDir, 'foo.edge')}:3:0)`)
assert.equal(error.message, 'Maximum of 2 arguments are allowed for slot tag')
}
})

Expand All @@ -167,8 +168,8 @@ test.group('Component', (group) => {
try {
compiler.compile('foo', true)
} catch (error) {
assert.equal(error.line, 3)
assert.equal(error.message, 'E_UNALLOWED_EXPRESSION: Identifier is not allowed for slot tag\n> More details: https://err.sh/poppinss/edge-errors/E_UNALLOWED_EXPRESSION')
assert.equal(error.stack.split('\n')[1], ` at (${join(viewsDir, 'foo.edge')}:3:0)`)
assert.equal(error.message, 'Identifier is not allowed for slot tag.')
}
})

Expand All @@ -189,8 +190,8 @@ test.group('Component', (group) => {
try {
compiler.compile('foo', true)
} catch (error) {
assert.equal(error.line, 3)
assert.equal(error.message, 'E_UNALLOWED_EXPRESSION: Literal is not allowed for slot tag\n> More details: https://err.sh/poppinss/edge-errors/E_UNALLOWED_EXPRESSION')
assert.equal(error.stack.split('\n')[1], ` at (${join(viewsDir, 'foo.edge')}:5:6)`)
assert.equal(error.message, 'Literal is not allowed for slot tag.')
}
})
})

0 comments on commit 4894f4a

Please sign in to comment.