Skip to content

Commit

Permalink
Remove async file adding from program (#278)
Browse files Browse the repository at this point in the history
* API change

* test changes to match new api

* Fix docs compiler

* update validate benchmark

* Restore original async file resolver
  • Loading branch information
TwitchBronBron authored Jan 19, 2021
1 parent 7544643 commit 06d8dbe
Show file tree
Hide file tree
Showing 24 changed files with 1,314 additions and 1,409 deletions.
10 changes: 7 additions & 3 deletions benchmarks/targets/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ module.exports = async (suite, name, brighterscript, projectPath) => {
throw new Error('No files found in program');
}

suite.add(name, async (deferred) => {
suite.add(name, (deferred) => {
const scopes = Object.values(builder.program.scopes);
//mark all scopes as invalid so they'll re-validate
for (let scope of scopes) {
scope.invalidate();
}
await builder.program.validate();
deferred.resolve();
let promise = builder.program.validate();
if (promise) {
promise.then(() => deferred.resolve());
} else {
deferred.resolve();
}
}, {
'defer': true
});
Expand Down
38 changes: 19 additions & 19 deletions scripts/compile-doc-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,46 @@ class DocCompiler {
private docsFolder = path.resolve(path.join(__dirname, '..', 'docs'));
public async run() {

var docs = glob.sync('**/*.md', {
let docs = glob.sync('**/*.md', {
cwd: this.docsFolder,
absolute: true
});

for (let docPath of docs) {
console.log('\n', docPath);
await this.processDoc(docPath);
this.processDoc(docPath);
}
}

private lines: string[];
private index: number;

public async processDoc(docPath: string) {
public processDoc(docPath: string) {
let contents = fsExtra.readFileSync(docPath).toString();
this.lines = util.splitIntoLines(contents);
this.index = 0;
while (this.index < this.lines.length) {
let line = this.lines[this.index];
if (line.includes('```')) {
await this.processCodeBlock();
this.processCodeBlock();
}
this.index++;
}

var result = this.lines.join('\n');
let result = this.lines.join('\n');
fsExtra.writeFileSync(docPath, result);
delete this.lines;
this.index = -1;
}

public async processCodeBlock() {
var sourceLines = [] as string[];
var sourceStartIndex = this.index + 1;
var sourceStopIndex: number;
public processCodeBlock() {
let sourceLines = [] as string[];
let sourceStartIndex = this.index + 1;
let sourceStopIndex: number;

//find the rest of the source code block
//+1 to step past the opening ```
for (var i = this.index + 1; i < this.lines.length; i++) {
for (let i = this.index + 1; i < this.lines.length; i++) {
let line = this.lines[i];
if (line.includes('```')) {
sourceStopIndex = i - 1;
Expand All @@ -59,11 +59,11 @@ class DocCompiler {

let sourceCode = sourceLines.join('\n');

var transpiledStartIndex: number;
var transpiledStopIndex: number;
let transpiledStartIndex: number;
let transpiledStopIndex: number;
//find the transpiled code block (there must be one after every
//+2 to step past the last line of code, and the final ```
outer: for (var i = sourceStopIndex + 2; i < this.lines.length; i++) {
outer: for (let i = sourceStopIndex + 2; i < this.lines.length; i++) {
let line = this.lines[i];
//the next code block MUST be a brightscript block. hard-fail if it isn't
if (line.includes('```')) {
Expand All @@ -88,9 +88,9 @@ class DocCompiler {
//now that we have the range for the transpiled code, we need to transpile the source code
if (transpiledStartIndex && transpiledStopIndex && sourceCode) {
console.log(`Transpiling code block at lines ${sourceStartIndex}-${sourceStopIndex}`);
var transpiledCode = await this.transpile(sourceCode);
const transpiledCode = this.transpile(sourceCode);
let transpiledLines = transpiledCode.split('\n');
let originalTranspiledLineCount = transpiledStopIndex - transpiledStartIndex
let originalTranspiledLineCount = transpiledStopIndex - transpiledStartIndex;

//replace the old transpiled lines with the new ones
this.lines.splice(
Expand All @@ -103,15 +103,15 @@ class DocCompiler {
}
}

public async transpile(code: string) {
var program = new Program({
public transpile(code: string) {
const program = new Program({
rootDir: `${__dirname}/rootDir`,
files: [
'source/main.brs'
]
});
var file = await program.addOrReplaceFile({ src: `${__dirname}/rootDir/source/main.bs`, dest: 'source/main.bs' }, code)
await program.validate();
const file = program.addOrReplaceFile({ src: `${__dirname}/rootDir/source/main.bs`, dest: 'source/main.bs' }, code);
program.validate();
let tranpileResult = file.transpile();
return tranpileResult.code;
}
Expand Down
4 changes: 2 additions & 2 deletions src/FunctionScope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ describe('FunctionScope', () => {
expect(variables).to.be.lengthOf(0);
});

it('returns variables defined above the specified line number', async () => {
let file = await program.addOrReplaceFile({ src: `${rootDir}/source/main.brs`, dest: 'source/main.brs' }, `
it('returns variables defined above the specified line number', () => {
let file = program.addOrReplaceFile({ src: `${rootDir}/source/main.brs`, dest: 'source/main.brs' }, `
sub main()
var1 = 1
var2 = 2
Expand Down
53 changes: 27 additions & 26 deletions src/LanguageServer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,20 @@ describe('LanguageServer', () => {
server.dispose();
});

async function addXmlFile(name: string, additionalXmlContents = '') {
function addXmlFile(name: string, additionalXmlContents = '') {
const filePath = `components/${name}.xml`;

const contents = `<?xml version="1.0" encoding="utf-8"?>
<component name="${name}" extends="Group">
${additionalXmlContents}
<script type="text/brightscript" uri="${name}.brs" />
</component>`;
await program.addOrReplaceFile(filePath, contents);
program.addOrReplaceFile(filePath, contents);
}

async function addScriptFile(name: string, contents: string, extension = 'brs') {
function addScriptFile(name: string, contents: string, extension = 'brs') {
const filePath = s`components/${name}.${extension}`;
await program.addOrReplaceFile(filePath, contents);
program.addOrReplaceFile(filePath, contents);
for (const key in program.files) {
if (key.includes(filePath)) {
const document = TextDocument.create(util.pathToUri(key), 'brightscript', 1, contents);
Expand Down Expand Up @@ -279,6 +279,7 @@ describe('LanguageServer', () => {
'source/**/*'
]
},
getFileContents: sinon.stub().callsFake(() => Promise.resolve('')) as any,
rootDir: rootDir,
program: {
addOrReplaceFile: <any>addOrReplaceFileStub
Expand All @@ -294,7 +295,7 @@ describe('LanguageServer', () => {
pathAbsolute: mainPath
}]);

expect(addOrReplaceFileStub.getCalls()[0].args[0]).to.eql({
expect(addOrReplaceFileStub.getCalls()[0]?.args[0]).to.eql({
src: mainPath,
dest: s`source/main.brs`
});
Expand Down Expand Up @@ -362,7 +363,7 @@ describe('LanguageServer', () => {
program = svr.workspaces[0].builder.program;

const name = `CallComponent`;
callDocument = await addScriptFile(name, `
callDocument = addScriptFile(name, `
sub init()
shouldBuildAwesome = true
if shouldBuildAwesome then
Expand All @@ -372,14 +373,14 @@ describe('LanguageServer', () => {
end if
end sub
`);
await addXmlFile(name, `<script type="text/brightscript" uri="${functionFileBaseName}.bs" />`);
addXmlFile(name, `<script type="text/brightscript" uri="${functionFileBaseName}.bs" />`);
});

it('should return the expected signature info when documentation is included', async () => {
const funcDescriptionComment = '@description Builds awesome for you';
const funcReturnComment = '@return {Integer} The key to everything';

await addScriptFile(functionFileBaseName, `
addScriptFile(functionFileBaseName, `
' /**
' * ${funcDescriptionComment}
' * ${funcReturnComment}
Expand All @@ -403,7 +404,7 @@ describe('LanguageServer', () => {
});

it('should work if used on a property value', async () => {
await addScriptFile(functionFileBaseName, `
addScriptFile(functionFileBaseName, `
${funcDefinitionLine}
return 42
end function
Expand All @@ -422,7 +423,7 @@ describe('LanguageServer', () => {

it('should give the correct signature for a class method', async () => {
const classMethodDefinitionLine = 'function buildAwesome(classVersion = true as Boolean)';
await addScriptFile(functionFileBaseName, `
addScriptFile(functionFileBaseName, `
class ${functionFileBaseName}
${classMethodDefinitionLine}
return 42
Expand Down Expand Up @@ -453,15 +454,15 @@ describe('LanguageServer', () => {
program = svr.workspaces[0].builder.program;

const functionFileBaseName = 'buildAwesome';
functionDocument = await addScriptFile(functionFileBaseName, `
functionDocument = addScriptFile(functionFileBaseName, `
function buildAwesome()
return 42
end function
`);

for (let i = 0; i < 5; i++) {
let name = `CallComponent${i}`;
const document = await addScriptFile(name, `
const document = addScriptFile(name, `
sub init()
shouldBuildAwesome = true
if shouldBuildAwesome then
Expand All @@ -470,7 +471,7 @@ describe('LanguageServer', () => {
end sub
`);

await addXmlFile(name, `<script type="text/brightscript" uri="${functionFileBaseName}.brs" />`);
addXmlFile(name, `<script type="text/brightscript" uri="${functionFileBaseName}.brs" />`);
referenceFileUris.push(document.uri);
}
});
Expand Down Expand Up @@ -521,7 +522,7 @@ describe('LanguageServer', () => {
program = svr.workspaces[0].builder.program;

const functionFileBaseName = 'buildAwesome';
functionDocument = await addScriptFile(functionFileBaseName, `
functionDocument = addScriptFile(functionFileBaseName, `
function pi()
return 3.141592653589793
end function
Expand All @@ -532,7 +533,7 @@ describe('LanguageServer', () => {
`);

const name = `CallComponent`;
referenceDocument = await addScriptFile(name, `
referenceDocument = addScriptFile(name, `
sub init()
shouldBuildAwesome = true
if shouldBuildAwesome then
Expand All @@ -543,7 +544,7 @@ describe('LanguageServer', () => {
end sub
`);

await addXmlFile(name, `<script type="text/brightscript" uri="${functionFileBaseName}.brs" />`);
addXmlFile(name, `<script type="text/brightscript" uri="${functionFileBaseName}.brs" />`);
});

it('should return the expected location if we entered on an identifier token', async () => {
Expand Down Expand Up @@ -605,7 +606,7 @@ describe('LanguageServer', () => {

it('should work for bs class functions as well', async () => {
const functionFileBaseName = 'Build';
functionDocument = await addScriptFile(functionFileBaseName, `
functionDocument = addScriptFile(functionFileBaseName, `
class ${functionFileBaseName}
function awesome()
return 42
Expand All @@ -614,14 +615,14 @@ describe('LanguageServer', () => {
`, 'bs');

const name = `CallComponent`;
referenceDocument = await addScriptFile(name, `
referenceDocument = addScriptFile(name, `
sub init()
build = new Build()
build.awesome()
end sub
`);

await addXmlFile(name, `<script type="text/brightscript" uri="${functionFileBaseName}.bs" />`);
addXmlFile(name, `<script type="text/brightscript" uri="${functionFileBaseName}.bs" />`);

const locations = await svr.onDefinition({
textDocument: {
Expand All @@ -647,7 +648,7 @@ describe('LanguageServer', () => {
});

it('should return the expected symbols even if pulled from cache', async () => {
const document = await addScriptFile('buildAwesome', `
const document = addScriptFile('buildAwesome', `
function pi()
return 3.141592653589793
end function
Expand All @@ -669,7 +670,7 @@ describe('LanguageServer', () => {
});

it('should work for brightscript classes as well', async () => {
const document = await addScriptFile('MyFirstClass', `
const document = addScriptFile('MyFirstClass', `
class MyFirstClass
function pi()
return 3.141592653589793
Expand Down Expand Up @@ -698,7 +699,7 @@ describe('LanguageServer', () => {
});

it('should work for brightscript namespaces as well', async () => {
const document = await addScriptFile('MyFirstNamespace', `
const document = addScriptFile('MyFirstNamespace', `
namespace MyFirstNamespace
function pi()
return 3.141592653589793
Expand Down Expand Up @@ -738,7 +739,7 @@ describe('LanguageServer', () => {
const className = 'MyFirstClass';
const namespaceName = 'MyFirstNamespace';

await addScriptFile('buildAwesome', `
addScriptFile('buildAwesome', `
function pi()
return 3.141592653589793
end function
Expand All @@ -748,7 +749,7 @@ describe('LanguageServer', () => {
end function
`);

await addScriptFile(className, `
addScriptFile(className, `
class ${className}
function ${className}pi()
return 3.141592653589793
Expand All @@ -761,7 +762,7 @@ describe('LanguageServer', () => {
`, 'bs');


await addScriptFile(namespaceName, `
addScriptFile(namespaceName, `
namespace ${namespaceName}
function pi()
return 3.141592653589793
Expand Down Expand Up @@ -810,7 +811,7 @@ describe('LanguageServer', () => {
const nestedNamespace = 'containerNamespace';
const nestedClassName = 'nestedClass';

await addScriptFile('nested', `
addScriptFile('nested', `
namespace ${nestedNamespace}
class ${nestedClassName}
function pi()
Expand Down
Loading

0 comments on commit 06d8dbe

Please sign in to comment.