From dc665b01d47ccbd5de5cd22ee39ac9e162ec63bc Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Sun, 29 Jan 2023 18:24:50 -0800 Subject: [PATCH 1/3] slightly more precise types for format nodes --- src/node-types.ts | 8 ++++---- src/parser.ts | 11 +++++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/node-types.ts b/src/node-types.ts index cf2000a..17bbed4 100644 --- a/src/node-types.ts +++ b/src/node-types.ts @@ -137,25 +137,25 @@ export type TextNode = { export type StarNode = { name: 'star'; - contents: FragmentNode[]; + contents: (TextNode | CommentNode | TagNode)[]; location: LocationRange; }; export type UnderscoreNode = { name: 'underscore'; - contents: FragmentNode[]; + contents: [TextNode]; location: LocationRange; }; export type TickNode = { name: 'tick'; - contents: FragmentNode[]; + contents: (TextNode | CommentNode | TagNode)[]; location: LocationRange; }; export type TildeNode = { name: 'tilde'; - contents: FragmentNode[]; + contents: (TextNode | CommentNode | TagNode)[]; location: LocationRange; }; diff --git a/src/parser.ts b/src/parser.ts index 5ba5065..8771ebc 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -20,6 +20,7 @@ import type { OrderedListNode, OrderedListItemNode, UnorderedListItemNode, + FormatNode, } from './node-types'; // TODO types for escapeHtml @@ -283,11 +284,14 @@ export class Parser { return this.finish({ name: 'text', contents }, undefined, endLoc); } - parseFormat(format: Format, opts: ParseFragmentOpts) { + parseFormat( + format: Format, + opts: ParseFragmentOpts + ): (TextNode | CommentNode | TagNode | FormatNode)[] { const startTok = this._t.next() as FormatToken; let contents: (TextNode | CommentNode | TagNode)[] = []; - if (startTok.name === 'underscore') { + if (format === 'underscore') { if (this._t.peek().name === 'text') { contents = [this._t.next() as TextNode]; } @@ -336,6 +340,9 @@ export class Parser { } else { return [this.finish(ntNode, start, end)]; } + } else if (format === 'underscore') { + // the cast is justified by the check at the start of this function + return [this.finish({ name: format, contents: contents as [TextNode] }, start, end)]; } return [this.finish({ name: format, contents }, start, end)]; From 9d2bf8cdd7ab118faa3915c7a99ed42f7da94832 Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Sun, 29 Jan 2023 18:32:04 -0800 Subject: [PATCH 2/3] even more precise type for underscore nodes --- src/emitter.ts | 2 +- src/node-types.ts | 2 +- src/parser.ts | 10 ++++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/emitter.ts b/src/emitter.ts index 0301f34..11d313a 100644 --- a/src/emitter.ts +++ b/src/emitter.ts @@ -122,7 +122,7 @@ export class Emitter { } emitUnderscore(node: UnderscoreNode) { - this.wrapFragment('var', node.contents); + this.str += `${node.contents}`; } emitTag(tag: OpaqueTagNode | CommentNode | TagNode) { diff --git a/src/node-types.ts b/src/node-types.ts index 17bbed4..253a8f8 100644 --- a/src/node-types.ts +++ b/src/node-types.ts @@ -143,7 +143,7 @@ export type StarNode = { export type UnderscoreNode = { name: 'underscore'; - contents: [TextNode]; + contents: string; location: LocationRange; }; diff --git a/src/parser.ts b/src/parser.ts index 8771ebc..7a109d6 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -341,8 +341,14 @@ export class Parser { return [this.finish(ntNode, start, end)]; } } else if (format === 'underscore') { - // the cast is justified by the check at the start of this function - return [this.finish({ name: format, contents: contents as [TextNode] }, start, end)]; + return [ + this.finish( + // the cast is justified by the check at the start of this function + { name: 'underscore', contents: (contents as [TextNode])[0].contents }, + start, + end + ), + ]; } return [this.finish({ name: format, contents }, start, end)]; From 13542fef00a8982b2c2e57e9bb6ca2fc6b5c570f Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Tue, 31 Jan 2023 08:15:54 -0800 Subject: [PATCH 3/3] fix visitor --- src/visitor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/visitor.ts b/src/visitor.ts index edb49d9..0ccca49 100644 --- a/src/visitor.ts +++ b/src/visitor.ts @@ -7,7 +7,7 @@ const childKeys = { algorithm: ['contents'], text: [], star: ['contents'], - underscore: ['contents'], + underscore: [], tick: ['contents'], tilde: ['contents'], pipe: [],