Skip to content

Commit

Permalink
Adding docs on the correct base level namespace (#2643)
Browse files Browse the repository at this point in the history
  • Loading branch information
allenjzhang authored Nov 7, 2023
1 parent 3695abb commit b7a6e41
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/compiler",
"comment": "Fixing @doc and /** */ disappears from multi-segment or nested namespaces. #2642",
"type": "none"
}
],
"packageName": "@typespec/compiler"
}
16 changes: 11 additions & 5 deletions packages/compiler/src/core/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ function createParser(code: string | SourceFile, options: ParseOptions = {}): Pa
item = parseScalarStatement(pos, decorators);
break;
case Token.NamespaceKeyword:
item = parseNamespaceStatement(pos, decorators);
item = parseNamespaceStatement(pos, decorators, docs);
break;
case Token.InterfaceKeyword:
item = parseInterfaceStatement(pos, decorators);
Expand Down Expand Up @@ -455,7 +455,9 @@ function createParser(code: string | SourceFile, options: ParseOptions = {}): Pa
}

mutate(item).directives = directives;
mutate(item).docs = docs;
if (tok !== Token.NamespaceKeyword) {
mutate(item).docs = docs;
}

if (isBlocklessNamespace(item)) {
if (seenBlocklessNs) {
Expand Down Expand Up @@ -506,7 +508,7 @@ function createParser(code: string | SourceFile, options: ParseOptions = {}): Pa
item = parseScalarStatement(pos, decorators);
break;
case Token.NamespaceKeyword:
const ns = parseNamespaceStatement(pos, decorators);
const ns = parseNamespaceStatement(pos, decorators, docs);

if (!Array.isArray(ns.statements)) {
error({ code: "blockless-namespace-first", messageId: "topLevel", target: ns });
Expand Down Expand Up @@ -554,7 +556,9 @@ function createParser(code: string | SourceFile, options: ParseOptions = {}): Pa
break;
}
mutate(item).directives = directives;
mutate(item).docs = docs;
if (tok !== Token.NamespaceKeyword) {
mutate(item).docs = docs;
}
stmts.push(item);
}

Expand Down Expand Up @@ -583,7 +587,8 @@ function createParser(code: string | SourceFile, options: ParseOptions = {}): Pa

function parseNamespaceStatement(
pos: number,
decorators: DecoratorExpressionNode[]
decorators: DecoratorExpressionNode[],
docs: DocNode[]
): NamespaceStatementNode {
parseExpected(Token.NamespaceKeyword);
let currentName = parseIdentifierOrMemberExpression();
Expand All @@ -605,6 +610,7 @@ function createParser(code: string | SourceFile, options: ParseOptions = {}): Pa
let outerNs: NamespaceStatementNode = {
kind: SyntaxKind.NamespaceStatement,
decorators,
docs: docs,
id: nsSegments[0],
locals: undefined!,
statements,
Expand Down
73 changes: 71 additions & 2 deletions packages/compiler/test/decorators/decorators.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { deepStrictEqual, ok, strictEqual } from "assert";
import { Model, Operation, Scalar, getVisibility, isSecret } from "../../src/index.js";
import { Model, Namespace, Operation, Scalar, getVisibility, isSecret } from "../../src/index.js";
import {
getDoc,
getEncode,
Expand All @@ -21,6 +21,75 @@ describe("compiler: built-in decorators", () => {
runner = await createTestRunner();
});

describe("dev comment /** */", () => {
it("applies /** */ on blockless namespace", async () => {
const { Foo } = await runner.compile(
`
@test
/** doc for namespace Foo */
namespace TestDoc.Foo;
model A {}
`
);

strictEqual(getDoc(runner.program, Foo), "doc for namespace Foo");
});

it("applies /** */ on enclosed namespace", async () => {
const { Foo } = await runner.compile(
`
@test
/** doc for namespace Foo */
namespace TestDoc.Foo {
model A {}
}
`
);

strictEqual(getDoc(runner.program, Foo), "doc for namespace Foo");
});

it("applies /** */ on nested enclosed namespace", async () => {
const { Foo } = await runner.compile(
// const { Foo, Foo_Bar } = await runner.compile(
`
@test
/** doc for namespace Foo */
namespace TestDoc.Foo {
/** doc for namespace Bar */
namespace Bar {
model A {};
}
}
`
);

const Bar = (Foo as Namespace).namespaces.get("Bar")!;
strictEqual(getDoc(runner.program, Foo), "doc for namespace Foo");
strictEqual(getDoc(runner.program, Bar), "doc for namespace Bar");
});

it("applies /** */ on nested blockless + enclosed namespace", async () => {
const { Foo } = await runner.compile(
`
@test
/** doc for namespace Foo */
namespace TestDoc.Foo;
/** doc for namespace Bar */
namespace Bar {
model A {}
}
`
);

const Bar = (Foo as Namespace).namespaces.get("Bar")!;
strictEqual(getDoc(runner.program, Foo), "doc for namespace Foo");
strictEqual(getDoc(runner.program, Bar), "doc for namespace Bar");
});
});

describe("@doc", () => {
it("applies @doc on model", async () => {
const { A } = await runner.compile(
Expand Down Expand Up @@ -59,7 +128,7 @@ describe("compiler: built-in decorators", () => {
`
@test
@doc("doc for namespace")
namespace TestDoc {
namespace Foo.TestDoc {
}
`
);
Expand Down

0 comments on commit b7a6e41

Please sign in to comment.