Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions packages/coli-export-string/core/jsx/jsx-element.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { JSXElement } from "coli";
import { stringfy, stringfy_tokenformatted, StringfyLanguage } from "../..";
import { indent, KeywordAndTokenStatic } from "@coli.codes/export-string-core";
import { formatters } from "@coli.codes/ast-formatter";
import { stringfy, StringfyLanguage } from "../..";
import { indent } from "@coli.codes/export-string-core";
import { strfy_jsx_opening_element } from "..";
export function strfy_jsx_element(c: JSXElement, l: StringfyLanguage): string {
// const ast = formatters.astfmt_jsx_element(c);
Expand All @@ -14,14 +13,12 @@ export function strfy_jsx_element(c: JSXElement, l: StringfyLanguage): string {
indent.onEachLine(
stringfy(children, {
language: l,
joinWith: KeywordAndTokenStatic.BreakLineToken,
joinWith: "\n",
})
);
const _close = stringfy(closingElement, { language: l });
if (_children) {
return `${_open}
${_children}
${_close}`;
return `${_open}\n${_children}\n${_close}`;
} else {
return `${_open}${_close}`;
}
Expand Down
21 changes: 13 additions & 8 deletions packages/coli-formatter-token/formatters/function-declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,28 @@ export function astfmt_function_declaration(c: FunctionDeclaration) {
]);

return [
f("\n"),
inject.insertBetween(
[f(modifiers.export), f(modifiers.default), f(modifiers.async)].filter(
Boolean
f("\n"), // L-1 pre new line
// L1:keywords
inject.insertTrailing(
inject.insertBetween(
[f(modifiers.export), f(modifiers.default), f(modifiers.async)].filter(
Boolean
),
f(" ")
),
f(" ")
),
f(" "),
// L1: function
f(SyntaxKind.FunctionKeyword),
f(" "),
// L1: function name
id,
f(" "),
// L1: function params
[f(SyntaxKind.OpenParenToken), parameters, f(SyntaxKind.CloseParenToken)],
returnType ? [f(SyntaxKind.ColonToken), f(" "), returnType] : "",
f(" "),
body,
f("\n"),
f("\n"),
body, // a block statement
f("\n\n"), // trailing new line 1
];
}
3 changes: 1 addition & 2 deletions packages/coli-formatter-token/formatters/jsx/jsx-element.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { JSXElement } from "coli";
import { format, inject } from "../..";
import { insertBetween } from "../../utils";
import f from "../../tokens";

export function astfmt_jsx_element(c: JSXElement) {
const { openingElement, closingElement, children } = c;
const _children =
children &&
Array.isArray(children) &&
inject.onEach(insertBetween(format(children), f("\n")), "\t");
inject.onEach(inject.insertBetween(format(children), f("\n")), "\t");
if (_children) {
return [openingElement, ..._children, closingElement];
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ export function astfmt_jsx_with_attributes({
const closing = close_token == "/>" ? "/>" : f(close_token);

if (attributes?.length > 0) {
const join_attributes_with = get_jsx_attribute_join_with_by_attributes(
attributes
);
const join_attributes_with =
get_jsx_attribute_join_with_by_attributes(attributes);
let closing_after_new_line =
join_attributes_with == ["\n", "\t"] ? f("\n") : f("");

Expand Down
4 changes: 3 additions & 1 deletion packages/coli-formatter-token/formatters/return-statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export function astfmt_return_statement(c: ReturnStatement) {
f(SyntaxKind.ReturnKeyword),
f(" "),
f(SyntaxKind.OpenParenToken),
inject.onEach([f("\n"), argument, f("\n")], f("\t")),
// FIXME: do -> inject.indent after formatting (argument)
// currently the jsx is not using ast-formatter, so we can't do this the right way. fix the jsx formatter first.
inject.indent([f("\n"), argument, f("\n")]),
f(SyntaxKind.CloseParenToken),
];
}
Expand Down
1 change: 1 addition & 0 deletions packages/coli-formatter-token/tokens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type FormatterTokenLike =
| SyntaxKind
| " "
| "\n"
| "\n\n"
| "\t"
| "//"
| "/**" // docstring start
Expand Down
36 changes: 36 additions & 0 deletions packages/coli-formatter-token/utils/indent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { FormattingToken } from "../tokens";
export function indent(o: any) {
const flattened = flatten(o);

// loop trhough flattened array, inject the "\t" before the "\n" appears.
return flattened.reduce((acc, item, index) => {
if (
item === "\n" ||
(item instanceof FormattingToken && item.kind === "\n")
) {
acc.push("\n", "\t");
} else {
acc.push(item);
}
return acc;
}, []);
}

/**
* deeply flattens the nested array
* @param arr
* @returns
*/
function flatten(arr) {
const newArr = arr.reduce((acc, item) => {
if (Array.isArray(item)) {
acc = acc.concat(flatten(item));
} else {
acc.push(item);
}

return acc;
}, []);

return newArr;
}
2 changes: 2 additions & 0 deletions packages/coli-formatter-token/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from "./on-each";
export * from "./insert-between";
export * from "./insert-trailing";
export * from "./wrap-with-matching-type";
export * from "./wrap-with-double-quote";
export * from "./wrap-array-with";
export * from "./extra-trailing-space-by-size";
export * from "./indent";
6 changes: 6 additions & 0 deletions packages/coli-formatter-token/utils/insert-trailing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* inserts the trailing item if the existing arrays are not empty
*/
export function insertTrailing(arr: any[], insert: any) {
return arr && arr.push(insert) && arr;
}