Comparing version 1.1.0 to 1.1.1
{ | ||
"name": "esrap", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Parse in reverse", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -33,17 +33,3 @@ // heavily based on https://github.com/davidbonnet/astring | ||
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); | ||
} | ||
@@ -71,2 +57,25 @@ | ||
/** | ||
* @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 = { | ||
@@ -240,2 +249,9 @@ '||': 2, | ||
const grouped_expression_types = [ | ||
'ImportDeclaration', | ||
'VariableDeclaration', | ||
'ExportDefaultDeclaration', | ||
'ExportNamedDeclaration' | ||
]; | ||
/** | ||
@@ -246,44 +262,89 @@ * @param {import('estree').Node[]} nodes | ||
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}` : ` `; | ||
function flush() { | ||
if (group.length > 0) { | ||
groups.push(group); | ||
group = []; | ||
} | ||
} | ||
chunks.push( | ||
c( | ||
comment.type === 'Block' | ||
? `${prefix}/*${comment.value}*/` | ||
: `${prefix}//${comment.value}` | ||
) | ||
); | ||
for (const statement of nodes) { | ||
if (statement.type === 'EmptyStatement') continue; | ||
add_newline = comment.type === 'Line'; | ||
} | ||
if ( | ||
(grouped_expression_types.includes(statement.type) || | ||
grouped_expression_types.includes(last_statement.type)) && | ||
last_statement.type !== statement.type | ||
) { | ||
flush(); | ||
} | ||
return chunks; | ||
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('*')); | ||
for (let i = 0; i < body.length; i += 1) { | ||
const needs_padding = has_newline(body[i]); | ||
if (leadingComments && leadingComments.length > 0) { | ||
prepend_comments(chunks, leadingComments, state); | ||
flush(); | ||
} | ||
let add_newline = false; | ||
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]); | ||
} | ||
} | ||
@@ -290,0 +351,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40997
1307