Skip to content

Commit

Permalink
chore: eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
itsamziii committed Nov 12, 2024
1 parent 8aa3f97 commit 356e149
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-cats-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tagparse": patch
---

minor repo changes
6 changes: 3 additions & 3 deletions eslint.config.cjs → eslint.config.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -20,4 +20,4 @@ const config = [
...prettier,
];

module.exports = config;
export default config;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 .",
Expand Down
15 changes: 7 additions & 8 deletions src/lib/Parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,7 +102,7 @@ export class Parser {
}

private async parseTag(): Promise<Node> {
let nameToken, nextToken;
let nameToken; let nextToken;

if (this.strict) {
nameToken = await this.nextToken();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export const enum NodeType {
}

export type ArgumentNode = {
finalValue?: string;
nodes: Node[];
type: NodeType.Argument;
finalValue?: string;
};

export type FunctionNode = {
Expand Down
16 changes: 9 additions & 7 deletions test/functionParser.test.ts
Original file line number Diff line number Diff line change
@@ -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}";
Expand Down

0 comments on commit 356e149

Please sign in to comment.