Skip to content

Commit

Permalink
Upgrade to bsc 1.0.0-alpha.25
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Jan 26, 2024
1 parent 0708eb5 commit cc21c6d
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 51 deletions.
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@types/node": "^14.6.0",
"@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1",
"brighterscript": "^0.66.0-alpha.6",
"brighterscript": "^1.0.0-alpha.25",
"chai": "^4.3.6",
"coveralls": "^3.1.1",
"eslint": "^7.7.0",
Expand All @@ -60,7 +60,7 @@
"typescript": "^4.9.4"
},
"peerDependencies": {
"brighterscript": ">= 0.66.0-alpha.6 < 1"
"brighterscript": "^1.0.0-alpha.25"
},
"mocha": {
"spec": "src/**/*.spec.ts",
Expand Down
29 changes: 21 additions & 8 deletions src/plugins/checkUsage/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AfterFileValidateEvent, AfterProgramValidateEvent, AfterScopeValidateEvent, File, CompilerPlugin, createVisitor, DiagnosticSeverity, isBrsFile, isXmlFile, Range, TokenKind, WalkMode, XmlFile } from 'brighterscript';
import { AfterFileValidateEvent, AfterProgramValidateEvent, AfterScopeValidateEvent, CompilerPlugin, createVisitor, DiagnosticSeverity, isBrsFile, isXmlFile, Range, TokenKind, WalkMode, XmlFile, FunctionExpression, BscFile, isFunctionExpression, Cache } from 'brighterscript';
import { SGNode } from 'brighterscript/dist/parser/SGTypes';
import { PluginContext } from '../../util';

Expand Down Expand Up @@ -37,7 +37,7 @@ export default class CheckUsage implements CompilerPlugin {
this.walked = walked;
}

private walkChildren(v: Vertice, children: SGNode[], file: File) {
private walkChildren(v: Vertice, children: SGNode[], file: BscFile) {
children.forEach(node => {
const name = node.tagName;
if (name) {
Expand Down Expand Up @@ -108,6 +108,12 @@ export default class CheckUsage implements CompilerPlugin {
}
}

private functionExpressionCache = new Cache<BscFile, FunctionExpression[]>();

beforeProgramValidate() {
this.functionExpressionCache.clear();
}

afterScopeValidate(event: AfterScopeValidateEvent) {
const { scope } = event;
const files = scope.getAllFiles();
Expand Down Expand Up @@ -159,9 +165,16 @@ export default class CheckUsage implements CompilerPlugin {
if (pkgPath === 'source/main.brs' || pkgPath === 'source/main.bs') {
this.main = fv;
}

// look up all function expressions exactly 1 time for this file, even if it's used across many scopes
const functionExpressions = this.functionExpressionCache.getOrAdd(file, () => {
return file.parser.ast.findChildren<FunctionExpression>(isFunctionExpression, { walkMode: WalkMode.visitExpressionsRecursive });
});


// find strings that look like referring to component names
file.parser.references.functionExpressions.forEach(fun => {
fun.body.walk(createVisitor({
for (const func of functionExpressions) {
func.body.walk(createVisitor({
LiteralExpression: (e) => {
const { kind } = e.token;
if (kind === TokenKind.StringLiteral) {
Expand All @@ -179,7 +192,7 @@ export default class CheckUsage implements CompilerPlugin {
}
}
}), { walkMode: WalkMode.visitExpressions });
});
}
});
}

Expand Down Expand Up @@ -220,7 +233,7 @@ function normalizePath(s: string) {
return p;
}

function createComponentEdge(name: string, range: Range = null, file: File = null) {
function createComponentEdge(name: string, range: Range = null, file: BscFile = null) {
return {
name: `"${name.toLowerCase()}"`,
range,
Expand All @@ -230,13 +243,13 @@ function createComponentEdge(name: string, range: Range = null, file: File = nul

interface Vertice {
name: string;
file: File;
file: BscFile;
edges: Edge[];
used?: boolean;
}

interface Edge {
name: string;
file?: File;
file?: BscFile;
range?: Range;
}
12 changes: 5 additions & 7 deletions src/plugins/codeStyle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export default class CodeStyle implements CompilerPlugin {
const validateCondition = conditionStyle !== 'off';
const requireConditionGroup = conditionStyle === 'group';
const validateAAStyle = aaCommaStyle !== 'off';
const walkExpressions = validateAAStyle || validateColorFormat;
const validateEolLast = eolLast !== 'off';
const disallowEolLast = eolLast === 'never';
const validateColorStyle = validateColorFormat ? createColorValidator(severity) : undefined;
Expand Down Expand Up @@ -85,6 +84,10 @@ export default class CodeStyle implements CompilerPlugin {
}

file.ast.walk(createVisitor({
// validate function style (`function` or `sub`)
FunctionExpression: (func) => {
this.validateFunctionStyle(func, diagnostics);
},
IfStatement: s => {
const hasThenToken = !!s.tokens.then;
if (!s.isInline && validateBlockIf) {
Expand Down Expand Up @@ -157,12 +160,7 @@ export default class CodeStyle implements CompilerPlugin {
diagnostics.push(messages.noStop(s.tokens.stop.range, noStop));
}
}
}), { walkMode: walkExpressions ? WalkMode.visitAllRecursive : WalkMode.visitStatementsRecursive });

// validate function style (`function` or `sub`)
for (const fun of file.parser.references.functionExpressions) {
this.validateFunctionStyle(fun, diagnostics);
}
}), { walkMode: WalkMode.visitAllRecursive });

// add file reference
let bsDiagnostics: BsDiagnostic[] = diagnostics.map(diagnostic => ({
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/codeStyle/styleFixes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { File, BsDiagnostic, FunctionExpression, GroupingExpression, IfStatement, isIfStatement, isVoidType, Position, Range, SymbolTypeFlag, VoidType, WhileStatement } from 'brighterscript';
import { BscFile, BsDiagnostic, FunctionExpression, GroupingExpression, IfStatement, isIfStatement, isVoidType, Position, Range, SymbolTypeFlag, VoidType, WhileStatement } from 'brighterscript';
import { ChangeEntry, comparePos, insertText, replaceText } from '../../textEdit';
import { CodeStyleError } from './diagnosticMessages';
import { platform } from 'process';

export function extractFixes(
addFixes: (file: File, changes: ChangeEntry) => void,
addFixes: (file: BscFile, changes: ChangeEntry) => void,
diagnostics: BsDiagnostic[]
): BsDiagnostic[] {
return diagnostics.filter(diagnostic => {
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/trackCodeFlow/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BsDiagnostic, BrsFile, OnGetCodeActionsEvent, Statement, EmptyStatement, FunctionExpression, isForEachStatement, isForStatement, isIfStatement, isWhileStatement, Range, createStackedVisitor, isBrsFile, isStatement, isExpression, WalkMode, isTryCatchStatement, isCatchStatement, CompilerPlugin, AfterScopeValidateEvent, AfterFileValidateEvent, util } from 'brighterscript';
import { BsDiagnostic, BrsFile, OnGetCodeActionsEvent, Statement, EmptyStatement, FunctionExpression, isForEachStatement, isForStatement, isIfStatement, isWhileStatement, Range, createStackedVisitor, isBrsFile, isStatement, isExpression, WalkMode, isTryCatchStatement, isCatchStatement, CompilerPlugin, AfterScopeValidateEvent, AfterFileValidateEvent, util, isFunctionExpression } from 'brighterscript';
import { PluginContext } from '../../util';
import { createReturnLinter } from './returnTracking';
import { createVarLinter, resetVarContext, runDeferredValidation } from './varTracking';
Expand Down Expand Up @@ -78,7 +78,7 @@ export default class TrackCodeFlow implements CompilerPlugin {

resetVarContext(file);

file.parser.references.functionExpressions.forEach((fun) => {
for (const fun of file.parser.ast.findChildren<FunctionExpression>(isFunctionExpression, { walkMode: WalkMode.visitExpressionsRecursive })) {
const state: LintState = {
file: file,
fun: fun,
Expand Down Expand Up @@ -170,7 +170,7 @@ export default class TrackCodeFlow implements CompilerPlugin {
varLinter.closeBlock(block);
}
}
});
}

if (this.lintContext.fix) {
diagnostics = extractFixes(this.lintContext.addFixes, diagnostics);
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/trackCodeFlow/returnTracking.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { File, FunctionExpression, BsDiagnostic, isCommentStatement, DiagnosticTag, isReturnStatement, isIfStatement, isThrowStatement, TokenKind, util, ReturnStatement, ThrowStatement, isTryCatchStatement, isCatchStatement, isVoidType, SymbolTypeFlag } from 'brighterscript';
import { BscFile, FunctionExpression, BsDiagnostic, isCommentStatement, DiagnosticTag, isReturnStatement, isIfStatement, isThrowStatement, TokenKind, util, ReturnStatement, ThrowStatement, isTryCatchStatement, isCatchStatement, isVoidType, SymbolTypeFlag } from 'brighterscript';
import { LintState, StatementInfo } from '.';
import { PluginContext } from '../../util';

Expand All @@ -23,7 +23,7 @@ enum ReturnLintError {

export function createReturnLinter(
lintContext: PluginContext,
file: File,
file: BscFile,
fun: FunctionExpression,
state: LintState,
diagnostics: BsDiagnostic[]
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/trackCodeFlow/trackFixes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { File, BsDiagnostic, Range } from 'brighterscript';
import { BscFile, BsDiagnostic, Range } from 'brighterscript';
import { ChangeEntry, replaceText } from '../../textEdit';
import { VarLintError } from './varTracking';

export function extractFixes(
addFixes: (file: File, changes: ChangeEntry) => void,
addFixes: (file: BscFile, changes: ChangeEntry) => void,
diagnostics: BsDiagnostic[]
): BsDiagnostic[] {
return diagnostics.filter(diagnostic => {
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/trackCodeFlow/varTracking.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { File, FunctionExpression, BsDiagnostic, Range, isForStatement, isForEachStatement, isIfStatement, isAssignmentStatement, isNamespaceStatement, NamespaceStatement, Expression, isVariableExpression, isBinaryExpression, TokenKind, Scope, CallableContainerMap, DiagnosticSeverity, isLiteralInvalid, isWhileStatement, isCatchStatement, isLabelStatement, isGotoStatement, ParseMode, util, isMethodStatement, isTryCatchStatement } from 'brighterscript';
import { BscFile, FunctionExpression, BsDiagnostic, Range, isForStatement, isForEachStatement, isIfStatement, isAssignmentStatement, isNamespaceStatement, NamespaceStatement, Expression, isVariableExpression, isBinaryExpression, TokenKind, Scope, CallableContainerMap, DiagnosticSeverity, isLiteralInvalid, isWhileStatement, isCatchStatement, isLabelStatement, isGotoStatement, ParseMode, util, isMethodStatement, isTryCatchStatement } from 'brighterscript';
import { LintState, StatementInfo, NarrowingInfo, VarInfo, VarRestriction } from '.';
import { PluginContext } from '../../util';

Expand Down Expand Up @@ -26,17 +26,17 @@ interface ValidationInfo {

const deferredValidation: Map<string, ValidationInfo[]> = new Map();

function getDeferred(file: File) {
function getDeferred(file: BscFile) {
return deferredValidation.get(file.srcPath);
}

export function resetVarContext(file: File) {
export function resetVarContext(file: BscFile) {
deferredValidation.set(file.srcPath, []);
}

export function createVarLinter(
lintContext: PluginContext,
file: File,
file: BscFile,
fun: FunctionExpression,
state: LintState,
diagnostics: BsDiagnostic[]
Expand Down Expand Up @@ -376,7 +376,7 @@ export function createVarLinter(
export function runDeferredValidation(
lintContext: PluginContext,
scope: Scope,
files: File[],
files: BscFile[],
callables: CallableContainerMap
) {
const topLevelVars = buildTopLevelVars(scope, lintContext.globals);
Expand Down Expand Up @@ -415,7 +415,7 @@ function buildTopLevelVars(scope: Scope, globals: string[]) {

function deferredVarLinter(
scope: Scope,
file: File,
file: BscFile,
callables: CallableContainerMap,
toplevel: Set<string>,
deferred: ValidationInfo[],
Expand Down
4 changes: 2 additions & 2 deletions src/testHelpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BsDiagnostic, File, DiagnosticSeverity, DiagnosticTag, Range } from 'brighterscript';
import { BsDiagnostic, BscFile, DiagnosticSeverity, DiagnosticTag, Range } from 'brighterscript';
import { CodeDescription, DiagnosticRelatedInformation, Diagnostic } from 'vscode-languageserver-types';
import { expect } from 'chai';
import { firstBy } from 'thenby';
Expand Down Expand Up @@ -50,7 +50,7 @@ interface PartialDiagnostic {
tags?: Partial<DiagnosticTag>[];
relatedInformation?: Partial<DiagnosticRelatedInformation>[];
data?: unknown;
file?: Partial<File>;
file?: Partial<BscFile>;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/textEdit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync } from 'fs';
import { readFile, writeFile } from 'fs-extra';
import { File, BsDiagnostic, OnGetCodeActionsEvent, Position, Range } from 'brighterscript';
import { BscFile, BsDiagnostic, OnGetCodeActionsEvent, Position, Range } from 'brighterscript';
import { CodeActionKind, codeActionUtil } from 'brighterscript/dist/CodeActionUtil';

interface LintCodeAction {
Expand Down Expand Up @@ -125,7 +125,7 @@ export async function applyFixes(fix: boolean, pendingFixes: Map<string, TextEdi
}

export function addFixesToEvent(event: OnGetCodeActionsEvent) {
return (file: File, entry: ChangeEntry) => {
return (file: BscFile, entry: ChangeEntry) => {
const changes: LintCodeAction[] = entry.changes.map(change => ({
type: 'replace',
filePath: file.srcPath,
Expand Down
10 changes: 5 additions & 5 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as minimatch from 'minimatch';
import { BsLintConfig, BsLintRules, RuleSeverity, BsLintSeverity } from './index';
import { readFileSync, existsSync } from 'fs';
import * as path from 'path';
import { Program, File, DiagnosticSeverity } from 'brighterscript';
import { Program, BscFile, DiagnosticSeverity } from 'brighterscript';
import { applyFixes, ChangeEntry, TextEdit } from './textEdit';
import { addJob } from './Linter';

Expand Down Expand Up @@ -99,10 +99,10 @@ export interface PluginContext {
severity: Readonly<BsLintRules>;
todoPattern: Readonly<RegExp>;
globals: string[];
ignores: (file: File) => boolean;
ignores: (file: BscFile) => boolean;
fix: Readonly<boolean>;
checkUsage: Readonly<boolean>;
addFixes: (file: File, entry: ChangeEntry) => void;
addFixes: (file: BscFile, entry: ChangeEntry) => void;
}

export interface PluginWrapperContext extends PluginContext {
Expand All @@ -121,12 +121,12 @@ export function createContext(program: Program): PluginWrapperContext {
severity: rulesToSeverity(rules),
todoPattern: rules['todo-pattern'] ? new RegExp(rules['todo-pattern']) : /TODO|todo|FIXME/,
globals,
ignores: (file: File) => {
ignores: (file: BscFile) => {
return !file || ignorePatterns.some(pattern => minimatch(file.srcPath, pattern));
},
fix,
checkUsage,
addFixes: (file: File, entry: ChangeEntry) => {
addFixes: (file: BscFile, entry: ChangeEntry) => {
if (!pendingFixes.has(file.srcPath)) {
pendingFixes.set(file.srcPath, entry.changes);
} else {
Expand Down

0 comments on commit cc21c6d

Please sign in to comment.