Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transpile override to AstEditor #511

Merged
merged 2 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/astUtils/AstEditor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { expect } from 'chai';
import { BrsTranspileState } from '../parser/BrsTranspileState';
import { AstEditor } from './AstEditor';
import { util } from '../util';
import { createToken } from '../astUtils/creators';
import { TokenKind } from '../lexer/TokenKind';
import { Program } from '../Program';
import { BrsFile } from '../files/BrsFile';
import { LiteralExpression } from '../parser/Expression';
import { SourceNode } from 'source-map';

describe('AstEditor', () => {
let changer: AstEditor;
Expand Down Expand Up @@ -158,4 +166,38 @@ describe('AstEditor', () => {
changer.undoAll();
expect(obj).to.eql(getTestObject());
});

describe('overrideTranspileResult', () => {
const state = new BrsTranspileState(new BrsFile('', '', new Program({})));
function transpileToString(transpilable: { transpile: (state: BrsTranspileState) => any }) {
if (transpilable.transpile) {
const result = transpilable.transpile(state);
if (Array.isArray(result)) {
return new SourceNode(null, null, null, result).toString();
}
}
}
it('overrides existing transpile method', () => {
const expression = new LiteralExpression(createToken(TokenKind.IntegerLiteral, 'original'));

expect(transpileToString(expression)).to.eql('original');

changer.overrideTranspileResult(expression, 'replaced');
expect(transpileToString(expression)).to.eql('replaced');

changer.undoAll();
expect(transpileToString(expression)).to.eql('original');
});

it('gracefully handles missing transpile method', () => {
const expression = {
range: util.createRange(1, 2, 3, 4)
} as any;
expect(expression.transpile).not.to.exist;
changer.overrideTranspileResult(expression, 'replaced');
expect(transpileToString(expression)).to.eql('replaced');
changer.undoAll();
expect(expression.transpile).not.to.exist;
});
});
});
14 changes: 14 additions & 0 deletions src/astUtils/AstEditor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { Expression } from '../parser/Expression';
import type { Statement } from '../parser/Statement';

export class AstEditor {
private changes: Change[] = [];

Expand All @@ -17,6 +20,17 @@ export class AstEditor {
change.apply();
}

/**
* Set custom text that will be emitted during transpile instead of the original text.
*/
public overrideTranspileResult(node: Expression | Statement, value: string) {
this.setProperty(node, 'transpile', (state) => {
return [
state.sourceNode(node, value)
];
});
}

/**
* Insert an element into an array at the specified index
*/
Expand Down