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

Fix namespace in blockless namespace with same name #4050

Merged
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,14 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---

Allow using compact namespace form `Foo.Bar` when inside another namespace
```tsp
namespace MyOrg.MyProject {
namespace MyModule.MySubmodule { // <-- this used to emit an error
// ...
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
changeKind: breaking
packages:
- "@typespec/compiler"
---

Fix issue where naming a namespace with the same name as the blockless namespace would merge with it instead of creating a subnamespace like any other name would.

```tsp
namespace MyOrg.MyProject;

namespace MyOrg.MyProject.MyArea {
model A {}
}

namespace MyArea2 {
model B {}
}
```

Previously model `A` would end-up in namespace `MyOrg.MyProject.MyArea` and model `B` in `MyOrg.MyProject.MyArea2`. With this change model `A` will now be in `MyOrg.MyProject.MyOrg.MyProject.MyArea`. To achieve the previous behavior the above code should be written as:

```tsp
namespace MyOrg.MyProject;

namespace MyArea {
model A {}
}

namespace MyArea2 {
model B {}
}
```
3 changes: 2 additions & 1 deletion packages/compiler/src/core/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,9 @@ export function createBinder(program: Program): Binder {
}

function bindNamespaceStatement(statement: NamespaceStatementNode) {
const effectiveScope = fileNamespace ?? scope;
// check if there's an existing symbol for this namespace
const existingBinding = (scope as NamespaceStatementNode).symbol.exports!.get(statement.id.sv);
const existingBinding = effectiveScope.symbol.exports!.get(statement.id.sv);
if (existingBinding && existingBinding.flags & SymbolFlags.Namespace) {
mutate(statement).symbol = existingBinding;
// locals are never shared.
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/core/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ function createParser(code: string | SourceFile, options: ParseOptions = {}): Pa
case Token.NamespaceKeyword:
const ns = parseNamespaceStatement(pos, decorators, docs, directives);

if (!Array.isArray(ns.statements)) {
if (isBlocklessNamespace(ns)) {
error({ code: "blockless-namespace-first", messageId: "topLevel", target: ns });
}
item = ns;
Expand Down
38 changes: 38 additions & 0 deletions packages/compiler/test/binder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,44 @@ describe("compiler: binder", () => {
},
});
});

it("namespace inside blockless namespace with the same name", () => {
const code = `
namespace A.B;
namespace A.B {
model D { }
}
`;
const script = bindTypeSpec(code);
strictEqual(script.namespaces.length, 4);
assertBindings("root", script.symbol.exports!, {
A: {
declarations: [SyntaxKind.NamespaceStatement],
flags: SymbolFlags.Namespace,
exports: {
B: {
flags: SymbolFlags.Namespace,
exports: {
A: {
declarations: [SyntaxKind.NamespaceStatement],
flags: SymbolFlags.Namespace,
exports: {
B: {
flags: SymbolFlags.Namespace,
exports: {
D: {
flags: SymbolFlags.Model,
},
},
},
},
},
},
},
},
},
});
});
it("binds namespaces", () => {
const code = `
namespace A {
Expand Down
Loading