diff --git a/packages/babel-generator/src/printer.ts b/packages/babel-generator/src/printer.ts index 39a6981babfd..e7d719727752 100644 --- a/packages/babel-generator/src/printer.ts +++ b/packages/babel-generator/src/printer.ts @@ -3,7 +3,6 @@ import type { Loc } from "./buffer"; import * as n from "./node"; import type * as t from "@babel/types"; import { - isProperty, isFunction, isStatement, isClassBody, @@ -1018,20 +1017,19 @@ class Printer { hasLoc = false; if (len === 1) { + const shouldSkipNewline = + !isStatement(node) && + !isClassBody(parent) && + !isTSInterfaceBody(parent); + if (type === COMMENT_TYPE.LEADING) { this._printComment( comment, - (!isStatement(node) && !isProperty(node)) || - isFunction(parent, { body: node }) + shouldSkipNewline || isFunction(parent, { body: node }) ? COMMENT_SKIP_NEWLINE.SKIP_ALL : COMMENT_SKIP_NEWLINE.DEFAULT, ); - } else if ( - type === COMMENT_TYPE.TRAILING && - !isStatement(node) && - !isClassBody(parent) && - !isTSInterfaceBody(parent) - ) { + } else if (type === COMMENT_TYPE.TRAILING && shouldSkipNewline) { this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL); } else { this._printComment(comment, COMMENT_SKIP_NEWLINE.DEFAULT); diff --git a/packages/babel-generator/test/index.js b/packages/babel-generator/test/index.js index 3e66ff8ad3e1..db317db3f56f 100644 --- a/packages/babel-generator/test/index.js +++ b/packages/babel-generator/test/index.js @@ -484,995 +484,74 @@ describe("generation", function () { expect(generate(ast).code).toBe("/*#__PURE__*/\n/*#__PURE__*/"); }); - it("leading comments without loc", () => { - const ast = { - type: "File", - errors: [], - program: { - type: "Program", - sourceType: "module", - interpreter: null, - body: [ - { - type: "ImportDeclaration", - importKind: "value", - specifiers: [ - { - type: "ImportSpecifier", - imported: { - type: "Identifier", - name: "Attribute", - }, - importKind: "value", - local: { - type: "Identifier", - name: "Attribute", - }, - }, - { - type: "ImportSpecifier", - imported: { - type: "Identifier", - name: "AttributeSDKType", - }, - importKind: "value", - local: { - type: "Identifier", - name: "AttributeSDKType", - }, - }, - ], - source: { - type: "StringLiteral", - extra: { - rawValue: "../../base/v1beta1/attribute", - raw: '"../../base/v1beta1/attribute"', - }, - value: "../../base/v1beta1/attribute", - }, - }, - { - type: "ImportDeclaration", - importKind: "value", - specifiers: [ - { - type: "ImportSpecifier", - imported: { - type: "Identifier", - name: "Rpc", - }, - importKind: "value", - local: { - type: "Identifier", - name: "Rpc", - }, - }, - ], - source: { - type: "StringLiteral", - extra: { - rawValue: "../../../helpers", - raw: '"../../../helpers"', - }, - value: "../../../helpers", - }, - }, - { - type: "ImportDeclaration", - importKind: "value", - specifiers: [ - { - type: "ImportNamespaceSpecifier", - local: { - type: "Identifier", - name: "_m0", - }, - }, - ], - source: { - type: "StringLiteral", - extra: { - rawValue: "protobufjs/minimal", - raw: '"protobufjs/minimal"', - }, - value: "protobufjs/minimal", - }, - }, - { - type: "ImportDeclaration", - importKind: "value", - specifiers: [ - { - type: "ImportSpecifier", - imported: { - type: "Identifier", - name: "MsgSignProviderAttributes", - }, - importKind: "value", - local: { - type: "Identifier", - name: "MsgSignProviderAttributes", - }, - }, - { - type: "ImportSpecifier", - imported: { - type: "Identifier", - name: "MsgSignProviderAttributesSDKType", - }, - importKind: "value", - local: { - type: "Identifier", - name: "MsgSignProviderAttributesSDKType", - }, - }, - { - type: "ImportSpecifier", - imported: { - type: "Identifier", - name: "MsgSignProviderAttributesResponse", - }, - importKind: "value", - local: { - type: "Identifier", - name: "MsgSignProviderAttributesResponse", - }, - }, - { - type: "ImportSpecifier", - imported: { - type: "Identifier", - name: "MsgSignProviderAttributesResponseSDKType", - }, - importKind: "value", - local: { - type: "Identifier", - name: "MsgSignProviderAttributesResponseSDKType", - }, - }, - { - type: "ImportSpecifier", - imported: { - type: "Identifier", - name: "MsgDeleteProviderAttributes", - }, - importKind: "value", - local: { - type: "Identifier", - name: "MsgDeleteProviderAttributes", - }, - }, - { - type: "ImportSpecifier", - imported: { - type: "Identifier", - name: "MsgDeleteProviderAttributesSDKType", - }, - importKind: "value", - local: { - type: "Identifier", - name: "MsgDeleteProviderAttributesSDKType", - }, - }, - { - type: "ImportSpecifier", - imported: { - type: "Identifier", - name: "MsgDeleteProviderAttributesResponse", - }, - importKind: "value", - local: { - type: "Identifier", - name: "MsgDeleteProviderAttributesResponse", - }, - }, - { - type: "ImportSpecifier", - imported: { - type: "Identifier", - name: "MsgDeleteProviderAttributesResponseSDKType", - }, - importKind: "value", - local: { - type: "Identifier", - name: "MsgDeleteProviderAttributesResponseSDKType", - }, - }, - ], - source: { - type: "StringLiteral", - extra: { - rawValue: "./audit", - raw: '"./audit"', - }, - value: "./audit", - }, - trailingComments: [ - { - type: "CommentBlock", - value: "* Msg defines the provider Msg service ", - }, - ], - }, - { - type: "ExportNamedDeclaration", - exportKind: "type", - specifiers: [], - source: null, - declaration: { - type: "TSInterfaceDeclaration", - id: { - type: "Identifier", - name: "Msg", - }, - body: { - type: "TSInterfaceBody", - body: [ - { - type: "TSMethodSignature", - key: { - type: "Identifier", - name: "signProviderAttributes", - }, - computed: false, - parameters: [ - { - type: "Identifier", - name: "request", - typeAnnotation: { - type: "TSTypeAnnotation", - typeAnnotation: { - type: "TSTypeReference", - typeName: { - type: "Identifier", - name: "MsgSignProviderAttributes", - }, - }, - }, - }, - ], - typeAnnotation: { - type: "TSTypeAnnotation", - typeAnnotation: { - type: "TSTypeReference", - typeName: { - type: "Identifier", - name: "Promise", - }, - typeParameters: { - type: "TSTypeParameterInstantiation", - params: [ - { - type: "TSTypeReference", - typeName: { - type: "Identifier", - name: "MsgSignProviderAttributesResponse", - }, - }, - ], - }, - }, - }, - kind: "method", - trailingComments: [ - { - type: "CommentBlock", - value: - "* DeleteProviderAttributes defines a method that deletes provider attributes ", - }, - ], - leadingComments: [ - { - type: "CommentBlock", - value: - "* SignProviderAttributes defines a method that signs provider attributes ", - }, - ], - }, - { - type: "TSMethodSignature", - key: { - type: "Identifier", - name: "deleteProviderAttributes", - }, - computed: false, - parameters: [ - { - type: "Identifier", - name: "request", - typeAnnotation: { - type: "TSTypeAnnotation", - typeAnnotation: { - type: "TSTypeReference", - typeName: { - type: "Identifier", - name: "MsgDeleteProviderAttributes", - }, - }, - }, - }, - ], - typeAnnotation: { - type: "TSTypeAnnotation", - typeAnnotation: { - type: "TSTypeReference", - typeName: { - type: "Identifier", - name: "Promise", - }, - typeParameters: { - type: "TSTypeParameterInstantiation", - params: [ - { - type: "TSTypeReference", - typeName: { - type: "Identifier", - name: "MsgDeleteProviderAttributesResponse", - }, - }, - ], - }, - }, - }, - kind: "method", - leadingComments: [ - { - type: "CommentBlock", - value: - "* DeleteProviderAttributes defines a method that deletes provider attributes ", - }, - ], - }, - ], - }, - }, - leadingComments: [ - { - type: "CommentBlock", - value: "* Msg defines the provider Msg service ", - }, - ], - }, - { - type: "ExportNamedDeclaration", - exportKind: "value", - specifiers: [], - source: null, - declaration: { - type: "ClassDeclaration", - id: { - type: "Identifier", - name: "MsgClientImpl", - }, - superClass: null, - implements: [ - { - type: "TSExpressionWithTypeArguments", - expression: { - type: "Identifier", - name: "Msg", - }, - }, - ], - body: { - type: "ClassBody", - body: [ - { - type: "ClassProperty", - accessibility: "private", - readonly: true, - static: false, - key: { - type: "Identifier", - name: "rpc", - }, - computed: false, - typeAnnotation: { - type: "TSTypeAnnotation", - typeAnnotation: { - type: "TSTypeReference", - typeName: { - type: "Identifier", - name: "Rpc", - }, - }, - }, - value: null, - }, - { - type: "ClassMethod", - static: false, - key: { - type: "Identifier", - name: "constructor", - }, - computed: false, - kind: "constructor", - id: null, - generator: false, - async: false, - params: [ - { - type: "Identifier", - name: "rpc", - typeAnnotation: { - type: "TSTypeAnnotation", - typeAnnotation: { - type: "TSTypeReference", - typeName: { - type: "Identifier", - name: "Rpc", - }, - }, - }, - }, - ], - body: { - type: "BlockStatement", - body: [ - { - type: "ExpressionStatement", - expression: { - type: "AssignmentExpression", - operator: "=", - left: { - type: "MemberExpression", - object: { - type: "ThisExpression", - }, - computed: false, - property: { - type: "Identifier", - name: "rpc", - }, - }, - right: { - type: "Identifier", - name: "rpc", - }, - }, - }, - ], - directives: [], - }, - trailingComments: [ - { - type: "CommentBlock", - value: - " SignProviderAttributes defines a method that signs provider attributes ", - }, - ], - }, - { - type: "ClassProperty", - static: false, - key: { - type: "Identifier", - name: "signProviderAttributes", - }, - computed: false, - value: { - type: "ArrowFunctionExpression", - returnType: { - type: "TSTypeAnnotation", - typeAnnotation: { - type: "TSTypeReference", - typeName: { - type: "Identifier", - name: "Promise", - }, - typeParameters: { - type: "TSTypeParameterInstantiation", - params: [ - { - type: "TSTypeReference", - typeName: { - type: "Identifier", - name: "MsgSignProviderAttributesResponse", - }, - }, - ], - }, - }, - }, - id: null, - generator: false, - async: true, - params: [ - { - type: "Identifier", - name: "request", - typeAnnotation: { - type: "TSTypeAnnotation", - typeAnnotation: { - type: "TSTypeReference", - typeName: { - type: "Identifier", - name: "MsgSignProviderAttributes", - }, - }, - }, - }, - ], - body: { - type: "BlockStatement", - body: [ - { - type: "VariableDeclaration", - declarations: [ - { - type: "VariableDeclarator", - id: { - type: "Identifier", - name: "data", - }, - init: { - type: "CallExpression", - callee: { - type: "MemberExpression", - object: { - type: "CallExpression", - callee: { - type: "MemberExpression", - object: { - type: "Identifier", - name: "MsgSignProviderAttributes", - }, - computed: false, - property: { - type: "Identifier", - name: "encode", - }, - }, - arguments: [ - { - type: "Identifier", - name: "request", - }, - ], - }, - computed: false, - property: { - type: "Identifier", - name: "finish", - }, - }, - arguments: [], - }, - }, - ], - kind: "const", - }, - { - type: "VariableDeclaration", - declarations: [ - { - type: "VariableDeclarator", - id: { - type: "Identifier", - name: "promise", - }, - init: { - type: "CallExpression", - callee: { - type: "MemberExpression", - object: { - type: "MemberExpression", - object: { - type: "ThisExpression", - }, - computed: false, - property: { - type: "Identifier", - name: "rpc", - }, - }, - computed: false, - property: { - type: "Identifier", - name: "request", - }, - }, - arguments: [ - { - type: "StringLiteral", - extra: { - rawValue: "akash.audit.v1beta1.Msg", - raw: '"akash.audit.v1beta1.Msg"', - }, - value: "akash.audit.v1beta1.Msg", - }, - { - type: "StringLiteral", - extra: { - rawValue: "SignProviderAttributes", - raw: '"SignProviderAttributes"', - }, - value: "SignProviderAttributes", - }, - { - type: "Identifier", - name: "data", - }, - ], - }, - }, - ], - kind: "const", - }, - { - type: "ReturnStatement", - argument: { - type: "CallExpression", - callee: { - type: "MemberExpression", - object: { - type: "Identifier", - name: "promise", - }, - computed: false, - property: { - type: "Identifier", - name: "then", - }, - }, - arguments: [ - { - type: "ArrowFunctionExpression", - id: null, - generator: false, - async: false, - params: [ - { - type: "Identifier", - name: "data", - }, - ], - body: { - type: "CallExpression", - callee: { - type: "MemberExpression", - object: { - type: "Identifier", - name: "MsgSignProviderAttributesResponse", - }, - computed: false, - property: { - type: "Identifier", - name: "decode", - }, - }, - arguments: [ - { - type: "NewExpression", - callee: { - type: "MemberExpression", - object: { - type: "Identifier", - name: "_m0", - }, - computed: false, - property: { - type: "Identifier", - name: "Reader", - }, - }, - arguments: [ - { - type: "Identifier", - name: "data", - }, - ], - }, - ], - }, - }, - ], - }, - }, - ], - directives: [], - }, - }, - trailingComments: [ - { - type: "CommentBlock", - value: - " DeleteProviderAttributes defines a method that deletes provider attributes ", - }, - ], - leadingComments: [ - { - type: "CommentBlock", - value: - " SignProviderAttributes defines a method that signs provider attributes ", - }, - ], - }, - { - type: "ClassProperty", - static: false, - key: { - type: "Identifier", - name: "deleteProviderAttributes", - }, - computed: false, - value: { - type: "ArrowFunctionExpression", - returnType: { - type: "TSTypeAnnotation", - typeAnnotation: { - type: "TSTypeReference", - typeName: { - type: "Identifier", - name: "Promise", - }, - typeParameters: { - type: "TSTypeParameterInstantiation", - params: [ - { - type: "TSTypeReference", - typeName: { - type: "Identifier", - name: "MsgDeleteProviderAttributesResponse", - }, - }, - ], - }, - }, - }, - id: null, - generator: false, - async: true, - params: [ - { - type: "Identifier", - name: "request", - typeAnnotation: { - type: "TSTypeAnnotation", - typeAnnotation: { - type: "TSTypeReference", - typeName: { - type: "Identifier", - name: "MsgDeleteProviderAttributes", - }, - }, - }, - }, - ], - body: { - type: "BlockStatement", - body: [ - { - type: "VariableDeclaration", - declarations: [ - { - type: "VariableDeclarator", - id: { - type: "Identifier", - name: "data", - }, - init: { - type: "CallExpression", - callee: { - type: "MemberExpression", - object: { - type: "CallExpression", - callee: { - type: "MemberExpression", - object: { - type: "Identifier", - name: "MsgDeleteProviderAttributes", - }, - computed: false, - property: { - type: "Identifier", - name: "encode", - }, - }, - arguments: [ - { - type: "Identifier", - name: "request", - }, - ], - }, - computed: false, - property: { - type: "Identifier", - name: "finish", - }, - }, - arguments: [], - }, - }, - ], - kind: "const", - }, - { - type: "VariableDeclaration", - declarations: [ - { - type: "VariableDeclarator", - id: { - type: "Identifier", - name: "promise", - }, - init: { - type: "CallExpression", - callee: { - type: "MemberExpression", - object: { - type: "MemberExpression", - object: { - type: "ThisExpression", - }, - computed: false, - property: { - type: "Identifier", - name: "rpc", - }, - }, - computed: false, - property: { - type: "Identifier", - name: "request", - }, - }, - arguments: [ - { - type: "StringLiteral", - extra: { - rawValue: "akash.audit.v1beta1.Msg", - raw: '"akash.audit.v1beta1.Msg"', - }, - value: "akash.audit.v1beta1.Msg", - }, - { - type: "StringLiteral", - extra: { - rawValue: "DeleteProviderAttributes", - raw: '"DeleteProviderAttributes"', - }, - value: "DeleteProviderAttributes", - }, - { - type: "Identifier", - name: "data", - }, - ], - }, - }, - ], - kind: "const", - }, - { - type: "ReturnStatement", - argument: { - type: "CallExpression", - callee: { - type: "MemberExpression", - object: { - type: "Identifier", - name: "promise", - }, - computed: false, - property: { - type: "Identifier", - name: "then", - }, - }, - arguments: [ - { - type: "ArrowFunctionExpression", - id: null, - generator: false, - async: false, - params: [ - { - type: "Identifier", - name: "data", - }, - ], - body: { - type: "CallExpression", - callee: { - type: "MemberExpression", - object: { - type: "Identifier", - name: "MsgDeleteProviderAttributesResponse", - }, - computed: false, - property: { - type: "Identifier", - name: "decode", - }, - }, - arguments: [ - { - type: "NewExpression", - callee: { - type: "MemberExpression", - object: { - type: "Identifier", - name: "_m0", - }, - computed: false, - property: { - type: "Identifier", - name: "Reader", - }, - }, - arguments: [ - { - type: "Identifier", - name: "data", - }, - ], - }, - ], - }, - }, - ], - }, - }, - ], - directives: [], - }, - }, - leadingComments: [ - { - type: "CommentBlock", - value: - " DeleteProviderAttributes defines a method that deletes provider attributes ", - }, - ], - }, - ], - }, - }, - }, - ], - directives: [], - }, - comments: [ - { - type: "CommentBlock", - value: "* Msg defines the provider Msg service ", - }, - { - type: "CommentBlock", - value: - "* SignProviderAttributes defines a method that signs provider attributes ", - }, - { - type: "CommentBlock", - value: - "* DeleteProviderAttributes defines a method that deletes provider attributes ", - }, - { - type: "CommentBlock", - value: - " SignProviderAttributes defines a method that signs provider attributes ", - }, - { - type: "CommentBlock", - value: - " DeleteProviderAttributes defines a method that deletes provider attributes ", - }, - ], - }; + it("comments without loc", () => { + const ast = parse( + ` + import { + Attribute, + AttributeSDKType + } + from "../../base/v1beta1/attribute"; + import { + Rpc + } + from "../../../helpers"; + import * as _m0 from "protobufjs/minimal"; + import { + MsgSignProviderAttributes, + MsgSignProviderAttributesSDKType, + MsgSignProviderAttributesResponse, + MsgSignProviderAttributesResponseSDKType, + MsgDeleteProviderAttributes, + MsgDeleteProviderAttributesSDKType, + MsgDeleteProviderAttributesResponse, + MsgDeleteProviderAttributesResponseSDKType + } + from "./audit"; + /** Msg defines the provider Msg service */ + export interface Msg { + /** SignProviderAttributes defines a method that signs provider attributes */ + signProviderAttributes(request: MsgSignProviderAttributes): Promise < MsgSignProviderAttributesResponse > ; + /** DeleteProviderAttributes defines a method that deletes provider attributes */ + deleteProviderAttributes(request: MsgDeleteProviderAttributes): Promise < MsgDeleteProviderAttributesResponse > ; + } + export class MsgClientImpl implements Msg { + private readonly rpc: Rpc; + constructor(rpc: Rpc) { + this.rpc = rpc; + } + /* SignProviderAttributes defines a method that signs provider attributes */ + signProviderAttributes = async(request: MsgSignProviderAttributes): Promise < MsgSignProviderAttributesResponse > => { + const data = MsgSignProviderAttributes.encode(request).finish(); + const promise = this.rpc.request("akash.audit.v1beta1.Msg", "SignProviderAttributes", data); + return promise.then(data => MsgSignProviderAttributesResponse.decode(new _m0.Reader(data))); + }; + /* DeleteProviderAttributes defines a method that deletes provider attributes */ + deleteProviderAttributes = async(request: MsgDeleteProviderAttributes): Promise < MsgDeleteProviderAttributesResponse > => { + const data = MsgDeleteProviderAttributes.encode(request).finish(); + const promise = this.rpc.request("akash.audit.v1beta1.Msg", "DeleteProviderAttributes", data); + return promise.then(data => MsgDeleteProviderAttributesResponse.decode(new _m0.Reader(data))); + }; + } + `, + { sourceType: "module", plugins: ["typescript"] }, + ); + + for (const comment of ast.comments) { + comment.loc = undefined; + } + expect(generate(ast).code).toMatchInlineSnapshot(` "import { Attribute, AttributeSDKType } from \\"../../base/v1beta1/attribute\\"; import { Rpc } from \\"../../../helpers\\"; import * as _m0 from \\"protobufjs/minimal\\"; import { MsgSignProviderAttributes, MsgSignProviderAttributesSDKType, MsgSignProviderAttributesResponse, MsgSignProviderAttributesResponseSDKType, MsgDeleteProviderAttributes, MsgDeleteProviderAttributesSDKType, MsgDeleteProviderAttributesResponse, MsgDeleteProviderAttributesResponseSDKType } from \\"./audit\\"; /** Msg defines the provider Msg service */ - /** Msg defines the provider Msg service */ export interface Msg { - /** SignProviderAttributes defines a method that signs provider attributes */signProviderAttributes(request: MsgSignProviderAttributes): Promise; + /** SignProviderAttributes defines a method that signs provider attributes */ + signProviderAttributes(request: MsgSignProviderAttributes): Promise; /** DeleteProviderAttributes defines a method that deletes provider attributes */ - /** DeleteProviderAttributes defines a method that deletes provider attributes */deleteProviderAttributes(request: MsgDeleteProviderAttributes): Promise; + deleteProviderAttributes(request: MsgDeleteProviderAttributes): Promise; } export class MsgClientImpl implements Msg { private readonly rpc: Rpc; @@ -1480,14 +559,12 @@ describe("generation", function () { this.rpc = rpc; } /* SignProviderAttributes defines a method that signs provider attributes */ - /* SignProviderAttributes defines a method that signs provider attributes */ signProviderAttributes = async (request: MsgSignProviderAttributes): Promise => { const data = MsgSignProviderAttributes.encode(request).finish(); const promise = this.rpc.request(\\"akash.audit.v1beta1.Msg\\", \\"SignProviderAttributes\\", data); return promise.then(data => MsgSignProviderAttributesResponse.decode(new _m0.Reader(data))); }; /* DeleteProviderAttributes defines a method that deletes provider attributes */ - /* DeleteProviderAttributes defines a method that deletes provider attributes */ deleteProviderAttributes = async (request: MsgDeleteProviderAttributes): Promise => { const data = MsgDeleteProviderAttributes.encode(request).finish(); const promise = this.rpc.request(\\"akash.audit.v1beta1.Msg\\", \\"DeleteProviderAttributes\\", data);