From bebf6c66382fe33faa98c11621da89f07a704f78 Mon Sep 17 00:00:00 2001 From: Jason Siefken Date: Mon, 19 Feb 2024 17:04:42 -0500 Subject: [PATCH] Store default arguments in `_renderInfo` for each arg --- examples/tsconfig.json | 2 +- package.json | 2 +- packages/unified-latex-types/index.ts | 3 +++ .../unified-latex-types/libs/ast-types.ts | 5 +++-- packages/unified-latex-types/package.json | 6 ++++-- .../libs/gobble-arguments.ts | 21 +++++++++++++++---- .../tests/get-args-content.test.ts | 1 + .../unifiex-latex-attach-arguments.test.ts | 1 + .../unified-latex-util-render-info/index.ts | 2 +- tsconfig.test.json | 9 ++++++-- 10 files changed, 39 insertions(+), 13 deletions(-) diff --git a/examples/tsconfig.json b/examples/tsconfig.json index 78d9d703..192a09bb 100644 --- a/examples/tsconfig.json +++ b/examples/tsconfig.json @@ -1,5 +1,5 @@ { "compilerOptions": { "rootDir": "./" }, "include": ["./**/*.ts"], - "extends": "../tsconfig.build.json", + "extends": "../tsconfig.build.json" } diff --git a/package.json b/package.json index cd0874bd..ad70b754 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "test:packages-esm": "cd test/esm && npm install && npm run test", "test:packages-cjs": "cd test/cjs && npm install && npm run test", "test:packages-install": "cd test && npx vite-node make-packages.ts", - "prettier": "prettier \"**/*.ts\" \"**/*.json\" --write", + "prettier": "prettier \"**/*.ts\" \"**/*.json\" --ignore-path .gitignore --write", "eslint": "eslint \"**/*.ts\" --ignore-pattern dist" }, "prettier": { diff --git a/packages/unified-latex-types/index.ts b/packages/unified-latex-types/index.ts index e3985eec..381f8745 100644 --- a/packages/unified-latex-types/index.ts +++ b/packages/unified-latex-types/index.ts @@ -2,6 +2,9 @@ export * from "./libs/ast-types"; export * from "./libs/type-guard"; export * from "./libs/info-specs"; +// Export something for importing packages +export default {}; + // NOTE: The docstring comment must be the last item in the index.ts file! /** * ## What is this? diff --git a/packages/unified-latex-types/libs/ast-types.ts b/packages/unified-latex-types/libs/ast-types.ts index a9757048..a7a34070 100644 --- a/packages/unified-latex-types/libs/ast-types.ts +++ b/packages/unified-latex-types/libs/ast-types.ts @@ -11,8 +11,9 @@ export interface GenericNode { // Abstract nodes interface BaseNode { type: string; - _renderInfo?: (MacroInfo["renderInfo"] | EnvInfo["renderInfo"]) & - Record; + _renderInfo?: (MacroInfo["renderInfo"] | EnvInfo["renderInfo"]) & { + defaultArg?: string; + } & Record; position?: { start: { offset: number; line: number; column: number }; end: { offset: number; line: number; column: number }; diff --git a/packages/unified-latex-types/package.json b/packages/unified-latex-types/package.json index a5999c86..014db447 100644 --- a/packages/unified-latex-types/package.json +++ b/packages/unified-latex-types/package.json @@ -13,12 +13,14 @@ "exports": { ".": { "prebuilt": "./dist/index.js", - "import": "./index.ts" + "import": "./index.ts", + "require": "./dist/index.cjs" }, "./*js": "./dist/*js", "./*": { "prebuilt": "./dist/*.js", - "import": "./*.ts" + "import": "./*.ts", + "require": "./dist/*.cjs" } }, "scripts": { diff --git a/packages/unified-latex-util-arguments/libs/gobble-arguments.ts b/packages/unified-latex-util-arguments/libs/gobble-arguments.ts index a68a2655..94664ec2 100644 --- a/packages/unified-latex-util-arguments/libs/gobble-arguments.ts +++ b/packages/unified-latex-util-arguments/libs/gobble-arguments.ts @@ -6,6 +6,7 @@ import { parse as parseArgspec, } from "@unified-latex/unified-latex-util-argspec"; import { gobbleSingleArgument } from "./gobble-single-argument"; +import { updateRenderInfo } from "@unified-latex/unified-latex-util-render-info"; /** * Gobbles an argument of whose type is specified @@ -38,7 +39,12 @@ export function gobbleArguments( // we need to keep gobbling arguments until we've got them all. const remainingTokens = new Set(spec.embellishmentTokens); const argForToken = Object.fromEntries( - spec.embellishmentTokens.map((t) => [t, emptyArg()]) + spec.embellishmentTokens.map((t, i) => { + // For empty arguments, we also store their default. + const defaultArg = + "defaultArg" in spec ? spec.defaultArg?.[i] : undefined; + return [t, emptyArg(defaultArg)]; + }) ); let { argument, nodesRemoved: removed } = gobbleSingleArgument( @@ -66,7 +72,10 @@ export function gobbleArguments( spec, startPos ); - args.push(argument || emptyArg()); + // For empty arguments, we also store their default. + const defaultArg = + "defaultArg" in spec ? spec.defaultArg : undefined; + args.push(argument || emptyArg(defaultArg)); nodesRemoved += removed; } } @@ -87,6 +96,10 @@ function embellishmentSpec(tokens: Set): ArgSpec.Embellishment { /** * Create an empty argument. */ -function emptyArg(): Ast.Argument { - return arg([], { openMark: "", closeMark: "" }); +function emptyArg(defaultArg?: string): Ast.Argument { + const ret = arg([], { openMark: "", closeMark: "" }); + if (defaultArg != null) { + updateRenderInfo(ret, { defaultArg }); + } + return ret; } diff --git a/packages/unified-latex-util-arguments/tests/get-args-content.test.ts b/packages/unified-latex-util-arguments/tests/get-args-content.test.ts index 2a6de7d8..d06cda3a 100644 --- a/packages/unified-latex-util-arguments/tests/get-args-content.test.ts +++ b/packages/unified-latex-util-arguments/tests/get-args-content.test.ts @@ -1,3 +1,4 @@ +import { describe, it, expect } from "vitest"; import util from "util"; import * as Ast from "@unified-latex/unified-latex-types"; import { attachMacroArgs } from "../libs/attach-arguments"; diff --git a/packages/unified-latex-util-arguments/tests/unifiex-latex-attach-arguments.test.ts b/packages/unified-latex-util-arguments/tests/unifiex-latex-attach-arguments.test.ts index 64bf5e6c..4bf11989 100644 --- a/packages/unified-latex-util-arguments/tests/unifiex-latex-attach-arguments.test.ts +++ b/packages/unified-latex-util-arguments/tests/unifiex-latex-attach-arguments.test.ts @@ -1,3 +1,4 @@ +import { describe, it, expect } from "vitest"; import { unified } from "unified"; import { VFile } from "unified-lint-rule/lib"; import util from "util"; diff --git a/packages/unified-latex-util-render-info/index.ts b/packages/unified-latex-util-render-info/index.ts index 97737676..93e324f9 100644 --- a/packages/unified-latex-util-render-info/index.ts +++ b/packages/unified-latex-util-render-info/index.ts @@ -9,7 +9,7 @@ import { visit } from "@unified-latex/unified-latex-util-visit"; * *This operation mutates `node`* */ export function updateRenderInfo( - node: Ast.Node, + node: Ast.Node | Ast.Argument, renderInfo: object | null | undefined ) { if (renderInfo != null) { diff --git a/tsconfig.test.json b/tsconfig.test.json index c0b42a32..69bc32e1 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -1,6 +1,11 @@ { "extends": "./tsconfig.build.json", - "include": ["**/*.ts"], + "include": [ + "**/*.ts", + // I have no idea why this needs to be included specifically + // and none of the other packages do. + "packages/unified-latex-types/dist/index.js" + ], "exclude": [ "**/*.d.ts", "node_modules", @@ -12,7 +17,7 @@ "compilerOptions": { "rootDir": "./packages", "paths": { - "@unified-latex/*": ["./packages/*/dist"] + "@unified-latex/*": ["./packages/*"] }, "types": ["vitest/globals"] }