diff --git a/.changeset/gorgeous-cats-pull.md b/.changeset/gorgeous-cats-pull.md new file mode 100644 index 0000000..070badd --- /dev/null +++ b/.changeset/gorgeous-cats-pull.md @@ -0,0 +1,5 @@ +--- +"tagparse": patch +--- + +minor repo changes diff --git a/eslint.config.cjs b/eslint.config.js similarity index 70% rename from eslint.config.cjs rename to eslint.config.js index 0a598d4..864d920 100644 --- a/eslint.config.cjs +++ b/eslint.config.js @@ -1,10 +1,10 @@ -const { common, node, typescript, prettier } = require("eslint-config-neon"); +import { common, node, typescript, prettier } from "eslint-config-neon"; /** * @type {import('@typescript-eslint/utils').TSESLint.FlatConfig.ConfigArray} */ const config = [ { - ignores: ["dist", "node_modules", "*.js"], + ignores: ["dist", "node_modules", "*.js", "**.test.**"], }, ...common, ...node, @@ -20,4 +20,4 @@ const config = [ ...prettier, ]; -module.exports = config; +export default config; diff --git a/package.json b/package.json index 8d0ee58..5225608 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "private": false, "main": "./dist/index.js", "module": "./dist/index.mjs", - "tyoes": "./dist/index.d.ts", + "types": "./dist/index.d.ts", + "type": "module", "scripts": { "build": "tsup", "lint": "eslint .", diff --git a/src/lib/Parser/Parser.ts b/src/lib/Parser/Parser.ts index 06dc7ff..71a31ac 100644 --- a/src/lib/Parser/Parser.ts +++ b/src/lib/Parser/Parser.ts @@ -25,10 +25,10 @@ export class Parser { /** * Strict mode rules: - * - No empty tags allowed, i.e. "{}" will throw an error - * - No spaces within function tag names or variable tags i.e. "{ pick:}" " "{pi ck:}" "{pick :}" "{test . var}" will all throw errors + * - No empty tags allowed, i.e. "\{\}" will throw an error + * - No spaces within function tag names or variable tags i.e. "\{ pick:\}" " "\{pi ck:\}" "\{pick :\}" "\{test . var\}" will all throw errors * - All tags are expected to end, reaching end of input before the end of tag will throw an error - * - All tags should start with tagStart , i.e. "{" by default, and end with "}" by default. + * - All tags should start with tagStart , i.e. "\{" by default, and end with "\}" by default. * - You are expected to supply atleast one argument to function tags, strict mode prevents you from supplying function tags without any arguments. */ private readonly strict: boolean = false; @@ -102,7 +102,7 @@ export class Parser { } private async parseTag(): Promise { - let nameToken, nextToken; + let nameToken; let nextToken; if (this.strict) { nameToken = await this.nextToken(); @@ -152,14 +152,12 @@ export class Parser { case TokenType.Colon: { const args = await this.parseFunctionArguments(); - if (args.length === 0) { - if (this.strict) { + if (args.length === 0 && this.strict) { throw new StrictModeError( "Expected at least one argument for the tag payload.", ); } // Honestly idk what to do here, so I'm just gonna let the function node have no args - } return this.parseTags ? // We can safely assert that `this.functionParser` is defined if `this.parseTags` is true @@ -213,7 +211,7 @@ export class Parser { while (true) { const token = await this.nextToken(); - let escaped = buffer.length && buffer.at(-1) === "\\"; + const escaped = buffer.length && buffer.at(-1) === "\\"; if (token.type === TokenType.Pipe) { if (escaped) { @@ -227,6 +225,7 @@ export class Parser { buffer = buffer.slice(0, -1) + token.value; continue; } + this.stack.push(token); break; } else if (token.type === TokenType.TagStart) { diff --git a/src/types.ts b/src/types.ts index 48c332c..e3b46ba 100644 --- a/src/types.ts +++ b/src/types.ts @@ -44,9 +44,9 @@ export const enum NodeType { } export type ArgumentNode = { + finalValue?: string; nodes: Node[]; type: NodeType.Argument; - finalValue?: string; }; export type FunctionNode = { diff --git a/test/functionParser.test.ts b/test/functionParser.test.ts index 70e5ec6..ef29189 100644 --- a/test/functionParser.test.ts +++ b/test/functionParser.test.ts @@ -1,16 +1,18 @@ import { describe, it, expect, vi } from "vitest"; -import { ArgumentNode, NodeType, Parser } from "../src/index.js"; +import type { ArgumentNode } from "../src/index.js"; +import { NodeType, Parser } from "../src/index.js"; describe("Parser class with parseTags set to true", () => { const functionParser = vi .fn() - .mockImplementation((name: string, args: ArgumentNode[]) => { - return `Parsed ${name} function with ${args.length} arguments: ${args.map((arg) => arg.finalValue).join(", ")}`; - }); + .mockImplementation( + (name: string, args: ArgumentNode[]) => + `Parsed ${name} function with ${args.length} arguments: ${args.map((arg) => arg.finalValue).join(", ")}`, + ); - const variableParser = vi.fn().mockImplementation((name: string) => { - return `Parsed ${name} variable`; - }); + const variableParser = vi + .fn() + .mockImplementation((name: string) => `Parsed ${name} variable`); it("should parse a variable tag", async () => { const input = "{hello}";