From 0df85e69a3808d93a386cf70e88cc7b2918fb4cf Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Tue, 8 Aug 2023 10:32:55 +0530 Subject: [PATCH] refactor: re-arrange source code files --- src/edge/main.ts | 2 +- src/{ => edge}/renderer.ts | 6 +- src/stringified_object.ts | 95 -------------------------------- src/tags/component.ts | 3 +- src/template.ts | 2 +- src/utils.ts | 85 ++++++++++++++++++++++++++++ tests/stringified_object.spec.ts | 2 +- 7 files changed, 92 insertions(+), 103 deletions(-) rename src/{ => edge}/renderer.ts (94%) delete mode 100644 src/stringified_object.ts diff --git a/src/edge/main.ts b/src/edge/main.ts index d4f442e..c99a249 100644 --- a/src/edge/main.ts +++ b/src/edge/main.ts @@ -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' /** diff --git a/src/renderer.ts b/src/edge/renderer.ts similarity index 94% rename from src/renderer.ts rename to src/edge/renderer.ts index 91f4ca7..d5e5294 100644 --- a/src/renderer.ts +++ b/src/edge/renderer.ts @@ -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 diff --git a/src/stringified_object.ts b/src/stringified_object.ts deleted file mode 100644 index c31cc80..0000000 --- a/src/stringified_object.ts +++ /dev/null @@ -1,95 +0,0 @@ -/* - * edge.js - * - * (c) EdgeJS - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -import { Parser } from 'edge-parser' - -/** - * 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() - } -} diff --git a/src/tags/component.ts b/src/tags/component.ts index b754a5b..29c82a3 100644 --- a/src/tags/component.ts +++ b/src/tags/component.ts @@ -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 diff --git a/src/template.ts b/src/template.ts index b0315e8..fee15cd 100644 --- a/src/template.ts +++ b/src/template.ts @@ -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) {} } diff --git a/src/utils.ts b/src/utils.ts index 5ef4368..2eb25da 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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() + } +} diff --git a/tests/stringified_object.spec.ts b/tests/stringified_object.spec.ts index d38dcd3..b9c9e5c 100644 --- a/tests/stringified_object.spec.ts +++ b/tests/stringified_object.spec.ts @@ -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