Formatter: Multiline string (#555)
This commit is contained in:
Родитель
bb82633b6c
Коммит
469b1457f4
|
@ -14,9 +14,12 @@ import {
|
|||
ModelStatementNode,
|
||||
NamespaceStatementNode,
|
||||
Node,
|
||||
NumericLiteralNode,
|
||||
OperationStatementNode,
|
||||
Statement,
|
||||
StringLiteralNode,
|
||||
SyntaxKind,
|
||||
TextRange,
|
||||
TypeReferenceNode,
|
||||
UnionExpressionNode,
|
||||
} from "../../compiler/types.js";
|
||||
|
@ -77,9 +80,9 @@ export function printADL(
|
|||
case SyntaxKind.Identifier:
|
||||
return node.sv;
|
||||
case SyntaxKind.StringLiteral:
|
||||
return `"${node.value}"`;
|
||||
return printStringLiteral(path as FastPath<StringLiteralNode>, options);
|
||||
case SyntaxKind.NumericLiteral:
|
||||
return `${node.value}`;
|
||||
return printNumberLiteral(path as FastPath<NumericLiteralNode>, options);
|
||||
case SyntaxKind.ModelExpression:
|
||||
return printModelExpression(path as FastPath<ModelExpressionNode>, options, print);
|
||||
case SyntaxKind.ModelProperty:
|
||||
|
@ -95,7 +98,7 @@ export function printADL(
|
|||
case SyntaxKind.TypeReference:
|
||||
return printTypeReference(path as FastPath<TypeReferenceNode>, options, print);
|
||||
default:
|
||||
return options.originalText.slice(node.pos, node.end);
|
||||
return getRawText(node, options);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -607,3 +610,28 @@ export function printTypeReference(
|
|||
const template = printTemplateParameters(path, options, print, "arguments");
|
||||
return concat([type, template]);
|
||||
}
|
||||
|
||||
export function printStringLiteral(
|
||||
path: prettier.FastPath<StringLiteralNode>,
|
||||
options: ADLPrettierOptions
|
||||
): prettier.doc.builders.Doc {
|
||||
const node = path.getValue();
|
||||
return getRawText(node, options);
|
||||
}
|
||||
|
||||
export function printNumberLiteral(
|
||||
path: prettier.FastPath<NumericLiteralNode>,
|
||||
options: ADLPrettierOptions
|
||||
): prettier.doc.builders.Doc {
|
||||
const node = path.getValue();
|
||||
return getRawText(node, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node Node that has postition information.
|
||||
* @param options Prettier options
|
||||
* @returns Raw text in the file for the given node.
|
||||
*/
|
||||
function getRawText(node: TextRange, options: ADLPrettierOptions) {
|
||||
return options.originalText.slice(node.pos, node.end);
|
||||
}
|
||||
|
|
|
@ -397,6 +397,110 @@ namespace Foo {
|
|||
op some(): string;
|
||||
}
|
||||
}
|
||||
`,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("string literals", () => {
|
||||
it("format single line string literal", () => {
|
||||
assertFormat({
|
||||
code: `
|
||||
@doc( "this is a doc. "
|
||||
)
|
||||
model Foo {}
|
||||
`,
|
||||
expected: `
|
||||
@doc("this is a doc. ")
|
||||
model Foo {}
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
||||
it("format single line with newline characters", () => {
|
||||
assertFormat({
|
||||
code: `
|
||||
@doc( "foo\\nbar"
|
||||
)
|
||||
model Foo {}
|
||||
`,
|
||||
expected: `
|
||||
@doc("foo\\nbar")
|
||||
model Foo {}
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
||||
it("format multi line string literal", () => {
|
||||
assertFormat({
|
||||
code: `
|
||||
@doc( """
|
||||
|
||||
this is a doc.
|
||||
that
|
||||
span
|
||||
multiple lines.
|
||||
"""
|
||||
)
|
||||
model Foo {}
|
||||
`,
|
||||
expected: `
|
||||
@doc("""
|
||||
|
||||
this is a doc.
|
||||
that
|
||||
span
|
||||
multiple lines.
|
||||
""")
|
||||
model Foo {}
|
||||
`,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("number literals", () => {
|
||||
it("format integer", () => {
|
||||
assertFormat({
|
||||
code: `
|
||||
alias MyNum = 123 ;
|
||||
`,
|
||||
expected: `
|
||||
alias MyNum = 123;
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
||||
it("format float", () => {
|
||||
assertFormat({
|
||||
code: `
|
||||
alias MyFloat1 = 1.234 ;
|
||||
alias MyFloat2 = 0.123 ;
|
||||
`,
|
||||
expected: `
|
||||
alias MyFloat1 = 1.234;
|
||||
alias MyFloat2 = 0.123;
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
||||
it("format e notation numbers", () => {
|
||||
assertFormat({
|
||||
code: `
|
||||
alias MyBigNumber = 1.0e8 ;
|
||||
`,
|
||||
expected: `
|
||||
alias MyBigNumber = 1.0e8;
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
||||
it("format big numbers", () => {
|
||||
assertFormat({
|
||||
code: `
|
||||
alias MyBigNumber = 1.0e999999999 ;
|
||||
`,
|
||||
expected: `
|
||||
alias MyBigNumber = 1.0e999999999;
|
||||
`,
|
||||
});
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче