Skip to content

Commit

Permalink
fix: allow symbols to be escaped in DocText
Browse files Browse the repository at this point in the history
  • Loading branch information
rmhartog committed May 16, 2024
1 parent 69d6cb7 commit 2408c62
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/compiler/src/core/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2828,6 +2828,12 @@ function createParser(code: string | SourceFile, options: ParseOptions = {}): Pa
}
nextToken();
break;
case Token.DocText:
parts.push(source.substring(start, tokenPos()));
parts.push(tokenValue());
nextToken();
start = tokenPos();
break;
default:
nextToken();
break;
Expand Down
16 changes: 16 additions & 0 deletions packages/compiler/src/core/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@ export function createScanner(
return getStringTokenValue(token, tokenFlags);
case Token.Identifier:
return getIdentifierTokenValue();
case Token.DocText:
return getDocTextValue();
default:
return getTokenText();
}
Expand Down Expand Up @@ -656,6 +658,10 @@ export function createScanner(
case CharCode.LineFeed:
return next(Token.NewLine);

case CharCode.Backslash:
tokenFlags |= TokenFlags.Escaped;
return next(Token.DocText, 2);

case CharCode.Space:
case CharCode.Tab:
case CharCode.VerticalTab:
Expand Down Expand Up @@ -1036,6 +1042,14 @@ export function createScanner(
return text;
}

function getDocTextValue(): string {
if (tokenFlags & TokenFlags.Escaped) {
return unescapeString(tokenPosition, position);
} else {
return input.substring(tokenPosition, position);
}
}

function findTripleQuotedStringIndent(start: number, end: number): [number, number] {
end = end - 3; // Remove the """
// remove whitespace before closing delimiter and record it as required
Expand Down Expand Up @@ -1231,6 +1245,8 @@ export function createScanner(
return "\\";
case CharCode.$:
return "$";
case CharCode.At:
return "@";
case CharCode.Backtick:
return "`";
default:
Expand Down
4 changes: 3 additions & 1 deletion packages/compiler/test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,8 @@ describe("compiler: parser", () => {
* \`\`\`
*
* \`This is not a @tag either because we're in a code span\`.
*
* This is not a \\@tag because it is escaped.
*
* @param x the param
* that continues on another line
Expand All @@ -1036,7 +1038,7 @@ describe("compiler: parser", () => {

strictEqual(
docs[0].content[0].text,
"This one has a `code span` and a code fence and it spreads over\nmore than one line.\n\n```\nThis is not a @tag because we're in a code fence.\n```\n\n`This is not a @tag either because we're in a code span`."
"This one has a `code span` and a code fence and it spreads over\nmore than one line.\n\n```\nThis is not a @tag because we're in a code fence.\n```\n\n`This is not a @tag either because we're in a code span`.\n\nThis is not a @tag because it is escaped."
);
strictEqual(docs[0].tags.length, 6);
const [xParam, yParam, tTemplate, uTemplate, returns, pretend] = docs[0].tags;
Expand Down

0 comments on commit 2408c62

Please sign in to comment.