From ae5dd79debee489d4e73db752a0a68bd12822304 Mon Sep 17 00:00:00 2001 From: thetutlage Date: Fri, 28 Feb 2020 08:27:55 +0530 Subject: [PATCH] feat: add property to context --- examples/views/alert.edge | 1 + examples/views/partial.edge | 3 ++- examples/views/user.edge | 15 +++++++++++++-- examples/views/user.presenter.js | 2 +- src/Compiler/index.ts | 8 +++----- src/Edge/globals/index.ts | 17 +++++++++++++++-- src/Tags/Component.ts | 2 +- 7 files changed, 36 insertions(+), 12 deletions(-) diff --git a/examples/views/alert.edge b/examples/views/alert.edge index 175aa1c..169604f 100644 --- a/examples/views/alert.edge +++ b/examples/views/alert.edge @@ -1,3 +1,4 @@ +{{ inspect($state, 1) }} @if($slots.title) {{{ $slots.title({ title }) }}} @else diff --git a/examples/views/partial.edge b/examples/views/partial.edge index b504114..462e5e2 100644 --- a/examples/views/partial.edge +++ b/examples/views/partial.edge @@ -1 +1,2 @@ -

Inside slot: {{ slotTitle() }}

\ No newline at end of file +{{ inspect($state, 1) }} +

Inside slot: {{ slotTitle() }}

diff --git a/examples/views/user.edge b/examples/views/user.edge index 059c82b..3a59040 100644 --- a/examples/views/user.edge +++ b/examples/views/user.edge @@ -6,12 +6,23 @@ - @set('user', { username: 'virk' }) - {{ inspect($state, 1) }} + + @component('alert', { title: 'hello world' }) + @slot('title', props) + @include('partial') + @endslot + @endcomponent diff --git a/examples/views/user.presenter.js b/examples/views/user.presenter.js index dd9dada..1745123 100644 --- a/examples/views/user.presenter.js +++ b/examples/views/user.presenter.js @@ -6,4 +6,4 @@ module.exports = class User { slotTitle (ctx) { return ctx.resolve('props').title.toUpperCase() } -} \ No newline at end of file +} diff --git a/src/Compiler/index.ts b/src/Compiler/index.ts index f45117a..2a71843 100644 --- a/src/Compiler/index.ts +++ b/src/Compiler/index.ts @@ -230,12 +230,9 @@ export class Compiler implements CompilerContract { const wrapAsFunction = !inline /** - * Get a new instance of the parser. We use the `templatePath` as the filename - * instead of the `absPath`, since `templatePath` is relative and readable. + * Get a new instance of the parser. */ - const parser = new Parser(this._tags, { - filename: `${absPath.replace(/\.edge$/, '')}.edge`, - }) + const parser = new Parser(this._tags, { filename: absPath }) /** * Resolve the template and Presenter using the given loader @@ -252,6 +249,7 @@ export class Compiler implements CompilerContract { * Finally process the ast */ const buffer = new EdgeBuffer() + buffer.writeStatement(`ctx.set('$filename', '${templatePath.replace(/\.edge$/, '')}.edge')`) templateTokens.forEach((token) => parser.processLexerToken(token, buffer)) const payload = { diff --git a/src/Edge/globals/index.ts b/src/Edge/globals/index.ts index a4f7b45..3c0e6a8 100644 --- a/src/Edge/globals/index.ts +++ b/src/Edge/globals/index.ts @@ -18,11 +18,24 @@ import { EdgeContract, ContextContract } from '../../Contracts' * Inspect value. */ function inspect (ctx: ContextContract, valueToInspect: any, depth: number = 1) { - return ctx.safe(`
${utilInspect(valueToInspect, {
+  const inspectedString = `
${utilInspect(valueToInspect, {
     showHidden: true,
     compact: false,
     depth: depth,
-  })}
`) + })}
` + + const filename = ` + ${ctx.resolve('$filename')} + ` + + return ctx.safe(`
${inspectedString}${filename}
`) +} + +/** + * Compacting the inspect output of self + */ +inspect[Symbol.for('nodejs.util.inspect.custom')] = function customInspect () { + return '[inspect]' } export default function globals (edge: EdgeContract) { diff --git a/src/Tags/Component.ts b/src/Tags/Component.ts index 9b2ef59..8dc7248 100644 --- a/src/Tags/Component.ts +++ b/src/Tags/Component.ts @@ -35,7 +35,7 @@ const componentNameAllowedExpressions: (keyof typeof expressions)[] = [ * Returns the component name and props by parsing the component jsArg expression */ function getComponentNameAndProps (expression: any, parser: Parser): [string, string] { - let name + let name: string /** * Use the first expression inside the sequence expression as the name