Skip to content
TypeScript

fix(web): keep pasted decorators as text (#8415)

Guillermo Casanova729924af29f3
diff --git a/packages/shared/src/composerInlineTokens.test.ts b/packages/shared/src/composerInlineTokens.test.ts
index 245157a3a30..1025fc77041 100644
--- a/packages/shared/src/composerInlineTokens.test.ts
+++ b/packages/shared/src/composerInlineTokens.test.ts
@@ -125,8 +125,30 @@ describe("collectComposerInlineTokens", () => {
expect(collectComposerInlineTokens("入力 @expo/ui を追加")).toEqual([]);
});
- it("keeps bare non-scoped file paths as mentions", () => {
- expect(collectComposerInlineTokens("Inspect @README.md next")).toEqual([
+ it.each(["@namespace.foo(bar)", "@écrire(option)", "@e\u0301crire(option)"])(
+ "keeps decorator call %s as plain text",
+ (reference) => {
+ expect(collectComposerInlineTokens(`${reference}\nclass Example {}`)).toEqual([]);
+ },
+ );
+
+ it.each(['@"foo(bar)"', "[foo(bar)](foo%28bar%29)"])(
+ "keeps an explicit decorator-shaped file reference as a mention: %s",
+ (reference) => {
+ expect(collectComposerInlineTokens(`${reference} `)).toEqual([
+ {
+ type: "mention",
+ value: "foo(bar)",
+ source: reference,
+ start: 0,
+ end: reference.length,
+ },
+ ]);
+ },
+ );
+
+ it("keeps bare file mentions before whitespace-separated parentheses", () => {
+ expect(collectComposerInlineTokens("Inspect @README.md (notes)")).toEqual([
{
type: "mention",
value: "README.md",
diff --git a/packages/shared/src/composerInlineTokens.ts b/packages/shared/src/composerInlineTokens.ts
index 90051d18e8f..d54b39317e8 100644
--- a/packages/shared/src/composerInlineTokens.ts
+++ b/packages/shared/src/composerInlineTokens.ts
@@ -40,6 +40,7 @@ const URI_SCHEME_REGEX = /^[A-Za-z][A-Za-z0-9+.-]*:/;
const WINDOWS_DRIVE_PATH_REGEX = /^[A-Za-z]:[\\/]/;
const SCOPED_PACKAGE_REFERENCE_REGEX =
/^[a-z0-9][a-z0-9._-]*\/[a-z0-9][a-z0-9._-]*(?:\/[^\s@"]+)*$/;
+const DECORATOR_CALL_REFERENCE_REGEX = /^[\p{ID_Start}_$][\p{ID_Continue}.$\u200c\u200d]*\(/u;
function collectMentionTokens(text: string): ComposerInlineToken[] {
const matches: ComposerInlineToken[] = [];
@@ -75,7 +76,11 @@ function collectMentionTokens(text: string): ComposerInlineToken[] {
const prefix = match[1] ?? "";
const quotedPath = match[2];
const path = quotedPath !== undefined ? quotedPath.replace(/\\(.)/g, "$1") : (match[3] ?? "");
- if (!path || (quotedPath === undefined && SCOPED_PACKAGE_REFERENCE_REGEX.test(path))) {
+ if (
+ !path ||
+ (quotedPath === undefined &&
+ (SCOPED_PACKAGE_REFERENCE_REGEX.test(path) || DECORATOR_CALL_REFERENCE_REGEX.test(path)))
+ ) {
continue;
}
const start = (match.index ?? 0) + prefix.length;