Skip to content

Commit

Permalink
feat: support embellishments and non-punctuation delimiters (#42)
Browse files Browse the repository at this point in the history
* feat: support embellishments and non-brace delimiters

* chore: remove some unused functions

* chore: fix some unformatted codes

* chore: remove unused imports

* chore: comment formatting, remove duplicate functionality

* fix: brace-enclosed embellishment token support

It appears that e{{_}} has the same effect as e{_}. e{{{_}}} triggers an
error. This commit reflects this behavior in `gobbleSingleArgument`
function.

* chore: pollish comments, apply formatting

* fix: order of embellishments shouldn't matter

* feat: support non-punctuation optional tokens

* test: add a few more tests for delimiters

* chore: use type assertions instead of type casting for
gobbleSingleArguments return values

* feat: make 'until' that requires splitting a string work

* chore: run prettier

* chore: add eslint

* test: \xxx^{a_b} in attach-arguments-in-array.test.ts

* chore: remove unused function in gobble-single-argument.ts

* fix: whitespaces between several embellishment arguments should be
ignored

* chore: move .prettierrc.json to package.json and run prettier

* chore: remove no-var rule from eslint

* refactor: make gobbleSingleArgument return Ast.Argument | null

Now gobbleSingleArgument gobbles single argument for each run, and
may mutates its parameter argspec to remove gobbled arguments.

* chore: remove a loop with constant condition

* fix: types in structured-clone
  • Loading branch information
theseanl authored Nov 26, 2023
1 parent c7161a7 commit 8764415
Show file tree
Hide file tree
Showing 59 changed files with 961 additions and 229 deletions.
13 changes: 12 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ module.exports = {
"browser": true,
"es6": true
},
"extends": "eslint:recommended",
"extends": ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
"parser": '@typescript-eslint/parser',
"plugins": ['@typescript-eslint'],
"root": true,
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
Expand All @@ -13,5 +16,13 @@ module.exports = {
"sourceType": "module"
},
"rules": {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/ban-types":"off",
"no-case-declarations": "off",
"prefer-const": "off",
"no-empty": "off",
"curly": "error"
}
};
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ directly to the correct folder during development.

### Testing

Tests in a specific workspace can be run via `npx jest` in that workspace. These for the whole project can be run via `npm run tests` in the
Tests in a specific workspace can be run via `npx jest` in that workspace. These for the whole project can be run via `npm run test` in the
root directory.

### Readme Generation and Consistency
Expand Down
Empty file modified package-lock.json
100644 → 100755
Empty file.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
"package": "npm run package -ws",
"publish": "npm run publish -ws",
"test": "vitest ./packages",
"test:ci": "vitest ./packages --maxWorkers=1"
"test:ci": "vitest ./packages --maxWorkers=1",
"prettier": "prettier '**/*.ts' --write",
"eslint": "eslint '**/*.ts' --ignore-pattern dist"
},
"prettier": {
"tabWidth": 4,
"trailingComma": "es5"
},
"devDependencies": {
"@types/node": "^20.5.9",
Expand Down
13 changes: 10 additions & 3 deletions packages/structured-clone/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @ts-nocheck

// globalThis polyfill from https://mathiasbynens.be/notes/globalthis
(function () {
if (typeof globalThis === "object") return;
if (typeof globalThis === "object") {
return;
}
Object.defineProperty(Object.prototype, "__magic__", {
get: function () {
return this;
Expand All @@ -25,3 +25,10 @@ const clone =
export function structuredClone<T>(obj: T): T {
return clone(obj);
}

declare global {
const __magic__: any;
interface Object {
__magic__?: any;
}
}
2 changes: 1 addition & 1 deletion packages/unified-latex-builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export * from "./libs/builders";
* ## When should I use this?
*
* If you want to programmatically create `Ast.Node` nodes.
*
*
*/
2 changes: 1 addition & 1 deletion packages/unified-latex-cli/libs/unified-args/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const schema: Option[] = [
"Expand the specified macro which is defined in the document. You can use --stats to list all macros defined in the document.",
short: "d",
type: "string",
value: "<name>"
value: "<name>",
},
{
long: "frail",
Expand Down
5 changes: 4 additions & 1 deletion packages/unified-latex-ctan/package/cleveref/provides.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { MacroInfoRecord, EnvInfoRecord } from "@unified-latex/unified-latex-types";
import {
MacroInfoRecord,
EnvInfoRecord,
} from "@unified-latex/unified-latex-types";

export const macros: MacroInfoRecord = {
cref: { signature: "s m" },
Expand Down
5 changes: 4 additions & 1 deletion packages/unified-latex-ctan/package/exam/provides.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { MacroInfoRecord, EnvInfoRecord } from "@unified-latex/unified-latex-types";
import {
MacroInfoRecord,
EnvInfoRecord,
} from "@unified-latex/unified-latex-types";
import { cleanEnumerateBody } from "../../utils/enumerate";

export const macros: MacroInfoRecord = {
Expand Down
5 changes: 4 additions & 1 deletion packages/unified-latex-ctan/package/geometry/provides.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { MacroInfoRecord, EnvInfoRecord } from "@unified-latex/unified-latex-types";
import {
MacroInfoRecord,
EnvInfoRecord,
} from "@unified-latex/unified-latex-types";

export const macros: MacroInfoRecord = {
geometry: {
Expand Down
5 changes: 4 additions & 1 deletion packages/unified-latex-ctan/package/hyperref/provides.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { MacroInfoRecord, EnvInfoRecord } from "@unified-latex/unified-latex-types";
import {
MacroInfoRecord,
EnvInfoRecord,
} from "@unified-latex/unified-latex-types";

export const macros: MacroInfoRecord = {
hypersetup: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const argumentParser: ArgumentParser = (nodes, startPos) => {
const { argument: optionalArg, nodesRemoved: optionalArgNodesRemoved } =
gobbleSingleArgument(nodes, argSpecO, startPos);

let codeArg: Argument | null = null;
let codeArg: Argument | Argument[] | null = null;
let codeArgNodesRemoved: number = 0;
const nextNode = nodes[startPos];
if (match.group(nextNode)) {
Expand Down
5 changes: 4 additions & 1 deletion packages/unified-latex-ctan/package/makeidx/provides.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { MacroInfoRecord, EnvInfoRecord } from "@unified-latex/unified-latex-types";
import {
MacroInfoRecord,
EnvInfoRecord,
} from "@unified-latex/unified-latex-types";

export const macros: MacroInfoRecord = {
see: { signature: "m m" },
Expand Down
5 changes: 4 additions & 1 deletion packages/unified-latex-ctan/package/mathtools/provides.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { MacroInfoRecord, EnvInfoRecord } from "@unified-latex/unified-latex-types";
import {
MacroInfoRecord,
EnvInfoRecord,
} from "@unified-latex/unified-latex-types";

export const macros: MacroInfoRecord = {
mathtoolsset: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const argumentParser: ArgumentParser = (nodes, startPos) => {
const { argument: languageArg, nodesRemoved: languageArgNodesRemoved } =
gobbleSingleArgument(nodes, argSpecM, startPos);

let codeArg: Argument | null = null;
let codeArg: Argument | Argument[] | null = null;
let codeArgNodesRemoved: number = 0;
const nextNode = nodes[startPos];
if (match.group(nextNode)) {
Expand Down
5 changes: 4 additions & 1 deletion packages/unified-latex-ctan/package/nicematrix/provides.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { MacroInfoRecord, EnvInfoRecord } from "@unified-latex/unified-latex-types";
import {
MacroInfoRecord,
EnvInfoRecord,
} from "@unified-latex/unified-latex-types";

export const macros: MacroInfoRecord = {
NiceMatrixOptions: {
Expand Down
5 changes: 4 additions & 1 deletion packages/unified-latex-ctan/package/systeme/provides.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { MacroInfoRecord, EnvInfoRecord } from "@unified-latex/unified-latex-types";
import {
MacroInfoRecord,
EnvInfoRecord,
} from "@unified-latex/unified-latex-types";

export const macros: MacroInfoRecord = {
systeme: {
Expand Down
5 changes: 4 additions & 1 deletion packages/unified-latex-ctan/package/tabularx/provides.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { MacroInfoRecord, EnvInfoRecord } from "@unified-latex/unified-latex-types";
import {
MacroInfoRecord,
EnvInfoRecord,
} from "@unified-latex/unified-latex-types";

export const macros: MacroInfoRecord = {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ArgSpecAst as ArgSpec,
parse as parseArgspec,
} from "@unified-latex/unified-latex-util-argspec";
import { ArgumentParser } from "@unified-latex/unified-latex-types";
import { Argument, ArgumentParser } from "@unified-latex/unified-latex-types";
import { gobbleSingleArgument } from "@unified-latex/unified-latex-util-arguments";
import { match } from "@unified-latex/unified-latex-util-match";
import { scan } from "@unified-latex/unified-latex-util-scan";
Expand Down Expand Up @@ -44,7 +44,10 @@ export const tikzCommandArgumentParser: ArgumentParser = (nodes, startPos) => {
const {
argument: _optionalArgument,
nodesRemoved: optionalArgumentNodesRemoved,
} = gobbleSingleArgument(nodes, OPTIONAL_ARGUMENT_ARG_SPEC, pos);
} = gobbleSingleArgument(nodes, OPTIONAL_ARGUMENT_ARG_SPEC, pos) as {
argument: Argument;
nodesRemoved: number;
};
nodesRemoved += optionalArgumentNodesRemoved;
const optionalArg = _optionalArgument || blankArg();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ export function colorToTextcolorMacro(
args,
_renderInfo: { inParMode: true },
};
}
}
5 changes: 4 additions & 1 deletion packages/unified-latex-ctan/package/xcolor/provides.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { MacroInfoRecord, EnvInfoRecord } from "@unified-latex/unified-latex-types";
import {
MacroInfoRecord,
EnvInfoRecord,
} from "@unified-latex/unified-latex-types";

export const macros: MacroInfoRecord = {
substitutecolormodel: {
Expand Down
5 changes: 4 additions & 1 deletion packages/unified-latex-ctan/package/xparse/provides.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { MacroInfoRecord, EnvInfoRecord } from "@unified-latex/unified-latex-types";
import {
MacroInfoRecord,
EnvInfoRecord,
} from "@unified-latex/unified-latex-types";

export const macros: MacroInfoRecord = {
NewDocumentCommand: {
Expand Down
37 changes: 34 additions & 3 deletions packages/unified-latex-ctan/tests/beamer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,46 @@ describe("unified-latex-ctan:beamer", () => {
const EXAMPLES: [string, Ast.Node[]][] = [
[
"\\onslide+<foo>",
[m("onslide", args([arg("+", {openMark:"", closeMark:""}), null, "foo", null], { braces: "[][]<>{}" }))],
[
m(
"onslide",
args(
[
arg("+", { openMark: "", closeMark: "" }),
null,
"foo",
null,
],
{ braces: "[][]<>{}" }
)
),
],
],
[
"\\onslide+<foo>{bar}",
[m("onslide", args([arg("+", {openMark:"", closeMark:""}), null, "foo", "bar"], { braces: "[][]<>{}" }))],
[
m(
"onslide",
args(
[
arg("+", { openMark: "", closeMark: "" }),
null,
"foo",
"bar",
],
{ braces: "[][]<>{}" }
)
),
],
],
[
"\\onslide{bar}",
[m("onslide", args([null, null, null, "bar"], { braces: "[][]<>{}" }))],
[
m(
"onslide",
args([null, null, null, "bar"], { braces: "[][]<>{}" })
),
],
],
];

Expand Down
5 changes: 4 additions & 1 deletion packages/unified-latex-ctan/tests/tabular.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import util from "util";
import { parse } from "../../unified-latex-util-parse";
import { parseTabularSpec, printRaw as tabularPrintRaw } from "../package/tabularx";
import {
parseTabularSpec,
printRaw as tabularPrintRaw,
} from "../package/tabularx";

/* eslint-env jest */

Expand Down
5 changes: 4 additions & 1 deletion packages/unified-latex-ctan/tests/tikz.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ describe("unified-latex-ctan:tikz", () => {
["\\tikz foo baz; bar", "\\tikz{foo baz;} bar"],
["\\tikz [xxx] {foo} bar", "\\tikz[xxx]{foo} bar"],
["\\tikz [xxx] foo baz; bar", "\\tikz[xxx]{foo baz;} bar"],
["\\tikz :fill = {aaa} :rotate = {bbb} [xxx] foo baz; bar", "\\tikz :fill = {aaa} :rotate = {bbb} [xxx]{foo baz;} bar"],
[
"\\tikz :fill = {aaa} :rotate = {bbb} [xxx] foo baz; bar",
"\\tikz :fill = {aaa} :rotate = {bbb} [xxx]{foo baz;} bar",
],
];

for (const [inStr, outStr] of EXAMPLES) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { pointStart, pointEnd } from "unist-util-position";
import { lintRule } from "unified-lint-rule";
import * as Ast from "@unified-latex/unified-latex-types";
import { match} from "@unified-latex/unified-latex-util-match";
import { match } from "@unified-latex/unified-latex-util-match";
import { prefixMatch, Trie } from "@unified-latex/unified-latex-util-scan";
import { visit } from "@unified-latex/unified-latex-util-visit";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,25 @@ CTAN l2tabuen Section 2.`;
export const unifiedLatexLintNoTexFontShapingCommands = lintRule<
Ast.Root,
PluginOptions
>({ origin: "unified-latex-lint:no-tex-font-shaping-commands" }, (tree, file, options) => {
visit(
tree,
(node, info) => {
const macroName = node.content;
file.message(
`Replace "${printRaw(node)}" with "${printRaw(
m(REPLACEMENTS[macroName])
)}"`,
node
);
>(
{ origin: "unified-latex-lint:no-tex-font-shaping-commands" },
(tree, file, options) => {
visit(
tree,
(node, info) => {
const macroName = node.content;
file.message(
`Replace "${printRaw(node)}" with "${printRaw(
m(REPLACEMENTS[macroName])
)}"`,
node
);

if (options?.fix) {
replaceNodeDuringVisit(m(REPLACEMENTS[macroName]), info);
}
},
{ test: isReplaceable }
);
});
if (options?.fix) {
replaceNodeDuringVisit(m(REPLACEMENTS[macroName]), info);
}
},
{ test: isReplaceable }
);
}
);
2 changes: 1 addition & 1 deletion packages/unified-latex-prettier/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export * from "./libs/prettier-plugin-latex";
* ## When should I use this?
*
* If you want to construct a `Prettier` instance that has LaTeX parsing abilities.
*
*
* You should probably use the `prettier-plugin-latex` package instead of directly accessing this package.
*/
6 changes: 2 additions & 4 deletions packages/unified-latex-prettier/libs/printer/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ function isLineType(elm: Doc): boolean {
return isLineType(elm[0]);
}
// Perhaps we can sneak by with Prettier v2 compatibility?
// @ts-ignore
if (elm.type === "concat") {
// @ts-ignore
return isLineType(elm.parts);
if ((elm.type as any) === "concat") {
return isLineType((elm as any).parts);
}
return elm.type === "line";
}
Expand Down
4 changes: 1 addition & 3 deletions packages/unified-latex-prettier/libs/printer/tikz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import {
softline,
fill,
} from "./common";
import {
printRaw,
} from "@unified-latex/unified-latex-util-print-raw";
import { printRaw } from "@unified-latex/unified-latex-util-print-raw";
import { match } from "@unified-latex/unified-latex-util-match";
import { trim } from "@unified-latex/unified-latex-util-trim";
import {
Expand Down
1 change: 0 additions & 1 deletion packages/unified-latex-prettier/tests/formatting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,5 +719,4 @@ c
await expect(inStr).toFormatAs(outStr, formatter);
}
});

});
Loading

0 comments on commit 8764415

Please sign in to comment.