Skip to content

Commit

Permalink
Fixes for release v0.66.0-alpha1
Browse files Browse the repository at this point in the history
  • Loading branch information
markwpearce committed Jun 13, 2023
1 parent 53f8f9c commit cad6866
Show file tree
Hide file tree
Showing 14 changed files with 1,838 additions and 220 deletions.
1,958 changes: 1,783 additions & 175 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@types/node": "^14.6.0",
"@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1",
"brighterscript": "../brighterscript",
"brighterscript": "0.66.0-alpha.1",
"chai": "^4.3.6",
"coveralls": "^3.1.1",
"eslint": "^7.7.0",
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/checkUsage/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { BsDiagnostic, Program } from 'brighterscript';
import { AfterProgramCreateEvent, BsDiagnostic } from 'brighterscript';
import Linter from '../../Linter';
import { createContext, PluginWrapperContext } from '../../util';
import CheckUsage from './index';
Expand All @@ -26,7 +26,8 @@ describe('checkUsage', () => {
linter = new Linter();
linter.builder.plugins.add({
name: 'test',
afterProgramCreate: (program: Program) => {
afterProgramCreate: (event: AfterProgramCreateEvent) => {
const { program } = event;
lintContext = createContext(program);
const checkUsage = new CheckUsage(lintContext);
program.plugins.add(checkUsage);
Expand Down
27 changes: 15 additions & 12 deletions src/plugins/checkUsage/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BscFile, CallableContainerMap, createVisitor, DiagnosticSeverity, isBrsFile, isXmlFile, Program, Range, Scope, TokenKind, WalkMode, XmlFile } from 'brighterscript';
import { AfterFileValidateEvent, AfterProgramValidateEvent, AfterScopeValidateEvent, BscFile, CompilerPlugin, createVisitor, DiagnosticSeverity, isBrsFile, isXmlFile, Range, TokenKind, WalkMode, XmlFile } from 'brighterscript';
import { SGNode } from 'brighterscript/dist/parser/SGTypes';
import { PluginContext } from '../../util';

Expand All @@ -9,7 +9,7 @@ export enum UnusedCode {
UnusedScript = 'LINT4002'
}

export default class CheckUsage {
export default class CheckUsage implements CompilerPlugin {

name = 'checkUsage';

Expand Down Expand Up @@ -39,16 +39,16 @@ export default class CheckUsage {

private walkChildren(v: Vertice, children: SGNode[], file: BscFile) {
children.forEach(node => {
const name = node.tag?.text;
const name = node.tagName;
if (name) {
v.edges.push(createComponentEdge(name, node.tag.range, file));
v.edges.push(createComponentEdge(name, node.tokens.startTagName.range, file));
}
const itemComponentName = node.getAttribute('itemcomponentname');
if (itemComponentName) {
v.edges.push(createComponentEdge(itemComponentName.value.text, itemComponentName.value.range, file));
v.edges.push(createComponentEdge(itemComponentName.value, itemComponentName.range, file));
}
if (node.children) {
this.walkChildren(v, node.children, file);
if (node.elements) {
this.walkChildren(v, node.elements, file);
}
});
}
Expand All @@ -69,7 +69,8 @@ export default class CheckUsage {
});
}

afterFileValidate(file: BscFile) {
afterFileValidate(event: AfterFileValidateEvent) {
const { file } = event;
// collect all XML components
if (isXmlFile(file)) {
if (!file.componentName) {
Expand Down Expand Up @@ -100,14 +101,16 @@ export default class CheckUsage {
v.edges.push(createComponentEdge(text, range, file));
}

const children = file.ast.component?.children;
const children = file.ast.componentElement?.childrenElement;
if (children) {
this.walkChildren(v, children.children, file);
this.walkChildren(v, children.elements, file);
}
}
}

afterScopeValidate(scope: Scope, files: BscFile[], _: CallableContainerMap) {
afterScopeValidate(event: AfterScopeValidateEvent) {
const { scope } = event;
const files = scope.getAllFiles();
const pkgPath = scope.name.toLowerCase();
let v: Vertice;
if (scope.name === 'global') {
Expand Down Expand Up @@ -180,7 +183,7 @@ export default class CheckUsage {
});
}

afterProgramValidate(_: Program) {
afterProgramValidate(_: AfterProgramValidateEvent) {
if (!this.main) {
throw new Error('No `main.brs`');
}
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/codeStyle/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import { expect } from 'chai';
import { AALiteralExpression, AssignmentStatement, BsDiagnostic, ParseMode, Parser, Program, util } from 'brighterscript';
import { AALiteralExpression, AfterProgramCreateEvent, AssignmentStatement, BsDiagnostic, ParseMode, Parser, util } from 'brighterscript';
import Linter from '../../Linter';
import CodeStyle, { collectWrappingAAMembersIndexes } from './index';
import { createContext, PluginWrapperContext } from '../../util';
Expand Down Expand Up @@ -28,7 +28,8 @@ describe('codeStyle', () => {
linter = new Linter();
linter.builder.plugins.add({
name: 'test',
afterProgramCreate: (program: Program) => {
afterProgramCreate: (event: AfterProgramCreateEvent) => {
const { program } = event;
lintContext = createContext(program);
const codeStyle = new CodeStyle(lintContext);
program.plugins.add(codeStyle);
Expand Down
9 changes: 5 additions & 4 deletions src/plugins/codeStyle/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BscFile, BsDiagnostic, createVisitor, FunctionExpression, isBrsFile, isGroupingExpression, TokenKind, WalkMode, CancellationTokenSource, DiagnosticSeverity, OnGetCodeActionsEvent, isCommentStatement, AALiteralExpression, AAMemberExpression, SymbolTypeFlags, isVoidType } from 'brighterscript';
import { BsDiagnostic, createVisitor, FunctionExpression, isBrsFile, isGroupingExpression, TokenKind, WalkMode, CancellationTokenSource, DiagnosticSeverity, OnGetCodeActionsEvent, isCommentStatement, AALiteralExpression, AAMemberExpression, SymbolTypeFlag, isVoidType, CompilerPlugin, AfterFileValidateEvent } from 'brighterscript';
import { RuleAAComma } from '../..';
import { addFixesToEvent } from '../../textEdit';
import { PluginContext } from '../../util';
import { messages } from './diagnosticMessages';
import { extractFixes } from './styleFixes';

export default class CodeStyle {
export default class CodeStyle implements CompilerPlugin {

name: 'codeStyle';

Expand All @@ -17,7 +17,8 @@ export default class CodeStyle {
extractFixes(addFixes, event.diagnostics);
}

afterFileValidate(file: BscFile) {
afterFileValidate(event: AfterFileValidateEvent) {
const { file } = event;
if (!isBrsFile(file) || this.lintContext.ignores(file)) {
return;
}
Expand Down Expand Up @@ -241,7 +242,7 @@ export default class CodeStyle {
getFunctionReturns(fun: FunctionExpression) {
let hasReturnedValue = false;
if (fun.returnTypeExpression) {
hasReturnedValue = !isVoidType(fun.returnTypeExpression.getType({ flags: SymbolTypeFlags.typetime }));
hasReturnedValue = !isVoidType(fun.returnTypeExpression.getType({ flags: SymbolTypeFlag.typetime }));
} else {
const cancel = new CancellationTokenSource();
fun.body.walk(createVisitor({
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/codeStyle/styleFixes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BscFile, BsDiagnostic, FunctionExpression, GroupingExpression, IfStatement, isIfStatement, isVoidType, Position, Range, SymbolTypeFlags, 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';
Expand Down Expand Up @@ -136,7 +136,7 @@ function replaceFunctionTokens(diagnostic: BsDiagnostic, token: string) {
replaceText(fun.end?.range, `end${space}${token}`)
];
// remove `as void` in case of `sub`
const returnType = fun.returnTypeExpression?.getType({ flags: SymbolTypeFlags.typetime }) ?? VoidType.instance;
const returnType = fun.returnTypeExpression?.getType({ flags: SymbolTypeFlag.typetime }) ?? VoidType.instance;
const returnChanges = token === 'sub' && fun.returnTypeExpression && isVoidType(returnType) ? [
replaceText(Range.create(fun.rightParen.range.end, fun.returnTypeExpression.range.end), '')
] : [];
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/trackCodeFlow/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import { expect } from 'chai';
import { BsDiagnostic, Program } from 'brighterscript';
import { AfterProgramCreateEvent, BsDiagnostic } from 'brighterscript';
import Linter from '../../Linter';
import TrackCodeFlow from './index';
import { createContext, PluginWrapperContext } from '../../util';
Expand All @@ -27,7 +27,8 @@ describe('trackCodeFlow', () => {
linter = new Linter();
linter.builder.plugins.add({
name: 'test',
afterProgramCreate: (program: Program) => {
afterProgramCreate: (event: AfterProgramCreateEvent) => {
const { program } = event;
lintContext = createContext(program);
const trackCodeFlow = new TrackCodeFlow(lintContext);
program.plugins.add(trackCodeFlow);
Expand Down
13 changes: 8 additions & 5 deletions src/plugins/trackCodeFlow/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BscFile, Scope, BsDiagnostic, CallableContainerMap, BrsFile, OnGetCodeActionsEvent, Statement, EmptyStatement, FunctionExpression, isForEachStatement, isForStatement, isIfStatement, isWhileStatement, Range, createStackedVisitor, isBrsFile, isStatement, isExpression, WalkMode, isTryCatchStatement, isCatchStatement } 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 } from 'brighterscript';
import { PluginContext } from '../../util';
import { createReturnLinter } from './returnTracking';
import { createVarLinter, resetVarContext, runDeferredValidation } from './varTracking';
Expand Down Expand Up @@ -50,7 +50,7 @@ export interface LintState {
branch?: StatementInfo;
}

export default class TrackCodeFlow {
export default class TrackCodeFlow implements CompilerPlugin {

name: 'trackCodeFlow';

Expand All @@ -62,12 +62,15 @@ export default class TrackCodeFlow {
extractFixes(addFixes, event.diagnostics);
}

afterScopeValidate(scope: Scope, files: BscFile[], callables: CallableContainerMap) {
const diagnostics = runDeferredValidation(this.lintContext, scope, files, callables);
afterScopeValidate(event: AfterScopeValidateEvent) {
const { scope } = event;
const callablesMap = util.getCallableContainersByLowerName(scope.getAllCallables());
const diagnostics = runDeferredValidation(this.lintContext, scope, scope.getAllFiles(), callablesMap);
scope.addDiagnostics(diagnostics);
}

afterFileValidate(file: BscFile) {
afterFileValidate(event: AfterFileValidateEvent) {
const { file } = event;
if (!isBrsFile(file) || this.lintContext.ignores(file)) {
return;
}
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 { BscFile, FunctionExpression, BsDiagnostic, isCommentStatement, DiagnosticTag, isReturnStatement, isIfStatement, isThrowStatement, TokenKind, util, ReturnStatement, ThrowStatement, isTryCatchStatement, isCatchStatement, isVoidType, SymbolTypeFlags } 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 Down Expand Up @@ -98,7 +98,7 @@ export function createReturnLinter(
const funRange = util.createRangeFromPositions(funRangeStart, funRangeEnd);

// Explicit `as void` or `sub` without return type should never return a value
const returnType = fun.returnTypeExpression?.getType({ flags: SymbolTypeFlags.typetime });
const returnType = fun.returnTypeExpression?.getType({ flags: SymbolTypeFlag.typetime });

if (
isVoidType(returnType) ||
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/trackCodeFlow/varTracking.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BscFile, FunctionExpression, BsDiagnostic, Range, isForStatement, isForEachStatement, isIfStatement, isAssignmentStatement, isNamespaceStatement, NamespaceStatement, Expression, isVariableExpression, isBinaryExpression, TokenKind, Scope, CallableContainerMap, DiagnosticSeverity, isLiteralInvalid, isWhileStatement, isClassMethodStatement, isBrsFile, isCatchStatement, isLabelStatement, isGotoStatement, ParseMode } from 'brighterscript';
import { BscFile, FunctionExpression, BsDiagnostic, Range, isForStatement, isForEachStatement, isIfStatement, isAssignmentStatement, isNamespaceStatement, NamespaceStatement, Expression, isVariableExpression, isBinaryExpression, TokenKind, Scope, CallableContainerMap, DiagnosticSeverity, isLiteralInvalid, isWhileStatement, isMethodStatement, isBrsFile, isCatchStatement, isLabelStatement, isGotoStatement, ParseMode } from 'brighterscript';
import { LintState, StatementInfo, NarrowingInfo, VarInfo, VarRestriction } from '.';
import { PluginContext } from '../../util';

Expand Down Expand Up @@ -27,11 +27,11 @@ interface ValidationInfo {
const deferredValidation: Map<string, ValidationInfo[]> = new Map();

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

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

export function createVarLinter(
Expand All @@ -52,7 +52,7 @@ export function createVarLinter(
args.set(name.toLowerCase(), { name: name, range: p.name.range, isParam: true, isUnsafe: false, isUsed: false });
});

if (isClassMethodStatement(fun.functionStatement)) {
if (isMethodStatement(fun.functionStatement)) {
args.set('super', { name: 'super', range: null, isParam: true, isUnsafe: false, isUsed: true });
}

Expand Down Expand Up @@ -383,7 +383,7 @@ export function runDeferredValidation(

const diagnostics: BsDiagnostic[] = [];
files.forEach((file) => {
const deferred = deferredValidation.get(file.pathAbsolute);
const deferred = deferredValidation.get(file.srcPath);
if (deferred) {
deferredVarLinter(scope, file, callables, globals, deferred, diagnostics);
}
Expand Down
6 changes: 3 additions & 3 deletions src/textEdit.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { Range } from 'vscode-languageserver-types';
import { Range } from 'vscode-languageserver';
import { applyEdits, compareRanges, getLineOffsets } from './textEdit';

describe('sortByRange', () => {
Expand All @@ -8,8 +8,8 @@ describe('sortByRange', () => {
const bOK = { range: Range.create(1, 0, 1, 0) };
const aKO = { range: null };
const bKO = { range: null };
const aKO2 = { range: { } };
const bKO2 = { range: { } };
const aKO2 = { range: {} };
const bKO2 = { range: {} };
const aKO3 = { range: { start: {} } };
const bKO3 = { range: { start: {} } };
expect(compareRanges(null, null)).equals(0);
Expand Down
2 changes: 1 addition & 1 deletion src/textEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export function addFixesToEvent(event: OnGetCodeActionsEvent) {
return (file: BscFile, entry: ChangeEntry) => {
const changes: LintCodeAction[] = entry.changes.map(change => ({
type: 'replace',
filePath: file.pathAbsolute,
filePath: file.srcPath,
range: change.range,
newText: change.text
}));
Expand Down
8 changes: 4 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ export function createContext(program: Program): PluginWrapperContext {
todoPattern: rules['todo-pattern'] ? new RegExp(rules['todo-pattern']) : /TODO|todo|FIXME/,
globals,
ignores: (file: BscFile) => {
return !file || ignorePatterns.some(pattern => minimatch(file.pathAbsolute, pattern));
return !file || ignorePatterns.some(pattern => minimatch(file.srcPath, pattern));
},
fix,
checkUsage,
addFixes: (file: BscFile, entry: ChangeEntry) => {
if (!pendingFixes.has(file.pathAbsolute)) {
pendingFixes.set(file.pathAbsolute, entry.changes);
if (!pendingFixes.has(file.srcPath)) {
pendingFixes.set(file.srcPath, entry.changes);
} else {
pendingFixes.get(file.pathAbsolute).push(...entry.changes);
pendingFixes.get(file.srcPath).push(...entry.changes);
}
},
applyFixes: () => addJob(applyFixes(fix, pendingFixes)),
Expand Down

0 comments on commit cad6866

Please sign in to comment.