Skip to content

Commit

Permalink
Merge pull request #5 from Rich-Harris/groups
Browse files Browse the repository at this point in the history
Groups
  • Loading branch information
Rich-Harris committed Nov 6, 2023
2 parents 9ba07da + 6eda74b commit f61783d
Show file tree
Hide file tree
Showing 20 changed files with 145 additions and 241 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ concurrency:
cancel-in-progress: true

jobs:
Check:
runs-on: ubuntu-latest
steps:
- run: git config --global core.autocrlf false
- uses: actions/checkout@v3
- uses: oven-sh/setup-bun@v1
- run: bun install
- name: check
run: bun check
Tests:
runs-on: ubuntu-latest
steps:
Expand All @@ -21,5 +30,3 @@ jobs:
- run: bun install
- name: test
run: bun test
- name: check
run: bun check
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"types": "./types/index.d.ts",
"devDependencies": {
"acorn": "^8.10.0",
"bun-types": "^1.0.9",
"dts-buddy": "^0.2.4",
"prettier": "^3.0.3",
"typescript": "^5.2.2",
Expand Down
168 changes: 0 additions & 168 deletions pnpm-lock.yaml

This file was deleted.

149 changes: 105 additions & 44 deletions src/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,7 @@ export function handle(node, state) {
const result = handler(node, state);

if (node.leadingComments) {
result.unshift(
c(
node.leadingComments
.map((comment) =>
comment.type === 'Block'
? `/*${comment.value}*/${
/** @type {any} */ (comment).has_trailing_newline ? `\n${state.indent}` : ` `
}`
: `//${comment.value}${
/** @type {any} */ (comment).has_trailing_newline ? `\n${state.indent}` : ` `
}`
)
.join(``)
)
);
prepend_comments(result, node.leadingComments, state);
}

if (node.trailingComments) {
Expand All @@ -67,6 +53,29 @@ function c(content, node) {
};
}

/**
* @param {import('./types').Chunk[]} chunks
* @param {import('estree').Comment[]} comments
* @param {import('./types').State} state
*/
function prepend_comments(chunks, comments, state) {
chunks.unshift(
c(
comments
.map((comment) =>
comment.type === 'Block'
? `/*${comment.value}*/${
/** @type {any} */ (comment).has_trailing_newline ? `\n${state.indent}` : ` `
}`
: `//${comment.value}${
/** @type {any} */ (comment).has_trailing_newline ? `\n${state.indent}` : ` `
}`
)
.join(``)
)
);
}

const OPERATOR_PRECEDENCE = {
'||': 2,
'&&': 3,
Expand Down Expand Up @@ -235,53 +244,105 @@ const join = (nodes, separator) => {
return joined;
};

const grouped_expression_types = [
'ImportDeclaration',
'VariableDeclaration',
'ExportDefaultDeclaration',
'ExportNamedDeclaration'
];

/**
* @param {import('estree').Node[]} nodes
* @param {import('./types').State} state
*/
const handle_body = (nodes, state) => {
const chunks = [];
/** @type {import('./types').Chunk[][][]} */
const groups = [];

const body = nodes
.filter((statement) => statement.type !== 'EmptyStatement')
.map((statement) => {
const chunks = handle(statement, {
...state,
indent: state.indent
});
/** @type {import('./types').Chunk[][]} */
let group = [];

let add_newline = false;
let last_statement = /** @type {import('estree').Node} */ ({ type: 'EmptyStatement' });

while (state.comments.length) {
const comment = /** @type {import('estree').Comment} */ (state.comments.shift());
const prefix = add_newline ? `\n${state.indent}` : ` `;

chunks.push(
c(
comment.type === 'Block'
? `${prefix}/*${comment.value}*/`
: `${prefix}//${comment.value}`
)
);
function flush() {
if (group.length > 0) {
groups.push(group);
group = [];
}
}

add_newline = comment.type === 'Line';
}
for (const statement of nodes) {
if (statement.type === 'EmptyStatement') continue;

return chunks;
if (
(grouped_expression_types.includes(statement.type) ||
grouped_expression_types.includes(last_statement.type)) &&
last_statement.type !== statement.type
) {
flush();
}

const leadingComments = statement.leadingComments;
delete statement.leadingComments;

const chunks = handle(statement, {
...state,
indent: state.indent
});

let needed_padding = false;
// if a statement requires multiple lines, or it has a leading `/**` comment,
// we add blank lines around it
const standalone =
has_newline(chunks) ||
(leadingComments?.[0]?.type === 'Block' && leadingComments[0].value.startsWith('*'));

if (leadingComments && leadingComments.length > 0) {
prepend_comments(chunks, leadingComments, state);
flush();
}

let add_newline = false;

for (let i = 0; i < body.length; i += 1) {
const needs_padding = has_newline(body[i]);
while (state.comments.length) {
const comment = /** @type {import('estree').Comment} */ (state.comments.shift());
const prefix = add_newline ? `\n${state.indent}` : ` `;

chunks.push(
c(
comment.type === 'Block' ? `${prefix}/*${comment.value}*/` : `${prefix}//${comment.value}`
)
);

add_newline = comment.type === 'Line';
}

if (standalone) {
flush();
group.push(chunks);
flush();
} else {
group.push(chunks);
}

last_statement = statement;
}

flush();

const chunks = [];

for (let i = 0; i < groups.length; i += 1) {
if (i > 0) {
chunks.push(c(needs_padding || needed_padding ? `\n\n${state.indent}` : `\n${state.indent}`));
chunks.push(c(`\n\n${state.indent}`));
}

push_array(chunks, body[i]);
for (let j = 0; j < groups[i].length; j += 1) {
if (j > 0) {
chunks.push(c(`\n${state.indent}`));
}

needed_padding = needs_padding;
push_array(chunks, groups[i][j]);
}
}

return chunks;
Expand Down
Loading

0 comments on commit f61783d

Please sign in to comment.