@typespec/compiler 0.46.0-dev.9 → 0.47.0-dev.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/manifest.js +2 -2
- package/dist/src/config/config-loader.d.ts.map +1 -1
- package/dist/src/config/config-loader.js +1 -0
- package/dist/src/config/config-loader.js.map +1 -1
- package/dist/src/config/config-schema.d.ts.map +1 -1
- package/dist/src/config/config-schema.js +24 -0
- package/dist/src/config/config-schema.js.map +1 -1
- package/dist/src/config/types.d.ts +8 -1
- package/dist/src/config/types.d.ts.map +1 -1
- package/dist/src/core/checker.d.ts.map +1 -1
- package/dist/src/core/checker.js +13 -8
- package/dist/src/core/checker.js.map +1 -1
- package/dist/src/core/cli/args.d.ts.map +1 -1
- package/dist/src/core/cli/args.js +1 -0
- package/dist/src/core/cli/args.js.map +1 -1
- package/dist/src/core/helpers/index.d.ts +1 -0
- package/dist/src/core/helpers/index.d.ts.map +1 -1
- package/dist/src/core/helpers/index.js +1 -0
- package/dist/src/core/helpers/index.js.map +1 -1
- package/dist/src/core/helpers/location-context.d.ts +4 -0
- package/dist/src/core/helpers/location-context.d.ts.map +1 -0
- package/dist/src/core/helpers/location-context.js +9 -0
- package/dist/src/core/helpers/location-context.js.map +1 -0
- package/dist/src/core/helpers/type-name-utils.d.ts.map +1 -1
- package/dist/src/core/helpers/type-name-utils.js +8 -3
- package/dist/src/core/helpers/type-name-utils.js.map +1 -1
- package/dist/src/core/index.d.ts +1 -1
- package/dist/src/core/index.d.ts.map +1 -1
- package/dist/src/core/index.js +1 -1
- package/dist/src/core/index.js.map +1 -1
- package/dist/src/core/library.d.ts +3 -1
- package/dist/src/core/library.d.ts.map +1 -1
- package/dist/src/core/library.js +6 -1
- package/dist/src/core/library.js.map +1 -1
- package/dist/src/core/linter.d.ts +10 -0
- package/dist/src/core/linter.d.ts.map +1 -0
- package/dist/src/core/linter.js +166 -0
- package/dist/src/core/linter.js.map +1 -0
- package/dist/src/core/messages.d.ts +77 -2
- package/dist/src/core/messages.d.ts.map +1 -1
- package/dist/src/core/messages.js +27 -0
- package/dist/src/core/messages.js.map +1 -1
- package/dist/src/core/node-host.d.ts +3 -0
- package/dist/src/core/node-host.d.ts.map +1 -1
- package/dist/src/core/node-host.js +22 -1
- package/dist/src/core/node-host.js.map +1 -1
- package/dist/src/core/options.d.ts +3 -1
- package/dist/src/core/options.d.ts.map +1 -1
- package/dist/src/core/parser.d.ts.map +1 -1
- package/dist/src/core/parser.js +56 -29
- package/dist/src/core/parser.js.map +1 -1
- package/dist/src/core/program.d.ts +3 -17
- package/dist/src/core/program.d.ts.map +1 -1
- package/dist/src/core/program.js +90 -46
- package/dist/src/core/program.js.map +1 -1
- package/dist/src/core/schema-validator.d.ts +1 -0
- package/dist/src/core/schema-validator.d.ts.map +1 -1
- package/dist/src/core/schema-validator.js +2 -2
- package/dist/src/core/schema-validator.js.map +1 -1
- package/dist/src/core/types.d.ts +110 -11
- package/dist/src/core/types.d.ts.map +1 -1
- package/dist/src/core/types.js.map +1 -1
- package/dist/src/emitter-framework/asset-emitter.d.ts.map +1 -1
- package/dist/src/emitter-framework/asset-emitter.js +89 -45
- package/dist/src/emitter-framework/asset-emitter.js.map +1 -1
- package/dist/src/emitter-framework/type-emitter.d.ts +43 -22
- package/dist/src/emitter-framework/type-emitter.d.ts.map +1 -1
- package/dist/src/emitter-framework/type-emitter.js +90 -23
- package/dist/src/emitter-framework/type-emitter.js.map +1 -1
- package/dist/src/emitter-framework/types.d.ts +14 -4
- package/dist/src/emitter-framework/types.d.ts.map +1 -1
- package/dist/src/emitter-framework/types.js +1 -0
- package/dist/src/emitter-framework/types.js.map +1 -1
- package/dist/src/formatter/parser.d.ts.map +1 -1
- package/dist/src/formatter/parser.js +4 -1
- package/dist/src/formatter/parser.js.map +1 -1
- package/dist/src/formatter/print/comment-handler.d.ts +1 -1
- package/dist/src/formatter/print/comment-handler.d.ts.map +1 -1
- package/dist/src/formatter/print/comment-handler.js +37 -5
- package/dist/src/formatter/print/comment-handler.js.map +1 -1
- package/dist/src/formatter/print/needs-parens.d.ts +1 -1
- package/dist/src/formatter/print/needs-parens.js +1 -1
- package/dist/src/formatter/print/printer.d.ts +3 -1
- package/dist/src/formatter/print/printer.d.ts.map +1 -1
- package/dist/src/formatter/print/printer.js +118 -32
- package/dist/src/formatter/print/printer.js.map +1 -1
- package/dist/src/init/init-template.d.ts +3 -1
- package/dist/src/init/init-template.d.ts.map +1 -1
- package/dist/src/init/init-template.js +3 -6
- package/dist/src/init/init-template.js.map +1 -1
- package/dist/src/init/init.d.ts.map +1 -1
- package/dist/src/init/init.js +96 -16
- package/dist/src/init/init.js.map +1 -1
- package/dist/src/lib/decorators.d.ts +9 -0
- package/dist/src/lib/decorators.d.ts.map +1 -1
- package/dist/src/lib/decorators.js +9 -0
- package/dist/src/lib/decorators.js.map +1 -1
- package/dist/src/testing/index.d.ts +1 -0
- package/dist/src/testing/index.d.ts.map +1 -1
- package/dist/src/testing/index.js +1 -0
- package/dist/src/testing/index.js.map +1 -1
- package/dist/src/testing/rule-tester.d.ts +12 -0
- package/dist/src/testing/rule-tester.d.ts.map +1 -0
- package/dist/src/testing/rule-tester.js +34 -0
- package/dist/src/testing/rule-tester.js.map +1 -0
- package/entrypoints/cli.js +5 -0
- package/entrypoints/server.js +5 -0
- package/lib/decorators.tsp +8 -0
- package/package.json +12 -9
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { getSourceLocation } from "../core/diagnostics.js";
|
|
2
2
|
import { parse as typespecParse, visitChildren } from "../core/parser.js";
|
|
3
3
|
import { SyntaxKind } from "../core/types.js";
|
|
4
|
+
import { mutate } from "../core/util.js";
|
|
4
5
|
export function parse(text, parsers, opts) {
|
|
5
|
-
const result = typespecParse(text, { comments: true, docs:
|
|
6
|
+
const result = typespecParse(text, { comments: true, docs: true });
|
|
6
7
|
flattenNamespaces(result);
|
|
7
8
|
const errors = result.parseDiagnostics.filter((x) => x.severity === "error");
|
|
8
9
|
if (errors.length > 0 && !result.printable) {
|
|
9
10
|
throw new PrettierParserError(errors[0]);
|
|
10
11
|
}
|
|
12
|
+
// Remove doc comments as those are handled directly.
|
|
13
|
+
mutate(result).comments = result.comments.filter((x) => !(x.kind === SyntaxKind.BlockComment && x.parsedAsDocs));
|
|
11
14
|
return result;
|
|
12
15
|
}
|
|
13
16
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parser.js","sourceRoot":"","sources":["../../../src/formatter/parser.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,KAAK,IAAI,aAAa,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAC1E,OAAO,EAAoB,UAAU,EAAsB,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"parser.js","sourceRoot":"","sources":["../../../src/formatter/parser.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,KAAK,IAAI,aAAa,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAC1E,OAAO,EAAoB,UAAU,EAAsB,MAAM,kBAAkB,CAAC;AACpF,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC,MAAM,UAAU,KAAK,CACnB,IAAY,EACZ,OAAyC,EACzC,IAA+C;IAE/C,MAAM,MAAM,GAAG,aAAa,CAAC,IAAI,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IAEnE,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAE1B,MAAM,MAAM,GAAG,MAAM,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC;IAC7E,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,IAAI,CAA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AAA,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,GAAG,mCAAI,CAAC;YACzB,GAAG,EAAE,MAAA,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,GAAG,mCAAI,CAAC;SACxB,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"comment-handler.d.ts","sourceRoot":"","sources":["../../../../src/formatter/print/comment-handler.ts"],"names":[],"mappings":"AAAA,OAAiB,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAE,IAAI,
|
|
1
|
+
{"version":3,"file":"comment-handler.d.ts","sourceRoot":"","sources":["../../../../src/formatter/print/comment-handler.ts"],"names":[],"mappings":"AAAA,OAAiB,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAkC,MAAM,qBAAqB,CAAC;AAU3E;;GAEG;AACH,eAAO,MAAM,cAAc,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,gBAAgB,CAQ1D,CAAC"}
|
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import prettier from "prettier";
|
|
2
|
-
import { SyntaxKind } from "../../core/
|
|
2
|
+
import { SyntaxKind } from "../../core/types.js";
|
|
3
3
|
const { util } = prettier;
|
|
4
4
|
/**
|
|
5
5
|
* Override the default behavior to attach comments to syntax node.
|
|
6
6
|
*/
|
|
7
7
|
export const commentHandler = {
|
|
8
|
-
ownLine: (comment
|
|
8
|
+
ownLine: (comment, text, options, ast, isLastComment) => [
|
|
9
|
+
addEmptyInterfaceComment,
|
|
10
|
+
addEmptyModelComment,
|
|
11
|
+
addStatementDecoratorComment,
|
|
12
|
+
handleOnlyComments,
|
|
13
|
+
].some((x) => x({ comment, text, options, ast: ast, isLastComment })),
|
|
9
14
|
};
|
|
10
15
|
/**
|
|
11
16
|
* When a comment is on an empty interface make sure it gets added as a dangling comment on it and not on the identifier.
|
|
@@ -16,7 +21,7 @@ export const commentHandler = {
|
|
|
16
21
|
* // My comment
|
|
17
22
|
* }
|
|
18
23
|
*/
|
|
19
|
-
function addEmptyInterfaceComment(comment) {
|
|
24
|
+
function addEmptyInterfaceComment({ comment }) {
|
|
20
25
|
const { precedingNode, enclosingNode } = comment;
|
|
21
26
|
if (enclosingNode &&
|
|
22
27
|
enclosingNode.kind === SyntaxKind.InterfaceStatement &&
|
|
@@ -39,7 +44,7 @@ function addEmptyInterfaceComment(comment) {
|
|
|
39
44
|
* model Foo {
|
|
40
45
|
* }
|
|
41
46
|
*/
|
|
42
|
-
function addStatementDecoratorComment(comment) {
|
|
47
|
+
function addStatementDecoratorComment({ comment }) {
|
|
43
48
|
const { enclosingNode, precedingNode } = comment;
|
|
44
49
|
if (precedingNode &&
|
|
45
50
|
precedingNode.kind === SyntaxKind.DecoratorExpression &&
|
|
@@ -48,6 +53,8 @@ function addStatementDecoratorComment(comment) {
|
|
|
48
53
|
enclosingNode.kind === SyntaxKind.ModelStatement ||
|
|
49
54
|
enclosingNode.kind === SyntaxKind.EnumStatement ||
|
|
50
55
|
enclosingNode.kind === SyntaxKind.OperationStatement ||
|
|
56
|
+
enclosingNode.kind === SyntaxKind.ScalarStatement ||
|
|
57
|
+
enclosingNode.kind === SyntaxKind.InterfaceStatement ||
|
|
51
58
|
enclosingNode.kind === SyntaxKind.ModelProperty ||
|
|
52
59
|
enclosingNode.kind === SyntaxKind.EnumMember ||
|
|
53
60
|
enclosingNode.kind === SyntaxKind.UnionStatement)) {
|
|
@@ -65,7 +72,7 @@ function addStatementDecoratorComment(comment) {
|
|
|
65
72
|
* // My comment
|
|
66
73
|
* }
|
|
67
74
|
*/
|
|
68
|
-
function addEmptyModelComment(comment) {
|
|
75
|
+
function addEmptyModelComment({ comment }) {
|
|
69
76
|
const { precedingNode, enclosingNode } = comment;
|
|
70
77
|
if (enclosingNode &&
|
|
71
78
|
enclosingNode.kind === SyntaxKind.ModelStatement &&
|
|
@@ -79,4 +86,29 @@ function addEmptyModelComment(comment) {
|
|
|
79
86
|
}
|
|
80
87
|
return false;
|
|
81
88
|
}
|
|
89
|
+
function handleOnlyComments({ comment, ast, isLastComment }) {
|
|
90
|
+
var _a, _b;
|
|
91
|
+
const { enclosingNode } = comment;
|
|
92
|
+
if (((_a = ast === null || ast === void 0 ? void 0 : ast.statements) === null || _a === void 0 ? void 0 : _a.length) === 0) {
|
|
93
|
+
if (isLastComment) {
|
|
94
|
+
util.addDanglingComment(ast, comment, undefined);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
util.addLeadingComment(ast, comment);
|
|
98
|
+
}
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
if ((enclosingNode === null || enclosingNode === void 0 ? void 0 : enclosingNode.kind) === SyntaxKind.TypeSpecScript &&
|
|
102
|
+
enclosingNode.statements.length === 0 &&
|
|
103
|
+
((_b = enclosingNode.directives) === null || _b === void 0 ? void 0 : _b.length) === 0) {
|
|
104
|
+
if (isLastComment) {
|
|
105
|
+
util.addDanglingComment(enclosingNode, comment, undefined);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
util.addLeadingComment(enclosingNode, comment);
|
|
109
|
+
}
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
82
114
|
//# sourceMappingURL=comment-handler.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"comment-handler.js","sourceRoot":"","sources":["../../../../src/formatter/print/comment-handler.ts"],"names":[],"mappings":"AAAA,OAAO,QAAqB,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAQ,UAAU,
|
|
1
|
+
{"version":3,"file":"comment-handler.js","sourceRoot":"","sources":["../../../../src/formatter/print/comment-handler.ts"],"names":[],"mappings":"AAAA,OAAO,QAAqB,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAQ,UAAU,EAAsB,MAAM,qBAAqB,CAAC;AAE3E,MAAM,EAAE,IAAI,EAAE,GAAG,QAAQ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,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;SACtC;QACD,OAAO,IAAI,CAAC;KACb;IAED,IACE,CAAA,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAE,IAAI,MAAK,UAAU,CAAC,cAAc;QACjD,aAAa,CAAC,UAAU,CAAC,MAAM,KAAK,CAAC;QACrC,CAAA,MAAA,aAAa,CAAC,UAAU,0CAAE,MAAM,MAAK,CAAC,EACtC;QACA,IAAI,aAAa,EAAE;YACjB,IAAI,CAAC,kBAAkB,CAAC,aAAa,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;SAC5D;aAAM;YACL,IAAI,CAAC,iBAAiB,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;SAChD;QACD,OAAO,IAAI,CAAC;KACb;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import prettier, { AstPath, Doc, Printer } from "prettier";
|
|
2
|
-
import { AliasStatementNode, ArrayExpressionNode, AugmentDecoratorStatementNode, Comment, DecoratorDeclarationStatementNode, DecoratorExpressionNode, DirectiveExpressionNode, EnumMemberNode, EnumStatementNode, FunctionDeclarationStatementNode, InterfaceStatementNode, IntersectionExpressionNode, MemberExpressionNode, ModelExpressionNode, ModelPropertyNode, ModelStatementNode, Node, OperationSignatureDeclarationNode, OperationSignatureReferenceNode, OperationStatementNode, ProjectionArithmeticExpressionNode, ProjectionBlockExpressionNode, ProjectionCallExpressionNode, ProjectionEqualityExpressionNode, ProjectionLambdaExpressionNode, ProjectionLambdaParameterDeclarationNode, ProjectionLogicalExpressionNode, ProjectionMemberExpressionNode, ProjectionModelExpressionNode, ProjectionModelPropertyNode, ProjectionRelationalExpressionNode, ProjectionTupleExpressionNode, ProjectionUnaryExpressionNode, ReturnExpressionNode, ScalarStatementNode, TupleExpressionNode, TypeReferenceNode, UnionExpressionNode, UnionStatementNode, UnionVariantNode, ValueOfExpressionNode } from "../../core/types.js";
|
|
2
|
+
import { AliasStatementNode, ArrayExpressionNode, AugmentDecoratorStatementNode, Comment, DecoratorDeclarationStatementNode, DecoratorExpressionNode, DirectiveExpressionNode, EnumMemberNode, EnumStatementNode, FunctionDeclarationStatementNode, InterfaceStatementNode, IntersectionExpressionNode, MemberExpressionNode, ModelExpressionNode, ModelPropertyNode, ModelStatementNode, Node, OperationSignatureDeclarationNode, OperationSignatureReferenceNode, OperationStatementNode, ProjectionArithmeticExpressionNode, ProjectionBlockExpressionNode, ProjectionCallExpressionNode, ProjectionEqualityExpressionNode, ProjectionLambdaExpressionNode, ProjectionLambdaParameterDeclarationNode, ProjectionLogicalExpressionNode, ProjectionMemberExpressionNode, ProjectionModelExpressionNode, ProjectionModelPropertyNode, ProjectionRelationalExpressionNode, ProjectionTupleExpressionNode, ProjectionUnaryExpressionNode, ReturnExpressionNode, ScalarStatementNode, TupleExpressionNode, TypeReferenceNode, TypeSpecScriptNode, UnionExpressionNode, UnionStatementNode, UnionVariantNode, ValueOfExpressionNode } from "../../core/types.js";
|
|
3
3
|
import { FlattenedNamespaceStatementNode } from "../types.js";
|
|
4
4
|
import { DecorableNode, PrettierChildPrint, TypeSpecPrettierOptions } from "./types.js";
|
|
5
5
|
export declare const typespecPrinter: Printer<Node>;
|
|
6
6
|
export declare function printTypeSpec(path: AstPath<Node>, options: TypeSpecPrettierOptions, print: PrettierChildPrint): prettier.Doc;
|
|
7
7
|
export declare function printNode(path: AstPath<Node>, options: TypeSpecPrettierOptions, print: PrettierChildPrint): prettier.Doc;
|
|
8
|
+
export declare function printTypeSpecScript(path: AstPath<TypeSpecScriptNode>, options: TypeSpecPrettierOptions, print: PrettierChildPrint): Doc;
|
|
8
9
|
export declare function printAliasStatement(path: AstPath<AliasStatementNode>, options: TypeSpecPrettierOptions, print: PrettierChildPrint): prettier.doc.builders.Doc[];
|
|
9
10
|
export declare function canAttachComment(node: Node): boolean;
|
|
10
11
|
export declare function printComment(commentPath: AstPath<Node | Comment>, options: TypeSpecPrettierOptions): Doc;
|
|
@@ -16,6 +17,7 @@ export declare function printDecorators(path: AstPath<DecorableNode>, options: o
|
|
|
16
17
|
};
|
|
17
18
|
export declare function printDecorator(path: AstPath<DecoratorExpressionNode>, options: TypeSpecPrettierOptions, print: PrettierChildPrint): prettier.doc.builders.Doc[];
|
|
18
19
|
export declare function printAugmentDecorator(path: AstPath<AugmentDecoratorStatementNode>, options: TypeSpecPrettierOptions, print: PrettierChildPrint): prettier.doc.builders.Doc[];
|
|
20
|
+
export declare function printDocComments(path: AstPath<Node>, options: object, print: PrettierChildPrint): prettier.doc.builders.Group | "";
|
|
19
21
|
export declare function printDirectives(path: AstPath<Node>, options: object, print: PrettierChildPrint): prettier.doc.builders.Group | "";
|
|
20
22
|
export declare function printDirective(path: AstPath<DirectiveExpressionNode>, options: TypeSpecPrettierOptions, print: PrettierChildPrint): prettier.doc.builders.Doc[];
|
|
21
23
|
export declare function printDirectiveArgs(path: AstPath<DirectiveExpressionNode>, options: object, print: PrettierChildPrint): prettier.doc.builders.Concat | "";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"printer.d.ts","sourceRoot":"","sources":["../../../../src/formatter/print/printer.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAI3D,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,6BAA6B,EAG7B,OAAO,EACP,iCAAiC,EACjC,uBAAuB,EACvB,uBAAuB,
|
|
1
|
+
{"version":3,"file":"printer.d.ts","sourceRoot":"","sources":["../../../../src/formatter/print/printer.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAI3D,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,6BAA6B,EAG7B,OAAO,EACP,iCAAiC,EACjC,uBAAuB,EACvB,uBAAuB,E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EAAE,uBAAuB,EAChC,KAAK,EAAE,kBAAkB,+BAI1B;AAED,wBAAgB,kCAAkC,CAChD,IAAI,EAAE,OAAO,CACT,+BAA+B,GAC/B,kCAAkC,GAClC,gCAAgC,GAChC,kCAAkC,CACrC,EACD,OAAO,EAAE,uBAAuB,EAChC,KAAK,EAAE,kBAAkB,+BAI1B;AAED,wBAAgB,8BAA8B,CAC5C,IAAI,EAAE,OAAO,CAAC,6BAA6B,CAAC,EAC5C,OAAO,EAAE,uBAAuB,EAChC,KAAK,EAAE,kBAAkB,+BAG1B;AAED,wBAAgB,6BAA6B,CAC3C,IAAI,EAAE,OAAO,CAAC,4BAA4B,CAAC,EAC3C,OAAO,EAAE,uBAAuB,EAChC,KAAK,EAAE,kBAAkB,+BAW1B;AAED,wBAAgB,+BAA+B,CAC7C,IAAI,EAAE,OAAO,CAAC,8BAA8B,CAAC,EAC7C,OAAO,EAAE,uBAAuB,EAChC,KAAK,EAAE,kBAAkB,+BAS1B;AAED,wBAAgB,yCAAyC,CACvD,IAAI,EAAE,OAAO,CAAC,wCAAwC,CAAC,EACvD,OAAO,EAAE,uBAAuB,EAChC,KAAK,EAAE,kBAAkB,6BAG1B;AAED,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,OAAO,CAAC,oBAAoB,CAAC,EACnC,OAAO,EAAE,uBAAuB,EAChC,KAAK,EAAE,kBAAkB,+BAG1B"}
|
|
@@ -7,6 +7,14 @@ import { commentHandler } from "./comment-handler.js";
|
|
|
7
7
|
import { needsParens } from "./needs-parens.js";
|
|
8
8
|
const { align, breakParent, group, hardline, ifBreak, indent, join, line, softline } = prettier.doc.builders;
|
|
9
9
|
const { isNextLineEmpty } = prettier.util;
|
|
10
|
+
/**
|
|
11
|
+
* If the decorators for that node should try to be kept inline.
|
|
12
|
+
*/
|
|
13
|
+
const DecoratorsTryInline = {
|
|
14
|
+
modelProperty: true,
|
|
15
|
+
enumMember: true,
|
|
16
|
+
unionVariant: true,
|
|
17
|
+
};
|
|
10
18
|
export const typespecPrinter = {
|
|
11
19
|
print: printTypeSpec,
|
|
12
20
|
canAttachComment: canAttachComment,
|
|
@@ -17,10 +25,11 @@ export function printTypeSpec(
|
|
|
17
25
|
// Path to the AST node to print
|
|
18
26
|
path, options, print) {
|
|
19
27
|
const node = path.getValue();
|
|
28
|
+
const docs = printDocComments(path, options, print);
|
|
20
29
|
const directives = shouldPrintDirective(node) ? printDirectives(path, options, print) : "";
|
|
21
30
|
const printedNode = printNode(path, options, print);
|
|
22
31
|
const value = needsParens(path, options) ? ["(", printedNode, ")"] : printedNode;
|
|
23
|
-
const parts = [directives, value];
|
|
32
|
+
const parts = [docs, directives, value];
|
|
24
33
|
if (node.kind === SyntaxKind.TypeSpecScript) {
|
|
25
34
|
// For TypeSpecScript(root of typespec document) we had a new line at the end.
|
|
26
35
|
// This must be done here so the hardline entry can be the last item of the doc array returned by the printer
|
|
@@ -37,13 +46,10 @@ export function printNode(
|
|
|
37
46
|
// Path to the AST node to print
|
|
38
47
|
path, options, print) {
|
|
39
48
|
const node = path.getValue();
|
|
40
|
-
printDirectives(path, options, print);
|
|
41
49
|
switch (node.kind) {
|
|
42
50
|
// Root
|
|
43
51
|
case SyntaxKind.TypeSpecScript:
|
|
44
|
-
return
|
|
45
|
-
printStatementSequence(path, options, print, "statements"),
|
|
46
|
-
];
|
|
52
|
+
return printTypeSpecScript(path, options, print);
|
|
47
53
|
// Statements
|
|
48
54
|
case SyntaxKind.ImportStatement:
|
|
49
55
|
return [`import "${node.path.value}";`];
|
|
@@ -182,6 +188,7 @@ path, options, print) {
|
|
|
182
188
|
case SyntaxKind.Return:
|
|
183
189
|
return printReturnExpression(path, options, print);
|
|
184
190
|
case SyntaxKind.Doc:
|
|
191
|
+
return printDoc(path, options, print);
|
|
185
192
|
case SyntaxKind.DocText:
|
|
186
193
|
case SyntaxKind.DocParamTag:
|
|
187
194
|
case SyntaxKind.DocTemplateTag:
|
|
@@ -190,8 +197,9 @@ path, options, print) {
|
|
|
190
197
|
// https://github.com/microsoft/typespec/issues/1319 Tracks pretty-printing doc comments.
|
|
191
198
|
compilerAssert(false, "Currently, doc comments are only handled as regular comments and we do not opt in to parsing them so we shouldn't reach here.");
|
|
192
199
|
return "";
|
|
193
|
-
case SyntaxKind.JsSourceFile:
|
|
194
200
|
case SyntaxKind.EmptyStatement:
|
|
201
|
+
return "";
|
|
202
|
+
case SyntaxKind.JsSourceFile:
|
|
195
203
|
case SyntaxKind.InvalidStatement:
|
|
196
204
|
return getRawText(node, options);
|
|
197
205
|
default:
|
|
@@ -202,6 +210,16 @@ path, options, print) {
|
|
|
202
210
|
return getRawText(node, options);
|
|
203
211
|
}
|
|
204
212
|
}
|
|
213
|
+
export function printTypeSpecScript(path, options, print) {
|
|
214
|
+
const node = path.getValue();
|
|
215
|
+
const nodeHasComments = hasComments(node, CommentCheckFlags.Dangling);
|
|
216
|
+
const body = [];
|
|
217
|
+
if (nodeHasComments) {
|
|
218
|
+
body.push(printDanglingComments(path, options, { sameIndent: true }));
|
|
219
|
+
}
|
|
220
|
+
body.push(printStatementSequence(path, options, print, "statements"));
|
|
221
|
+
return body;
|
|
222
|
+
}
|
|
205
223
|
export function printAliasStatement(path, options, print) {
|
|
206
224
|
const id = path.call(print, "id");
|
|
207
225
|
const template = printTemplateParameters(path, options, print, "templateParameters");
|
|
@@ -227,6 +245,13 @@ export function canAttachComment(node) {
|
|
|
227
245
|
return Boolean(kind &&
|
|
228
246
|
kind !== SyntaxKind.LineComment &&
|
|
229
247
|
kind !== SyntaxKind.BlockComment &&
|
|
248
|
+
kind !== SyntaxKind.EmptyStatement &&
|
|
249
|
+
kind !== SyntaxKind.Doc &&
|
|
250
|
+
kind !== SyntaxKind.DocParamTag &&
|
|
251
|
+
kind !== SyntaxKind.DocReturnsTag &&
|
|
252
|
+
kind !== SyntaxKind.DocTemplateTag &&
|
|
253
|
+
kind !== SyntaxKind.DocText &&
|
|
254
|
+
kind !== SyntaxKind.DocUnknownTag &&
|
|
230
255
|
!(node.flags & 8 /* NodeFlags.Synthetic */));
|
|
231
256
|
}
|
|
232
257
|
export function printComment(commentPath, options) {
|
|
@@ -244,11 +269,10 @@ export function printComment(commentPath, options) {
|
|
|
244
269
|
function printBlockComment(commentPath, options) {
|
|
245
270
|
const comment = commentPath.getValue();
|
|
246
271
|
const rawComment = options.originalText.slice(comment.pos + 2, comment.end - 2);
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
return ["/*", rawComment, "*/"];
|
|
272
|
+
const printed = isIndentableBlockComment(rawComment)
|
|
273
|
+
? printIndentableBlockCommentContent(rawComment)
|
|
274
|
+
: rawComment;
|
|
275
|
+
return ["/*", printed, "*/"];
|
|
252
276
|
}
|
|
253
277
|
function isIndentableBlockComment(rawComment) {
|
|
254
278
|
// If the comment has multiple lines and every line starts with a star
|
|
@@ -258,28 +282,42 @@ function isIndentableBlockComment(rawComment) {
|
|
|
258
282
|
const lines = `*${rawComment}*`.split("\n");
|
|
259
283
|
return lines.length > 1 && lines.every((line) => line.trim()[0] === "*");
|
|
260
284
|
}
|
|
261
|
-
function
|
|
285
|
+
function printIndentableBlockCommentContent(rawComment) {
|
|
262
286
|
const lines = rawComment.split("\n");
|
|
263
287
|
return [
|
|
264
|
-
"/*",
|
|
265
288
|
join(hardline, lines.map((line, index) => index === 0
|
|
266
289
|
? line.trimEnd()
|
|
267
290
|
: " " + (index < lines.length - 1 ? line.trim() : line.trimStart()))),
|
|
268
|
-
"*/",
|
|
269
291
|
];
|
|
270
292
|
}
|
|
293
|
+
/** Print a doc comment. */
|
|
294
|
+
function printDoc(path, options, print) {
|
|
295
|
+
const node = path.getValue();
|
|
296
|
+
const rawComment = options.originalText.slice(node.pos + 3, node.end - 2);
|
|
297
|
+
const printed = isIndentableBlockComment(rawComment)
|
|
298
|
+
? printIndentableBlockCommentContent(rawComment)
|
|
299
|
+
: rawComment.includes("\n")
|
|
300
|
+
? rawComment
|
|
301
|
+
: ` ${rawComment.trim()} `;
|
|
302
|
+
return ["/**", printed, "*/"];
|
|
303
|
+
}
|
|
271
304
|
export function printDecorators(path, options, print, { tryInline }) {
|
|
272
305
|
const node = path.getValue();
|
|
273
306
|
if (node.decorators.length === 0) {
|
|
274
307
|
return { decorators: "", multiline: false };
|
|
275
308
|
}
|
|
276
|
-
const shouldBreak =
|
|
309
|
+
const shouldBreak = shouldDecoratorBreakLine(path, options, { tryInline });
|
|
277
310
|
const decorators = path.map((x) => [print(x), ifBreak(line, " ")], "decorators");
|
|
278
311
|
return {
|
|
279
312
|
decorators: group([shouldBreak ? breakParent : "", decorators]),
|
|
280
313
|
multiline: shouldBreak,
|
|
281
314
|
};
|
|
282
315
|
}
|
|
316
|
+
/** Check if the decorators of the given node should be broken in sparate line */
|
|
317
|
+
function shouldDecoratorBreakLine(path, options, { tryInline }) {
|
|
318
|
+
const node = path.getValue();
|
|
319
|
+
return (!tryInline || node.decorators.length >= 3 || hasNewlineBetweenOrAfterDecorators(node, options));
|
|
320
|
+
}
|
|
283
321
|
/**
|
|
284
322
|
* Check if there is already new lines in between the decorators of the node.
|
|
285
323
|
*/
|
|
@@ -307,6 +345,14 @@ function printAugmentDecoratorArgs(path, options, print) {
|
|
|
307
345
|
")",
|
|
308
346
|
];
|
|
309
347
|
}
|
|
348
|
+
export function printDocComments(path, options, print) {
|
|
349
|
+
const node = path.getValue();
|
|
350
|
+
if (node.docs === undefined || node.docs.length === 0) {
|
|
351
|
+
return "";
|
|
352
|
+
}
|
|
353
|
+
const docs = path.map((x) => [print(x), line], "docs");
|
|
354
|
+
return group([...docs, breakParent]);
|
|
355
|
+
}
|
|
310
356
|
export function printDirectives(path, options, print) {
|
|
311
357
|
const node = path.getValue();
|
|
312
358
|
if (node.directives === undefined || node.directives.length === 0) {
|
|
@@ -378,7 +424,9 @@ export function printEnumMember(path, options, print) {
|
|
|
378
424
|
const node = path.getValue();
|
|
379
425
|
const id = path.call(print, "id");
|
|
380
426
|
const value = node.value ? [": ", path.call(print, "value")] : "";
|
|
381
|
-
const { decorators, multiline } = printDecorators(path, options, print, {
|
|
427
|
+
const { decorators, multiline } = printDecorators(path, options, print, {
|
|
428
|
+
tryInline: DecoratorsTryInline.enumMember,
|
|
429
|
+
});
|
|
382
430
|
const propertyIndex = path.stack[path.stack.length - 2];
|
|
383
431
|
const isNotFirst = typeof propertyIndex === "number" && propertyIndex > 0;
|
|
384
432
|
return [multiline && isNotFirst ? hardline : "", decorators, id, value];
|
|
@@ -410,7 +458,9 @@ export function printUnionVariantsBlock(path, options, print) {
|
|
|
410
458
|
export function printUnionVariant(path, options, print) {
|
|
411
459
|
const id = path.call(print, "id");
|
|
412
460
|
const value = [": ", path.call(print, "value")];
|
|
413
|
-
const { decorators } = printDecorators(path, options, print, {
|
|
461
|
+
const { decorators } = printDecorators(path, options, print, {
|
|
462
|
+
tryInline: DecoratorsTryInline.unionVariant,
|
|
463
|
+
});
|
|
414
464
|
return [decorators, id, value];
|
|
415
465
|
}
|
|
416
466
|
export function printInterfaceStatement(path, options, print) {
|
|
@@ -534,18 +584,19 @@ export function printTuple(path, options, print) {
|
|
|
534
584
|
}
|
|
535
585
|
export function printMemberExpression(path, options, print) {
|
|
536
586
|
const node = path.getValue();
|
|
537
|
-
return [node.base ? [path.call(print, "base"),
|
|
587
|
+
return [node.base ? [path.call(print, "base"), node.selector] : "", path.call(print, "id")];
|
|
538
588
|
}
|
|
539
589
|
export function printModelExpression(path, options, print) {
|
|
540
590
|
const inBlock = isModelExpressionInBlock(path);
|
|
591
|
+
const node = path.getValue();
|
|
541
592
|
if (inBlock) {
|
|
542
593
|
return group(printModelPropertiesBlock(path, options, print));
|
|
543
594
|
}
|
|
544
595
|
else {
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
softline
|
|
548
|
-
]);
|
|
596
|
+
const properties = node.properties.length === 0
|
|
597
|
+
? ""
|
|
598
|
+
: indent(joinPropertiesInBlock(path, options, print, ifBreak(",", ", "), softline));
|
|
599
|
+
return group([properties, softline]);
|
|
549
600
|
}
|
|
550
601
|
}
|
|
551
602
|
export function printModelStatement(path, options, print) {
|
|
@@ -579,16 +630,54 @@ function printModelPropertiesBlock(path, options, print) {
|
|
|
579
630
|
const tryInline = ((_a = path.getParentNode()) === null || _a === void 0 ? void 0 : _a.kind) === SyntaxKind.TemplateParameterDeclaration;
|
|
580
631
|
const lineDoc = tryInline ? softline : hardline;
|
|
581
632
|
const seperator = isModelAValue(path) ? "," : ";";
|
|
582
|
-
const body = [
|
|
583
|
-
lineDoc,
|
|
584
|
-
join([seperator, lineDoc], path.map((x) => [print(x)], "properties")),
|
|
585
|
-
hasProperties ? ifBreak(seperator) : "",
|
|
586
|
-
];
|
|
633
|
+
const body = [joinPropertiesInBlock(path, options, print, seperator, lineDoc)];
|
|
587
634
|
if (nodeHasComments) {
|
|
588
635
|
body.push(printDanglingComments(path, options, { sameIndent: true }));
|
|
589
636
|
}
|
|
590
637
|
return group(["{", indent(body), lineDoc, "}"]);
|
|
591
638
|
}
|
|
639
|
+
function joinPropertiesInBlock(path, options, print, separator, regularLine) {
|
|
640
|
+
const doc = [regularLine];
|
|
641
|
+
const propertyContainerNode = path.getValue();
|
|
642
|
+
let newLineBeforeNextProp = false;
|
|
643
|
+
path.each((item, propertyIndex) => {
|
|
644
|
+
const isFirst = propertyIndex === 0;
|
|
645
|
+
const isLast = propertyIndex === propertyContainerNode.properties.length - 1;
|
|
646
|
+
const shouldWrapInNewLines = shouldWrapPropertyInNewLines(item, options);
|
|
647
|
+
if ((newLineBeforeNextProp || shouldWrapInNewLines) && !isFirst) {
|
|
648
|
+
doc.push(hardline);
|
|
649
|
+
newLineBeforeNextProp = false;
|
|
650
|
+
}
|
|
651
|
+
doc.push(print(item));
|
|
652
|
+
if (isLast) {
|
|
653
|
+
doc.push(ifBreak(separator));
|
|
654
|
+
}
|
|
655
|
+
else {
|
|
656
|
+
doc.push(separator);
|
|
657
|
+
doc.push(regularLine);
|
|
658
|
+
if (shouldWrapInNewLines) {
|
|
659
|
+
newLineBeforeNextProp = true;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
}, "properties");
|
|
663
|
+
return doc;
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Check if property item (PropertyNode, SpreadProperty) should be wrapped in new lines.
|
|
667
|
+
* It can be wrapped for the following reasons:
|
|
668
|
+
* - has decorators on lines above
|
|
669
|
+
* - has leading comments
|
|
670
|
+
*/
|
|
671
|
+
function shouldWrapPropertyInNewLines(path, options) {
|
|
672
|
+
var _a;
|
|
673
|
+
const node = path.getValue();
|
|
674
|
+
return (((node.kind === SyntaxKind.ModelProperty || node.kind === SyntaxKind.ProjectionModelProperty) &&
|
|
675
|
+
shouldDecoratorBreakLine(path, options, {
|
|
676
|
+
tryInline: DecoratorsTryInline.modelProperty,
|
|
677
|
+
})) ||
|
|
678
|
+
hasComments(node, CommentCheckFlags.Leading) ||
|
|
679
|
+
(node.docs && ((_a = node.docs) === null || _a === void 0 ? void 0 : _a.length) > 0));
|
|
680
|
+
}
|
|
592
681
|
/**
|
|
593
682
|
* Figure out if this model is being used as a definition or value.
|
|
594
683
|
* @returns true if the model is used as a value(e.g. decorator value), false if it is used as a model definition.
|
|
@@ -610,14 +699,11 @@ function isModelAValue(path) {
|
|
|
610
699
|
}
|
|
611
700
|
export function printModelProperty(path, options, print) {
|
|
612
701
|
const node = path.getValue();
|
|
613
|
-
const
|
|
614
|
-
|
|
615
|
-
const { decorators, multiline } = printDecorators(path, options, print, {
|
|
616
|
-
tryInline: true,
|
|
702
|
+
const { decorators } = printDecorators(path, options, print, {
|
|
703
|
+
tryInline: DecoratorsTryInline.modelProperty,
|
|
617
704
|
});
|
|
618
705
|
const id = printIdentifier(node.id, options);
|
|
619
706
|
return [
|
|
620
|
-
multiline && isNotFirst ? hardline : "",
|
|
621
707
|
printDirectives(path, options, print),
|
|
622
708
|
decorators,
|
|
623
709
|
id,
|