Skip to content

Commit

Permalink
Formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Jan 16, 2022
1 parent a657ccc commit d00fae9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 49 deletions.
5 changes: 2 additions & 3 deletions src/files/BrsFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,10 @@ describe('BrsFile', () => {
names = result.map(x => x.label);
expect(names).to.includes('sayHello');
});

});
describe('enums', () => {

it.only('gets enum completions', () => {
describe('enums', () => {
it('gets enum completions', () => {
//eslint-disable-next-line @typescript-eslint/no-floating-promises
program.addOrReplaceFile('source/main.bs', `
enum foo
Expand Down
92 changes: 46 additions & 46 deletions src/files/BrsFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class BrsFile {
private diagnostics = [] as BsDiagnostic[];

public getDiagnostics() {
return [ ...this.diagnostics ];
return [...this.diagnostics];
}

public addDiagnostics(diagnostics: BsDiagnostic[]) {
Expand Down Expand Up @@ -226,7 +226,7 @@ export class BrsFile {
}

//tokenize the input file
let lexer = this.program.logger.time(LogLevel.debug, [ 'lexer.lex', chalk.green(this.pathAbsolute) ], () => {
let lexer = this.program.logger.time(LogLevel.debug, ['lexer.lex', chalk.green(this.pathAbsolute)], () => {
return Lexer.scan(fileContents, {
includeWhitespace: false
});
Expand All @@ -240,7 +240,7 @@ export class BrsFile {
//TODO preprocessor should go away in favor of the AST handling this internally (because it affects transpile)
//currently the preprocessor throws exceptions on syntax errors...so we need to catch it
try {
this.program.logger.time(LogLevel.debug, [ 'preprocessor.process', chalk.green(this.pathAbsolute) ], () => {
this.program.logger.time(LogLevel.debug, ['preprocessor.process', chalk.green(this.pathAbsolute)], () => {
preprocessor.process(lexer.tokens, this.program.getManifest());
});
} catch (error: any) {
Expand All @@ -253,7 +253,7 @@ export class BrsFile {
//if the preprocessor generated tokens, use them.
let tokens = preprocessor.processedTokens.length > 0 ? preprocessor.processedTokens : lexer.tokens;

this.program.logger.time(LogLevel.debug, [ 'parser.parse', chalk.green(this.pathAbsolute) ], () => {
this.program.logger.time(LogLevel.debug, ['parser.parse', chalk.green(this.pathAbsolute)], () => {
this._parser = Parser.parse(tokens, {
mode: this.parseMode,
logger: this.program.logger
Expand Down Expand Up @@ -381,7 +381,7 @@ export class BrsFile {
const results = [] as CompletionItem[];
for (const key of Object.keys(propertyHints)) {
results.push({
label: propertyHints[ key ],
label: propertyHints[key],
kind: CompletionItemKind.Text
});
}
Expand All @@ -402,7 +402,7 @@ export class BrsFile {
* @param lines - the lines of the program
*/
public getCommentFlags(tokens: Token[]) {
const processor = new CommentFlagProcessor(this, [ 'rem', `'` ], diagnosticCodes, [ DiagnosticCodeMap.unknownDiagnosticCode ]);
const processor = new CommentFlagProcessor(this, ['rem', `'`], diagnosticCodes, [DiagnosticCodeMap.unknownDiagnosticCode]);

this.commentFlags = [];
for (let token of tokens) {
Expand Down Expand Up @@ -724,7 +724,7 @@ export class BrsFile {
} else if (tokenKind === TokenKind.StringLiteral || tokenKind === TokenKind.TemplateStringQuasi) {
const match = /^("?)(pkg|libpkg):/.exec(currentToken.text);
if (match) {
const [ , openingQuote, fileProtocol ] = match;
const [, openingQuote, fileProtocol] = match;
//include every absolute file path from this scope
for (const file of scope.getAllFiles()) {
const pkgPath = `${fileProtocol}:/${file.pkgPath.replace(/\\/g, '/')}`;
Expand Down Expand Up @@ -755,7 +755,7 @@ export class BrsFile {
let enumCompletions = this.getEnumStatementCompletions(currentToken, this.parseMode);
let namespaceCompletions = this.getNamespaceCompletions(currentToken, this.parseMode, scope);
if (namespaceCompletions.length > 0) {
return [ ...namespaceCompletions, ...enumCompletions ];
return [...namespaceCompletions, ...enumCompletions];
}

let enumMemberCompletions = this.getEnumMemberStatementCompletions(currentToken, this.parseMode);
Expand All @@ -769,9 +769,9 @@ export class BrsFile {
//we aren't in any function scope, so return the keyword completions and namespaces
if (this.getTokenBefore(currentToken, TokenKind.New)) {
// there's a new keyword, so only class types are viable here
return [ ...this.getGlobalClassStatementCompletions(currentToken, this.parseMode) ];
return [...this.getGlobalClassStatementCompletions(currentToken, this.parseMode)];
} else {
return [ ...KeywordCompletions, ...this.getGlobalClassStatementCompletions(currentToken, this.parseMode), ...namespaceCompletions, ...this.getEnumStatementCompletions(currentToken, this.parseMode) ];
return [...KeywordCompletions, ...this.getGlobalClassStatementCompletions(currentToken, this.parseMode), ...namespaceCompletions, ...this.getEnumStatementCompletions(currentToken, this.parseMode)];
}
}

Expand All @@ -793,7 +793,7 @@ export class BrsFile {
const selfClassMemberCompletions = this.getClassMemberCompletions(position, currentToken, functionScope, scope);

if (selfClassMemberCompletions.size > 0) {
return [ ...selfClassMemberCompletions.values() ].filter((i) => i.label !== 'new');
return [...selfClassMemberCompletions.values()].filter((i) => i.label !== 'new');
}

if (!this.getClassFromMReference(position, currentToken, functionScope)) {
Expand Down Expand Up @@ -830,10 +830,10 @@ export class BrsFile {
let variables = functionScope.variableDeclarations;
for (let variable of variables) {
//skip duplicate variable names
if (names[ variable.name.toLowerCase() ]) {
if (names[variable.name.toLowerCase()]) {
continue;
}
names[ variable.name.toLowerCase() ] = true;
names[variable.name.toLowerCase()] = true;
result.push({
label: variable.name,
kind: isFunctionType(variable.type) ? CompletionItemKind.Function : CompletionItemKind.Variable
Expand All @@ -846,10 +846,10 @@ export class BrsFile {
for (let stmt of namespaces) {
let firstPart = stmt.nameExpression.getNameParts().shift();
//skip duplicate namespace names
if (names[ firstPart.toLowerCase() ]) {
if (names[firstPart.toLowerCase()]) {
continue;
}
names[ firstPart.toLowerCase() ] = true;
names[firstPart.toLowerCase()] = true;
result.push({
label: firstPart,
kind: CompletionItemKind.Module
Expand All @@ -874,7 +874,7 @@ export class BrsFile {
if (classStatement) {
let classes = scope.getClassHierarchy(classStatement.item.getName(ParseMode.BrighterScript).toLowerCase());
for (let cs of classes) {
for (let member of [ ...cs?.item?.fields ?? [], ...cs?.item?.methods ?? [] ]) {
for (let member of [...cs?.item?.fields ?? [], ...cs?.item?.methods ?? []]) {
if (!results.has(member.name.text.toLowerCase())) {
results.set(member.name.text.toLowerCase(), {
label: member.name.text,
Expand Down Expand Up @@ -903,15 +903,15 @@ export class BrsFile {
return [];
}
let results = new Map<string, CompletionItem>();
let completionName = this.getPartialVariableName(currentToken, [ TokenKind.New ])?.toLowerCase();
let completionName = this.getPartialVariableName(currentToken, [TokenKind.New])?.toLowerCase();
if (completionName?.includes('.')) {
return [];
}
let scopes = this.program.getScopesForFile(this);
for (let scope of scopes) {
let classMap = scope.getClassMap();
// let viableKeys = [...classMap.keys()].filter((k) => k.startsWith(completionName));
for (const key of [ ...classMap.keys() ]) {
for (const key of [...classMap.keys()]) {
let cs = classMap.get(key).item;
if (!results.has(cs.name.text)) {
results.set(cs.name.text, {
Expand All @@ -921,7 +921,7 @@ export class BrsFile {
}
}
}
return [ ...results.values() ];
return [...results.values()];
}

private getEnumStatementCompletions(currentToken: Token, parseMode: ParseMode): CompletionItem[] {
Expand All @@ -933,7 +933,7 @@ export class BrsFile {
let scopes = this.program.getScopesForFile(this);
for (let scope of scopes) {
let enumMap = scope.getEnumMap();
for (const key of [ ...enumMap.keys() ]) {
for (const key of [...enumMap.keys()]) {
let es = enumMap.get(key).item;
if (es.fullName.startsWith(completionName)) {

Expand All @@ -946,7 +946,7 @@ export class BrsFile {
}
}
}
return [ ...results.values() ];
return [...results.values()];
}
private getEnumMemberStatementCompletions(currentToken: Token, parseMode: ParseMode): CompletionItem[] {
if (parseMode === ParseMode.BrightScript) {
Expand All @@ -957,12 +957,12 @@ export class BrsFile {
let scopes = this.program.getScopesForFile(this);
for (let scope of scopes) {
let enumMap = scope.getEnumMap();
for (const key of [ ...enumMap.keys() ]) {
for (const key of [...enumMap.keys()]) {
let es = enumMap.get(key).item;
if (completionName.startsWith(es.fullName) && completionName.length > es.fullName.length) {

for (const esm of es.getMembers()) {
const name = es.fullName + "." + esm.name;
const name = es.fullName + '.' + esm.name;
if (name.startsWith(completionName)) {
if (!results.has(name)) {
results.set(name, {
Expand All @@ -976,7 +976,7 @@ export class BrsFile {
}
}
}
return [ ...results.values() ];
return [...results.values()];
}

private getNamespaceCompletions(currentToken: Token, parseMode: ParseMode, scope: Scope): CompletionItem[] {
Expand All @@ -985,7 +985,7 @@ export class BrsFile {
return [];
}

let completionName = this.getPartialVariableName(currentToken, [ TokenKind.New ]);
let completionName = this.getPartialVariableName(currentToken, [TokenKind.New]);
if (!completionName) {
return [];
}
Expand All @@ -995,15 +995,15 @@ export class BrsFile {
let newToken = this.getTokenBefore(currentToken, TokenKind.New);

let result = new Map<string, CompletionItem>();
for (let [ , namespace ] of scope.namespaceLookup) {
for (let [, namespace] of scope.namespaceLookup) {
//completionName = "NameA."
//completionName = "NameA.Na
//NameA
//NameA.NameB
//NameA.NameB.NameC
if (namespace.fullName.toLowerCase() === closestParentNamespaceName.toLowerCase()) {
//add all of this namespace's immediate child namespaces, bearing in mind if we are after a new keyword
for (let [ , ns ] of namespace.namespaces) {
for (let [, ns] of namespace.namespaces) {
if (!newToken || ns.statements.find((s) => isClassStatement(s))) {
if (!result.has(ns.lastPartName)) {
result.set(ns.lastPartName, {
Expand Down Expand Up @@ -1051,7 +1051,7 @@ export class BrsFile {

}
}
return [ ...result.values() ];
return [...result.values()];
}

private getNamespaceDefinitions(token: Token, file: BrsFile): Location {
Expand All @@ -1061,8 +1061,8 @@ export class BrsFile {
}
let location;

const nameParts = this.getPartialVariableName(token, [ TokenKind.New ]).split('.');
const endName = nameParts[ nameParts.length - 1 ].toLowerCase();
const nameParts = this.getPartialVariableName(token, [TokenKind.New]).split('.');
const endName = nameParts[nameParts.length - 1].toLowerCase();
const namespaceName = nameParts.slice(0, -1).join('.').toLowerCase();

const statementHandler = (statement: NamespaceStatement) => {
Expand Down Expand Up @@ -1096,13 +1096,13 @@ export class BrsFile {
* Given a current token, walk
*/
public getPartialVariableName(currentToken: Token, excludeTokens: TokenKind[] = null) {
let identifierAndDotKinds = [ TokenKind.Identifier, ...AllowedLocalIdentifiers, TokenKind.Dot ];
let identifierAndDotKinds = [TokenKind.Identifier, ...AllowedLocalIdentifiers, TokenKind.Dot];

//consume tokens backwards until we find something other than a dot or an identifier
let tokens = [];
const parser = this.parser;
for (let i = parser.tokens.indexOf(currentToken); i >= 0; i--) {
currentToken = parser.tokens[ i ];
currentToken = parser.tokens[i];
if (identifierAndDotKinds.includes(currentToken.kind) && (!excludeTokens || !excludeTokens.includes(currentToken.kind))) {
tokens.unshift(currentToken.text);
} else {
Expand Down Expand Up @@ -1140,7 +1140,7 @@ export class BrsFile {
private getTokenBefore(currentToken: Token, tokenKind: TokenKind): Token {
const index = this.parser.tokens.indexOf(currentToken);
for (let i = index - 1; i >= 0; i--) {
currentToken = this.parser.tokens[ i ];
currentToken = this.parser.tokens[i];
if (currentToken.kind === TokenKind.Newline) {
break;
} else if (currentToken.kind === tokenKind) {
Expand All @@ -1153,15 +1153,15 @@ export class BrsFile {
private tokenFollows(currentToken: Token, tokenKind: TokenKind): boolean {
const index = this.parser.tokens.indexOf(currentToken);
if (index > 0) {
return this.parser.tokens[ index - 1 ].kind === tokenKind;
return this.parser.tokens[index - 1].kind === tokenKind;
}
return false;
}

public getTokensUntil(currentToken: Token, tokenKind: TokenKind, direction: -1 | 1 = -1) {
let tokens = [];
for (let i = this.parser.tokens.indexOf(currentToken); direction === -1 ? i >= 0 : i === this.parser.tokens.length; i += direction) {
currentToken = this.parser.tokens[ i ];
currentToken = this.parser.tokens[i];
if (currentToken.kind === TokenKind.Newline || currentToken.kind === tokenKind) {
break;
}
Expand All @@ -1173,7 +1173,7 @@ export class BrsFile {
public getPreviousToken(token: Token) {
const parser = this.parser;
let idx = parser.tokens.indexOf(token);
return parser.tokens[ idx - 1 ];
return parser.tokens[idx - 1];
}

/**
Expand Down Expand Up @@ -1210,7 +1210,7 @@ export class BrsFile {
let scopes = this.program.getScopesForFile(this);
for (let scope of scopes) {
let namespace = scope.namespaceLookup.get(namespaceName.toLowerCase());
if (namespace.functionStatements[ lowerCalleeName ]) {
if (namespace.functionStatements[lowerCalleeName]) {
return true;
}
}
Expand All @@ -1227,19 +1227,19 @@ export class BrsFile {
public getClosestToken(position: Position) {
let tokens = this.parser.tokens;
for (let i = 0; i < tokens.length; i++) {
let token = tokens[ i ];
let token = tokens[i];
if (util.rangeContains(token.range, position)) {
return token;
}
//if the position less than this token range, then this position touches no token,
if (util.positionIsGreaterThanRange(position, token.range) === false) {
let t = tokens[ i - 1 ];
let t = tokens[i - 1];
//return the token or the first token
return t ? t : tokens[ 0 ];
return t ? t : tokens[0];
}
}
//return the last token
return tokens[ tokens.length - 1 ];
return tokens[tokens.length - 1];
}

/**
Expand Down Expand Up @@ -1397,7 +1397,7 @@ export class BrsFile {
let cs = this.parser.references.classStatements.find((cs) => cs.classKeyword.range === classToken.range);
if (cs?.parentClassName) {
const nameParts = cs.parentClassName.getNameParts();
let extendedClass = this.getClassFileLink(nameParts[ nameParts.length - 1 ], nameParts.slice(0, -1).join('.'));
let extendedClass = this.getClassFileLink(nameParts[nameParts.length - 1], nameParts.slice(0, -1).join('.'));
if (extendedClass) {
results.push(Location.create(util.pathToUri(extendedClass.file.pathAbsolute), extendedClass.item.range));
}
Expand Down Expand Up @@ -1565,7 +1565,7 @@ export class BrsFile {
return [];
}
let resultsMap = new Map<string, SignatureInfoObj>();
for (let [ , namespace ] of scope.namespaceLookup) {
for (let [, namespace] of scope.namespaceLookup) {
//completionName = "NameA."
//completionName = "NameA.Na
//NameA
Expand All @@ -1585,7 +1585,7 @@ export class BrsFile {
}
}

return [ ...resultsMap.values() ];
return [...resultsMap.values()];
}

public getSignatureHelpForStatement(statement: Statement): SignatureInfoObj {
Expand Down Expand Up @@ -1620,7 +1620,7 @@ export class BrsFile {
continue;
}
// if we already had a new line as the last token then exit out
if (functionComments[ 0 ] === currentToken.text) {
if (functionComments[0] === currentToken.text) {
break;
}
functionComments.unshift(currentToken.text);
Expand Down Expand Up @@ -1664,7 +1664,7 @@ export class BrsFile {
}
if (walkParents && classStatement.parentClassName) {
const nameParts = classStatement.parentClassName.getNameParts();
classStatement = this.getClassFileLink(nameParts[ nameParts.length - 1 ], nameParts.slice(0, -1).join('.'))?.item;
classStatement = this.getClassFileLink(nameParts[nameParts.length - 1], nameParts.slice(0, -1).join('.'))?.item;
} else {
break;
}
Expand Down

0 comments on commit d00fae9

Please sign in to comment.