js-slang
Version:
Javascript-based implementations of Source, written in Typescript
80 lines • 2.92 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.astToString = void 0;
const astring = require("astring");
/**
* Writes into `state` the `text` string reindented with the provided `indent`.
*/
function reindent(state, text, indent, lineEnd) {
const lines = text.split('\n');
const end = lines.length - 1;
state.write(lines[0].trim());
if (end > 0) {
state.write(lineEnd);
for (let i = 1; i < end; i++) {
state.write(indent + lines[i].trim() + lineEnd);
}
state.write(indent + lines[end].trim());
}
}
/**
* Writes into `state` the provided list of `comments`, with the given `indent` and `lineEnd` strings.
* Line comments will end with `"\n"` regardless of the value of `lineEnd`.
* Expects to start on a new unindented line.
*/
function formatComments(state, comments, indent, lineEnd) {
const { length } = comments;
for (let i = 0; i < length; i++) {
const comment = comments[i];
state.write(indent);
if (comment.type[0] === 'L') {
// Line comment
state.write('// ' + comment.value.trim() + '\n', comment);
}
else {
// Block comment
state.write('/*');
reindent(state, comment.value, indent, lineEnd);
state.write('*/' + lineEnd);
}
}
}
const sourceGen = Object.assign({}, astring.GENERATOR, {
StatementSequence: function (node, state) {
const indent = state.indent.repeat(state.indentLevel);
const { lineEnd, writeComments } = state;
const statements = node.body;
if (statements != null && statements.length > 0) {
state.write(lineEnd);
if (writeComments && node.comments != null) {
formatComments(state, node.comments, indent, lineEnd);
}
const { length } = statements;
for (let i = 0; i < length; i++) {
const statement = statements[i];
if (writeComments && statement.comments != null) {
formatComments(state, statement.comments, indent, lineEnd);
}
state.write(indent);
this[statement.type](statement, state);
state.write(lineEnd);
}
state.write(indent);
}
else {
if (writeComments && node.comments != null) {
state.write(lineEnd);
formatComments(state, node.comments, indent, lineEnd);
state.write(indent);
}
}
if (writeComments && node.trailingComments != null) {
formatComments(state, node.trailingComments, indent, lineEnd);
}
}
});
const astToString = (node) => astring.generate(node, {
generator: sourceGen
});
exports.astToString = astToString;
//# sourceMappingURL=astToString.js.map
;