Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding docs on the correct base level namespace #2643

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading