Skip to content

Commit 65867dd

Browse files
Merge branch 'main' into Issue3632
2 parents a0431dd + 71ecc06 commit 65867dd

File tree

13 files changed

+81
-22
lines changed

13 files changed

+81
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
3+
changeKind: fix
4+
packages:
5+
- "@typespec/compiler"
6+
---
7+
8+
Deprecate getAssetEmitter and recommend calling `createAssetEmitter` directly
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
3+
changeKind: fix
4+
packages:
5+
- "@typespec/json-schema"
6+
- "@typespec/openapi3"
7+
---
8+
9+
Fix issue that could result in invalid document generation when running `tsp compile` from another directory

docs/extending-typespec/create-decorators.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Decorators can be implemented in JavaScript by prefixing the function name with
8181
// model.ts
8282
import type { DecoratorContext, Type } from "@typespec/compiler";
8383

84-
export function $logType(context: DecoratorContext, target: Type, name: valueof string) {
84+
export function $logType(context: DecoratorContext, target: Type, name: string) {
8585
console.log(name + ": " + targetType.kind);
8686
}
8787
```

packages/compiler/src/core/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2639,6 +2639,8 @@ export interface EmitContext<TOptions extends object = Record<string, never>> {
26392639
/**
26402640
* Get an asset emitter to write emitted output to disk using a TypeEmitter
26412641
*
2642+
* @deprecated call {@link createAssetEmitter} directly instead.
2643+
*
26422644
* @param TypeEmitterClass The TypeEmitter to construct your emitted output
26432645
*/
26442646
getAssetEmitter<T>(TypeEmitterClass: typeof TypeEmitter<T, TOptions>): AssetEmitter<T, TOptions>;

packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/src/Providers/ClientPipelineExtensionsProvider.cs

-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ namespace Microsoft.Generator.CSharp.ClientModel.Providers
1717
{
1818
internal class ClientPipelineExtensionsProvider : TypeProvider
1919
{
20-
private static readonly Lazy<ClientPipelineExtensionsProvider> _instance = new(() => new ClientPipelineExtensionsProvider());
21-
22-
public static ClientPipelineExtensionsProvider Instance => _instance.Value;
23-
2420
private const string _processMessageAsync = "ProcessMessageAsync";
2521
private const string _processMessage = "ProcessMessage";
2622
private const string _processHeadAsBoolMessageAsync = "ProcessHeadAsBoolMessageAsync";

packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Expressions/CatchExpression.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ internal override void Write(CodeWriter writer)
1414
{
1515
writer.AppendRaw(" (");
1616
Exception.Write(writer);
17-
writer.AppendRaw(")");
17+
writer.WriteRawLine(")");
18+
}
19+
20+
using (writer.Scope())
21+
{
22+
Body.Write(writer);
1823
}
19-
writer.WriteRawLine("{");
20-
Body.Write(writer);
21-
writer.WriteRawLine("}");
2224
}
2325
}
2426
}

packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Statements/TryCatchFinallyStatement.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ public TryCatchFinallyStatement(MethodBodyStatement Try, CatchExpression Catch,
3131
internal override void Write(CodeWriter writer)
3232
{
3333
writer.WriteRawLine("try");
34-
writer.WriteRawLine("{");
35-
Try.Write(writer);
36-
writer.WriteRawLine("}");
34+
using (writer.Scope())
35+
{
36+
Try.Write(writer);
37+
}
3738

3839
foreach (var catchStatement in Catches)
3940
{
@@ -43,9 +44,10 @@ internal override void Write(CodeWriter writer)
4344
if (Finally != null)
4445
{
4546
writer.WriteRawLine("finally");
46-
writer.WriteRawLine("{");
47-
Finally.Write(writer);
48-
writer.WriteRawLine("}");
47+
using (writer.Scope())
48+
{
49+
Finally.Write(writer);
50+
}
4951
}
5052
}
5153
}

packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Statements/StatementTests.cs

+24-2
Original file line numberDiff line numberDiff line change
@@ -359,17 +359,39 @@ public void TryCatchFinallyStatementWithTryCatchAndFinally()
359359
public void TryCatchFinallyStatementWithMultipleCatches()
360360
{
361361
var tryStatement = new MethodBodyStatement();
362+
var var1 = new DeclarationExpression(typeof(UnauthorizedAccessException), "ex1");
363+
var var2 = new DeclarationExpression(typeof(Exception), "ex2");
362364
var catchStatements = new[]
363365
{
364-
new CatchExpression(null, new MethodBodyStatement()),
365-
new CatchExpression(null, new MethodBodyStatement())
366+
new CatchExpression(var1, new MethodBodyStatement()),
367+
new CatchExpression(var2, new MethodBodyStatement())
366368
};
367369
var finallyStatement = new MethodBodyStatement();
368370
var tryCatchFinally = new TryCatchFinallyStatement(tryStatement, catchStatements, finallyStatement);
369371

370372
Assert.AreEqual(tryStatement, tryCatchFinally.Try);
371373
CollectionAssert.AreEqual(catchStatements, tryCatchFinally.Catches);
372374
Assert.AreEqual(finallyStatement, tryCatchFinally.Finally);
375+
376+
var mockTypeProvider = new Mock<TypeProvider>();
377+
378+
// Create a method declaration statement
379+
var method = new MethodProvider(
380+
new MethodSignature(
381+
Name: "Foo",
382+
Modifiers: MethodSignatureModifiers.Public,
383+
ReturnType: new CSharpType(typeof(bool)),
384+
Parameters: [],
385+
Description: null, ReturnDescription: null),
386+
new MethodBodyStatement[] { tryCatchFinally },
387+
mockTypeProvider.Object);
388+
389+
// Verify the expected behavior
390+
using var writer = new CodeWriter();
391+
writer.WriteMethod(method);
392+
var expectedResult = Helpers.GetExpectedFromFile();
393+
var test = writer.ToString(false);
394+
Assert.AreEqual(expectedResult, test);
373395
}
374396

375397
[Test]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public bool Foo()
2+
{
3+
try
4+
{
5+
}
6+
catch (global::System.UnauthorizedAccessException ex1)
7+
{
8+
}
9+
catch (global::System.Exception ex2)
10+
{
11+
}
12+
finally
13+
{
14+
}
15+
}

packages/json-schema/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
setTypeSpecNamespace,
1515
typespecTypeToJson,
1616
} from "@typespec/compiler";
17+
import { createAssetEmitter } from "@typespec/compiler/emitter-framework";
1718
import { ValidatesRawJsonDecorator } from "../generated-defs/TypeSpec.JsonSchema.Private.js";
1819
import {
1920
BaseUriDecorator,
@@ -45,7 +46,7 @@ export type JsonSchemaDeclaration = Model | Union | Enum | Scalar;
4546
const jsonSchemaKey = createStateSymbol("JsonSchema");
4647

4748
export async function $onEmit(context: EmitContext<JSONSchemaEmitterOptions>) {
48-
const emitter = context.getAssetEmitter(JsonSchemaEmitter);
49+
const emitter = createAssetEmitter(context.program, JsonSchemaEmitter as any, context);
4950

5051
if (emitter.getOptions().emitAllModels) {
5152
emitter.emitProgram({ emitTypeSpecNamespace: false });

packages/openapi3/src/openapi.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,14 @@ function createOAPIEmitter(
301301
service.type,
302302
options.omitUnreachableTypes
303303
);
304-
schemaEmitter = context.getAssetEmitter(
304+
schemaEmitter = createAssetEmitter(
305+
program,
305306
class extends OpenAPI3SchemaEmitter {
306307
constructor(emitter: AssetEmitter<Record<string, any>, OpenAPI3EmitterOptions>) {
307308
super(emitter, metadataInfo, visibilityUsage, options);
308309
}
309-
} as any
310+
} as any,
311+
context
310312
);
311313

312314
const securitySchemes = getOpenAPISecuritySchemes(allHttpAuthentications);

packages/website/static/1ds-init.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/website/versioned_docs/version-latest/extending-typespec/create-decorators.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Decorators can be implemented in JavaScript by prefixing the function name with
8181
// model.ts
8282
import type { DecoratorContext, Type } from "@typespec/compiler";
8383

84-
export function $logType(context: DecoratorContext, target: Type, name: valueof string) {
84+
export function $logType(context: DecoratorContext, target: Type, name: string) {
8585
console.log(name + ": " + targetType.kind);
8686
}
8787
```

0 commit comments

Comments
 (0)