-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No private usage linter rule (#1193)
fix [#977](#977) Add new linter rule to prevent using items from Private namespace from an external library.
- Loading branch information
1 parent
c0ff005
commit 1726967
Showing
10 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
.chronus/changes/no-private-linter-rule-2024-6-17-15-10-51.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
changeKind: feature | ||
packages: | ||
- "@azure-tools/typespec-azure-core" | ||
--- | ||
|
||
Add new linter rule to prevent using items from Private namespace from an external library. |
7 changes: 7 additions & 0 deletions
7
.chronus/changes/no-private-linter-rule-2024-6-17-15-11-17.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
changeKind: feature | ||
packages: | ||
- "@azure-tools/typespec-azure-rulesets" | ||
--- | ||
|
||
Add new `no-private-usage` linter rule to `data-plane` and `resource-manager` rulesets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
title: "no-private-usage" | ||
--- | ||
|
||
```text title="Full name" | ||
@azure-tools/typespec-azure-core/no-private-usage | ||
``` | ||
|
||
Verify that a spec is not referencing items from another library using a private namespace. | ||
|
||
#### ❌ Incorrect | ||
|
||
```ts | ||
@Azure.Core.Foundations.Private.embeddingVector(string) | ||
model Foo {} | ||
``` | ||
|
||
#### ✅ Ok | ||
|
||
Using items from a private namespace within the same library is allowed. | ||
|
||
```ts | ||
namespace MyService; | ||
|
||
@MyService.Private.myPrivateDecorator | ||
model Foo {} | ||
|
||
|
||
namespace Private { | ||
extern dec myPrivateDecorator(target); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
packages/typespec-azure-core/src/rules/no-private-usage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { | ||
createRule, | ||
DecoratedType, | ||
DiagnosticTarget, | ||
getLocationContext, | ||
getTypeName, | ||
Namespace, | ||
paramMessage, | ||
Type, | ||
} from "@typespec/compiler"; | ||
|
||
export const noPrivateUsage = createRule({ | ||
name: "no-private-usage", | ||
description: "Verify that elements inside Private namespace are not referenced.", | ||
severity: "warning", | ||
url: "https://azure.github.io/typespec-azure/docs/libraries/azure-core/rules/no-private-usage", | ||
messages: { | ||
default: paramMessage`Referencing elements inside Private namespace "${"ns"}" is not allowed.`, | ||
}, | ||
create(context) { | ||
function checkReference(origin: Type, type: Type, target: DiagnosticTarget) { | ||
if (getLocationContext(context.program, origin).type !== "project") { | ||
return; | ||
} | ||
if (getLocationContext(context.program, type).type === "project") { | ||
return; | ||
} | ||
if (isInPrivateNamespace(type)) { | ||
context.reportDiagnostic({ | ||
target, | ||
format: { ns: getTypeName(type.namespace) }, | ||
}); | ||
} | ||
} | ||
|
||
function checkDecorators(type: Type & DecoratedType) { | ||
if (getLocationContext(context.program, type).type !== "project") { | ||
return; | ||
} | ||
for (const decorator of type.decorators) { | ||
if ( | ||
decorator.definition && | ||
isInPrivateNamespace(decorator.definition) && | ||
getLocationContext(context.program, decorator.definition).type !== "project" | ||
) { | ||
context.reportDiagnostic({ | ||
target: decorator.node ?? type, | ||
format: { ns: getTypeName(decorator.definition.namespace) }, | ||
}); | ||
} | ||
} | ||
} | ||
return { | ||
model: (model) => { | ||
checkDecorators(model); | ||
model.baseModel && checkReference(model, model.baseModel, model); | ||
}, | ||
modelProperty: (prop) => { | ||
checkDecorators(prop); | ||
checkReference(prop, prop.type, prop); | ||
}, | ||
unionVariant: (variant) => { | ||
checkDecorators(variant); | ||
checkReference(variant, variant.type, variant); | ||
}, | ||
operation: (type) => { | ||
checkDecorators(type); | ||
}, | ||
interface: (type) => { | ||
checkDecorators(type); | ||
}, | ||
enum: (type) => { | ||
checkDecorators(type); | ||
}, | ||
union: (type) => { | ||
checkDecorators(type); | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
function isInPrivateNamespace(type: Type): type is Type & { namespace: Namespace } { | ||
if (!("namespace" in type)) { | ||
return false; | ||
} | ||
let current = type; | ||
while (current.namespace) { | ||
if (current.namespace?.name === "Private") { | ||
return true; | ||
} | ||
current = current.namespace; | ||
} | ||
return false; | ||
} |
75 changes: 75 additions & 0 deletions
75
packages/typespec-azure-core/test/rules/no-private-usage.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { | ||
BasicTestRunner, | ||
LinterRuleTester, | ||
createLinterRuleTester, | ||
} from "@typespec/compiler/testing"; | ||
import { beforeEach, it } from "vitest"; | ||
import { noPrivateUsage } from "../../src/rules/no-private-usage.js"; | ||
import { createAzureCoreTestRunner } from "../test-host.js"; | ||
|
||
let runner: BasicTestRunner; | ||
let tester: LinterRuleTester; | ||
|
||
beforeEach(async () => { | ||
runner = await createAzureCoreTestRunner({ omitServiceNamespace: true }); | ||
tester = createLinterRuleTester(runner, noPrivateUsage, "@azure-tools/typespec-azure-core"); | ||
}); | ||
|
||
it("emits a warning diagnostic if using type from Azure.Core.Foundations.Private", async () => { | ||
await tester | ||
.expect( | ||
` | ||
@useDependency(Azure.Core.Versions.v1_0_Preview_2) | ||
namespace MyService { | ||
model Foo { | ||
bar: Azure.Core.Foundations.Private.ArmResourceIdentifierConfigOptions | ||
} | ||
} | ||
` | ||
) | ||
.toEmitDiagnostics([ | ||
{ | ||
code: "@azure-tools/typespec-azure-core/no-private-usage", | ||
message: | ||
'Referencing elements inside Private namespace "Azure.Core.Foundations.Private" is not allowed.', | ||
}, | ||
]); | ||
}); | ||
|
||
it("emits a warning diagnostic if using decorators from Azure.Core.Foundations.Private", async () => { | ||
await tester | ||
.expect( | ||
` | ||
@useDependency(Azure.Core.Versions.v1_0_Preview_2) | ||
namespace MyService { | ||
@Azure.Core.Foundations.Private.embeddingVector(string) | ||
model Foo {} | ||
} | ||
` | ||
) | ||
.toEmitDiagnostics([ | ||
{ | ||
code: "@azure-tools/typespec-azure-core/no-private-usage", | ||
message: | ||
'Referencing elements inside Private namespace "Azure.Core.Foundations.Private" is not allowed.', | ||
}, | ||
]); | ||
}); | ||
|
||
it("ok using item from Private namespace in the project", async () => { | ||
await tester | ||
.expect( | ||
` | ||
namespace MyService { | ||
model Foo { | ||
bar: MyLib.Private.Bar; | ||
} | ||
} | ||
namespace MyLib.Private { | ||
model Bar {} | ||
} | ||
` | ||
) | ||
.toBeValid(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters