I've written a VS Code extension that uses TypeScrpt AST API for organizing class members. My issue is that after running ts.transform(...) and than convert transformed syntax tree back to text all empty lines are missing making the resulting source code incorrectly formatted. How do I prevent AST API from removing blank lines?
Sample of the code I'm using:
let sourceFile: ts.SourceFile;
let sourceCode: string;
sourceCode = editor.document.getText();
sourceFile = ts.createSourceFile(editor.document.fileName, sourceCode, ts.ScriptTarget.Latest, false, ts.ScriptKind.TS);
transformation = ts.transform(sourceFile, [organizeTransformer]);
sourceCode = transformation.transformed[0].getFullText();
Workaround:
replace comments with empty line
import {decodeEmptyLines, encodeEmptyLines} from 'ts-empty-line-encoder';
let sourceCode = editor.document.getText();
//encode empty lines
sourceCode = encodeEmptyLines(sourceCode);
const sourceFile = ts.createSourceFile(editor.document.fileName, sourceCode, ts.ScriptTarget.Latest, false, ts.ScriptKind.TS);
const transformation = ts.transform(sourceFile, [organizeTransformer]);
sourceCode = transformation.transformed[0].getFullText();
//decode empty lines
sourceCode = decodeEmptyLines(sourceCode);
A parser is not the best tool for code formatting:
In fact pretty printing doesn't need parsing at all. It's a source to source transformation and all what's needed is a lexer, to identify the various types of input elements (as they are relevant for formatting, in particular whitespaces + comments). You can see a way to implement a code formatter in my vscode extension vscode-antlr4. The principle is simple: collect source positions (not source text) for each non-white space element in a lists (including comments). Add the formatting whitespaces too. Then generate the new text from this list by copying the original text to the output. That avoids trouble with quoting, number radixes, comment types etc., which a parser might convert in a way that makes it easier for its processing, but doesn't necessarily represent the original form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With