Fix AST formatter and serializer

This commit is contained in:
Sam Vervaeck 2020-06-16 21:05:41 +02:00
parent 0b7aacbc1e
commit 0019f37749
5 changed files with 14 additions and 13 deletions

View file

@ -25,7 +25,7 @@ export abstract class Syntax {
// NOTE The following properties and methods are only valid when inside a BoltTraitDeclaration // NOTE The following properties and methods are only valid when inside a BoltTraitDeclaration
// TODO Move this to BoltTraitDeclaration as soon as tsastgen supports this // TODO Move this to BoltTraitDeclaration as soon as tsastgen supports this
private impls?: BoltImplDeclaration[] = []; private impls?: BoltImplDeclaration[];
public addImplDeclaration(node: BoltImplDeclaration) { public addImplDeclaration(node: BoltImplDeclaration) {
if (this.impls === undefined) { if (this.impls === undefined) {
@ -61,10 +61,10 @@ export abstract class Syntax {
} }
let out = `${proto.constructor.name} {\n`; let out = `${proto.constructor.name} {\n`;
for (const key of Object.keys(this)) { for (const key of Object.keys(this)) {
if (key === 'kind' || key === 'parentNode' || key === 'errors' || key === 'type' || key === 'id') { if (key === 'kind' || key === 'impls' || key === 'parentNode' || key === 'errors' || key === 'type' || key === 'id') {
continue; continue;
} }
out += `${key}: ${inspect((this as any)[key], newOptions)},\n`; out += indent(`${key}: ${inspect((this as any)[key], newOptions)},\n`);
} }
out += '}\n'; out += '}\n';
return out; return out;
@ -73,7 +73,7 @@ export abstract class Syntax {
private [serializeTag]() { private [serializeTag]() {
const result: any[] = []; const result: any[] = [];
for (const key of Object.keys(this)) { for (const key of Object.keys(this)) {
if (key === 'kind' || key === 'span' || key === 'parentNode' || key === 'errors' || key === 'type' || key === 'id') { if (key === 'kind' || key === 'impls' || key === 'span' || key === 'parentNode' || key === 'errors' || key === 'type' || key === 'id') {
continue; continue;
} }
result.push((this as any)[key]); result.push((this as any)[key]);

View file

@ -25,7 +25,7 @@ export abstract class SyntaxBase {
// -------------------------------------------------------------------------------- // --------------------------------------------------------------------------------
// NOTE The following properties and methods are only valid when inside a BoltTraitDeclaration // NOTE The following properties and methods are only valid when inside a BoltTraitDeclaration
// TODO Move this to BoltTraitDeclaration as soon as tsastgen supports this // TODO Move this to BoltTraitDeclaration as soon as tsastgen supports this
private impls?: BoltImplDeclaration[] = []; private impls?: BoltImplDeclaration[];
public addImplDeclaration(node: BoltImplDeclaration) { public addImplDeclaration(node: BoltImplDeclaration) {
if (this.impls === undefined) { if (this.impls === undefined) {
this.impls = []; this.impls = [];
@ -53,10 +53,10 @@ export abstract class SyntaxBase {
}; };
let out = `${proto.constructor.name} {\n`; let out = `${proto.constructor.name} {\n`;
for (const key of Object.keys(this)) { for (const key of Object.keys(this)) {
if (key === 'kind' || key === 'parentNode' || key === 'errors' || key === 'type' || key === 'id') { if (key === 'kind' || key === 'impls' || key === 'parentNode' || key === 'errors' || key === 'type' || key === 'id') {
continue; continue;
} }
out += `${key}: ${inspect((this as any)[key], newOptions)},\n`; out += indent(`${key}: ${inspect((this as any)[key], newOptions)},\n`);
} }
out += '}\n'; out += '}\n';
return out; return out;
@ -64,7 +64,7 @@ export abstract class SyntaxBase {
private [serializeTag]() { private [serializeTag]() {
const result: any[] = []; const result: any[] = [];
for (const key of Object.keys(this)) { for (const key of Object.keys(this)) {
if (key === 'kind' || key === 'span' || key === 'parentNode' || key === 'errors' || key === 'type' || key === 'id') { if (key === 'kind' || key === 'impls' || key === 'span' || key === 'parentNode' || key === 'errors' || key === 'type' || key === 'id') {
continue; continue;
} }
result.push((this as any)[key]); result.push((this as any)[key]);

View file

@ -525,10 +525,11 @@ export function indent(text: string, indentation = ' ', afterNewLine = true) {
if (ch === '\n') { if (ch === '\n') {
afterNewLine = true; afterNewLine = true;
out += ch; out += ch;
} else if (afterNewLine) {
out += indentation + ch;
afterNewLine = false;
} else { } else {
if (afterNewLine && !/[\t ]/.test(ch)) {
out += indentation;
afterNewLine = false;
}
out += ch; out += ch;
} }
} }

View file

@ -1 +1 @@
533381409c88be6a3b19b035f6082831c15985315e186ea2430d1bc8d1f984818e1cd07f960756cbbb8f68e5f087afcd43bc2fa6eb4125ed0b21913f0d228e3f 9e87b23b96c54b7245893be35befc6fe7fd2c14ef41cc4b4815e50aee6b07d0c2c6be999719ecd236a55bbc227598117cfd2f747955c3f10b503edb069cf1828