Skip to content

Commit

Permalink
refactor: re-arrange source code files
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 8, 2023
1 parent 55dbbc1 commit 0df85e6
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 103 deletions.
2 changes: 1 addition & 1 deletion src/edge/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Loader } from '../loader.js'
import { Compiler } from '../compiler.js'
import { Template } from '../template.js'
import { Processor } from '../processor.js'
import { EdgeRenderer } from '../renderer.js'
import { EdgeRenderer } from './renderer.js'
import type { TagContract, EdgeOptions, LoaderTemplate, LoaderContract } from '../types.js'

/**
Expand Down
6 changes: 3 additions & 3 deletions src/renderer.ts → src/edge/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

import lodash from '@poppinss/utils/lodash'

import { Template } from './template.js'
import { Processor } from './processor.js'
import type { Compiler } from './compiler.js'
import { Template } from '../template.js'
import { Processor } from '../processor.js'
import type { Compiler } from '../compiler.js'

/**
* Renders a given template with it's shared state
Expand Down
95 changes: 0 additions & 95 deletions src/stringified_object.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/tags/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import type { TagToken } from 'edge-lexer/types'
import { EdgeBuffer, expressions, Parser } from 'edge-parser'

import type { TagContract } from '../types.js'
import { StringifiedObject } from '../stringified_object.js'
import { isSubsetOf, unallowedExpression, parseJsArg } from '../utils.js'
import { isSubsetOf, unallowedExpression, parseJsArg, StringifiedObject } from '../utils.js'

/**
* A list of allowed expressions for the component name
Expand Down
2 changes: 1 addition & 1 deletion src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { CompiledTemplate } from './types.js'
* method ensures that underlying value is never
* escaped.
*/
export class SafeValue {
class SafeValue {
constructor(public value: any) {}
}

Expand Down
85 changes: 85 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,88 @@ export async function asyncEach(
}
}
}

/**
* This class generates a valid object as a string, which is written to the template
* output. The reason we need a string like object, since we don't want it's
* properties to be evaluated during the object creation, instead it must
* be evaluated when the compiled output is invoked.
*/
export class StringifiedObject {
#obj: string = ''

addSpread(key: string) {
this.#obj += this.#obj.length ? `, ${key}` : `${key}`
}

/**
* Add key/value pair to the object.
*
* ```js
* stringifiedObject.add('username', `'virk'`)
* ```
*/
add(key: any, value: any, isComputed: boolean = false) {
key = isComputed ? `[${key}]` : key
this.#obj += this.#obj.length ? `, ${key}: ${value}` : `${key}: ${value}`
}

/**
* Returns the object alike string back.
*
* ```js
* stringifiedObject.flush()
*
* // returns
* `{ username: 'virk' }`
* ```
*/
flush(): string {
const obj = `{ ${this.#obj} }`
this.#obj = ''
return obj
}

/**
* Parses an array of expressions to form an object. Each expression inside the array must
* be `ObjectExpression` or an `AssignmentExpression`, otherwise it will be ignored.
*
* ```js
* (title = 'hello')
* // returns { title: 'hello' }
*
* ({ title: 'hello' })
* // returns { title: 'hello' }
*
* ({ title: 'hello' }, username = 'virk')
* // returns { title: 'hello', username: 'virk' }
* ```
*/
static fromAcornExpressions(expressions: any[], parser: Parser): string {
if (!Array.isArray(expressions)) {
throw new Error('"fromAcornExpressions" expects an array of acorn ast expressions')
}

const objectifyString = new this()

expressions.forEach((arg) => {
if (arg.type === 'ObjectExpression') {
arg.properties.forEach((prop: any) => {
if (prop.type === 'SpreadElement') {
objectifyString.addSpread(parser.utils.stringify(prop))
} else {
const key = parser.utils.stringify(prop.key)
const value = parser.utils.stringify(prop.value)
objectifyString.add(key, value, prop.computed)
}
})
}

if (arg.type === 'AssignmentExpression') {
objectifyString.add(arg.left.name, parser.utils.stringify(arg.right))
}
})

return objectifyString.flush()
}
}
2 changes: 1 addition & 1 deletion tests/stringified_object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { test } from '@japa/runner'
import { Parser, Stack } from 'edge-parser'
import { StringifiedObject } from '../src/stringified_object.js'
import { StringifiedObject } from '../src/utils.js'

/**
* Sample loc
Expand Down

0 comments on commit 0df85e6

Please sign in to comment.