From 65545d689b743d843b3afe080528a8ea48b5534e Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Tue, 16 Apr 2024 09:06:33 -0700 Subject: [PATCH 1/6] Add test case. --- packages/compiler/test/parser.test.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/compiler/test/parser.test.ts b/packages/compiler/test/parser.test.ts index 784064d1e5..c9ce104cb4 100644 --- a/packages/compiler/test/parser.test.ts +++ b/packages/compiler/test/parser.test.ts @@ -5,6 +5,7 @@ import { formatDiagnostic, logVerboseTestOutput } from "../src/core/diagnostics. import { hasParseError, parse, visitChildren } from "../src/core/parser.js"; import { IdentifierNode, + NamespaceStatementNode, Node, ParseOptions, SourceFile, @@ -713,6 +714,26 @@ describe("compiler: parser", () => { { strict: true } ); }); + + describe("ensure directives and decorators are applied to leaf node", () => { + parseEach([ + [ + `@doc("foo")\n#suppress "foo"\nnamespace Foo.Bar {}`, + (node) => { + const fooNs = node.statements[0] as NamespaceStatementNode; + assert(fooNs.kind === SyntaxKind.NamespaceStatement); + const barNs = (fooNs as any).statements as NamespaceStatementNode; + assert(barNs.kind === SyntaxKind.NamespaceStatement); + assert(fooNs.id.sv === "Foo"); + assert(barNs.id.sv === "Bar"); + assert(fooNs.directives?.length === 0); + assert(fooNs.decorators.length === 0); + assert(barNs.directives?.length === 1); + assert(barNs.decorators.length === 1); + }, + ], + ]); + }); }); describe("augment decorator statements", () => { From 7ec18de999c5b29038a3cc647d77bf940610b960 Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Tue, 16 Apr 2024 10:04:06 -0700 Subject: [PATCH 2/6] Fixes #3165. --- packages/compiler/src/core/parser.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/compiler/src/core/parser.ts b/packages/compiler/src/core/parser.ts index 46328c00a0..6bd6ab4205 100644 --- a/packages/compiler/src/core/parser.ts +++ b/packages/compiler/src/core/parser.ts @@ -420,7 +420,7 @@ function createParser(code: string | SourceFile, options: ParseOptions = {}): Pa item = parseScalarStatement(pos, decorators); break; case Token.NamespaceKeyword: - item = parseNamespaceStatement(pos, decorators, docs); + item = parseNamespaceStatement(pos, decorators, docs, directives); break; case Token.InterfaceKeyword: item = parseInterfaceStatement(pos, decorators); @@ -461,8 +461,8 @@ function createParser(code: string | SourceFile, options: ParseOptions = {}): Pa break; } - mutate(item).directives = directives; if (tok !== Token.NamespaceKeyword) { + mutate(item).directives = directives; mutate(item).docs = docs; } @@ -515,7 +515,7 @@ function createParser(code: string | SourceFile, options: ParseOptions = {}): Pa item = parseScalarStatement(pos, decorators); break; case Token.NamespaceKeyword: - const ns = parseNamespaceStatement(pos, decorators, docs); + const ns = parseNamespaceStatement(pos, decorators, docs, directives); if (!Array.isArray(ns.statements)) { error({ code: "blockless-namespace-first", messageId: "topLevel", target: ns }); @@ -595,7 +595,8 @@ function createParser(code: string | SourceFile, options: ParseOptions = {}): Pa function parseNamespaceStatement( pos: number, decorators: DecoratorExpressionNode[], - docs: DocNode[] + docs: DocNode[], + directives: DirectiveExpressionNode[] ): NamespaceStatementNode { parseExpected(Token.NamespaceKeyword); let currentName = parseIdentifierOrMemberExpression(); @@ -621,7 +622,7 @@ function createParser(code: string | SourceFile, options: ParseOptions = {}): Pa id: nsSegments[0], locals: undefined!, statements, - + directives: directives, ...finishNode(pos), }; @@ -629,6 +630,7 @@ function createParser(code: string | SourceFile, options: ParseOptions = {}): Pa outerNs = { kind: SyntaxKind.NamespaceStatement, decorators: [], + directives: [], id: nsSegments[i], statements: outerNs, locals: undefined!, From 69690b06e20eef3217a10ba45fc5fe2f089a0d65 Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Tue, 16 Apr 2024 10:26:00 -0700 Subject: [PATCH 3/6] Create fixDirectiveParsing-2024-3-16-17-9-37.md --- .chronus/changes/fixDirectiveParsing-2024-3-16-17-9-37.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .chronus/changes/fixDirectiveParsing-2024-3-16-17-9-37.md diff --git a/.chronus/changes/fixDirectiveParsing-2024-3-16-17-9-37.md b/.chronus/changes/fixDirectiveParsing-2024-3-16-17-9-37.md new file mode 100644 index 0000000000..9afb07bcb5 --- /dev/null +++ b/.chronus/changes/fixDirectiveParsing-2024-3-16-17-9-37.md @@ -0,0 +1,8 @@ +--- +# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking +changeKind: fix +packages: + - "@typespec/compiler" +--- + +Fix issue where directives were not parsed to the leaf node in multi-segment Namespace segments. From 5c604fd39dafd4348d0b1860c15f600ec4224998 Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Tue, 16 Apr 2024 10:29:20 -0700 Subject: [PATCH 4/6] Code review feedback. --- packages/compiler/test/parser.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/compiler/test/parser.test.ts b/packages/compiler/test/parser.test.ts index c9ce104cb4..a1df955e24 100644 --- a/packages/compiler/test/parser.test.ts +++ b/packages/compiler/test/parser.test.ts @@ -720,9 +720,9 @@ describe("compiler: parser", () => { [ `@doc("foo")\n#suppress "foo"\nnamespace Foo.Bar {}`, (node) => { - const fooNs = node.statements[0] as NamespaceStatementNode; + const fooNs = node.statements[0]; assert(fooNs.kind === SyntaxKind.NamespaceStatement); - const barNs = (fooNs as any).statements as NamespaceStatementNode; + const barNs = (fooNs as any).statements; assert(barNs.kind === SyntaxKind.NamespaceStatement); assert(fooNs.id.sv === "Foo"); assert(barNs.id.sv === "Bar"); From 46c5f8156adf471e51ed8c6e31bb40323f5712a3 Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Tue, 16 Apr 2024 10:48:29 -0700 Subject: [PATCH 5/6] Code review feedback. --- packages/compiler/test/parser.test.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/compiler/test/parser.test.ts b/packages/compiler/test/parser.test.ts index a1df955e24..d056323592 100644 --- a/packages/compiler/test/parser.test.ts +++ b/packages/compiler/test/parser.test.ts @@ -721,15 +721,15 @@ describe("compiler: parser", () => { `@doc("foo")\n#suppress "foo"\nnamespace Foo.Bar {}`, (node) => { const fooNs = node.statements[0]; - assert(fooNs.kind === SyntaxKind.NamespaceStatement); + strictEqual(fooNs.kind, SyntaxKind.NamespaceStatement); const barNs = (fooNs as any).statements; - assert(barNs.kind === SyntaxKind.NamespaceStatement); - assert(fooNs.id.sv === "Foo"); - assert(barNs.id.sv === "Bar"); - assert(fooNs.directives?.length === 0); - assert(fooNs.decorators.length === 0); - assert(barNs.directives?.length === 1); - assert(barNs.decorators.length === 1); + strictEqual(barNs.kind, SyntaxKind.NamespaceStatement); + strictEqual(fooNs.id.sv, "Foo"); + strictEqual(barNs.id.sv, "Bar"); + strictEqual(fooNs.directives?.length, 0); + strictEqual(fooNs.decorators.length, 0); + strictEqual(barNs.directives?.length, 1); + strictEqual(barNs.decorators.length, 1); }, ], ]); From 2adc6b57aacea505e06c6a09a820e8a63d67f1b3 Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Tue, 16 Apr 2024 11:13:37 -0700 Subject: [PATCH 6/6] tsp format and linter stuff. --- packages/compiler/test/parser.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/compiler/test/parser.test.ts b/packages/compiler/test/parser.test.ts index d056323592..6d78183e8a 100644 --- a/packages/compiler/test/parser.test.ts +++ b/packages/compiler/test/parser.test.ts @@ -5,7 +5,6 @@ import { formatDiagnostic, logVerboseTestOutput } from "../src/core/diagnostics. import { hasParseError, parse, visitChildren } from "../src/core/parser.js"; import { IdentifierNode, - NamespaceStatementNode, Node, ParseOptions, SourceFile,