Skip to content

Commit

Permalink
Updated to work with with Brighterscript v1.0.0-alpha.35 (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
markwpearce committed Aug 1, 2024
1 parent fa6a65c commit 8aa3b31
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 193 deletions.
60 changes: 21 additions & 39 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@types/node": "^14.6.0",
"@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1",
"brighterscript": "^1.0.0-alpha.34",
"brighterscript": "^1.0.0-alpha.35",
"chai": "^4.3.6",
"coveralls-next": "^4.2.0",
"eslint": "^7.7.0",
Expand Down
40 changes: 20 additions & 20 deletions src/createColorValidator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BsDiagnostic, Range } from 'brighterscript';
import { BsDiagnostic, Location } from 'brighterscript';
import { messages } from './plugins/codeStyle/diagnosticMessages';
import { BsLintRules, RuleColorFormat, RuleColorCase, RuleColorAlpha, RuleColorAlphaDefaults, RuleColorCertCompliant } from './index';

export function createColorValidator(severity: Readonly<BsLintRules>) {
const { colorFormat, colorCase, colorAlpha, colorAlphaDefaults, colorCertCompliant } = severity;
return (text, range, diagnostics) => {
return (text: string, location: Location, diagnostics: BsDiagnostic[]) => {
const len = text.length;
if (len < 7 || len > 12) {
// we're only interested in string length is between 7 (#EBEBEB) to 12 ("0xEBEBEBFF") chars long
Expand All @@ -17,68 +17,68 @@ export function createColorValidator(severity: Readonly<BsLintRules>) {
const quotedNumericHexMatches = (text.startsWith('0x') || text.startsWith('"0x')) ? text.match(quotedNumericHexRegex) : undefined;

if ((colorFormat === 'never') && (quotedNumericHexMatches || hashHexMatches)) {
diagnostics.push(messages.expectedColorFormat(range));
diagnostics.push(messages.expectedColorFormat(location));
return;
}
const hashHexAlphaRegex = /#[0-9A-Fa-f]{8}/g;
const quotedNumericHexAlphaRegex = /0x[0-9A-Fa-f]{8}/g;

if (colorFormat === 'hash-hex') {
if (quotedNumericHexMatches) {
diagnostics.push(messages.expectedColorFormat(range));
diagnostics.push(messages.expectedColorFormat(location));
}
validateColorCase(hashHexMatches, range, diagnostics, colorCase, colorFormat);
validateColorAlpha(text.match(hashHexAlphaRegex), hashHexMatches, quotedNumericHexMatches, range, diagnostics, colorAlpha, colorAlphaDefaults);
validateColorCertCompliance(hashHexMatches, range, diagnostics, colorFormat, colorCertCompliant);
validateColorCase(hashHexMatches, location, diagnostics, colorCase, colorFormat);
validateColorAlpha(text.match(hashHexAlphaRegex), hashHexMatches, quotedNumericHexMatches, location, diagnostics, colorAlpha, colorAlphaDefaults);
validateColorCertCompliance(hashHexMatches, location, diagnostics, colorFormat, colorCertCompliant);

} else if (colorFormat === 'quoted-numeric-hex') {
if (hashHexMatches) {
diagnostics.push(messages.expectedColorFormat(range));
diagnostics.push(messages.expectedColorFormat(location));
}
validateColorCase(quotedNumericHexMatches, range, diagnostics, colorCase, colorFormat);
validateColorAlpha(text.match(quotedNumericHexAlphaRegex), hashHexMatches, quotedNumericHexMatches, range, diagnostics, colorAlpha, colorAlphaDefaults);
validateColorCertCompliance(quotedNumericHexMatches, range, diagnostics, colorFormat, colorCertCompliant);
validateColorCase(quotedNumericHexMatches, location, diagnostics, colorCase, colorFormat);
validateColorAlpha(text.match(quotedNumericHexAlphaRegex), hashHexMatches, quotedNumericHexMatches, location, diagnostics, colorAlpha, colorAlphaDefaults);
validateColorCertCompliance(quotedNumericHexMatches, location, diagnostics, colorFormat, colorCertCompliant);
}
};
}

function validateColorAlpha(alphaMatches: RegExpMatchArray, hashMatches: RegExpMatchArray, quotedNumericHexMatches: RegExpMatchArray, range: Range, diagnostics: (Omit<BsDiagnostic, 'file'>)[], alpha: RuleColorAlpha, alphaDefaults: RuleColorAlphaDefaults) {
function validateColorAlpha(alphaMatches: RegExpMatchArray, hashMatches: RegExpMatchArray, quotedNumericHexMatches: RegExpMatchArray, location: Location, diagnostics: (BsDiagnostic)[], alpha: RuleColorAlpha, alphaDefaults: RuleColorAlphaDefaults) {
const validateColorAlpha = (alpha === 'never' || alpha === 'always' || alpha === 'allowed');
if (validateColorAlpha) {
if (alpha === 'never' && alphaMatches) {
diagnostics.push(messages.expectedColorAlpha(range));
diagnostics.push(messages.expectedColorAlpha(location));
}
if ((alpha === 'always' && alphaMatches === null) && (hashMatches || quotedNumericHexMatches)) {
diagnostics.push(messages.expectedColorAlpha(range));
diagnostics.push(messages.expectedColorAlpha(location));
}
if ((alphaDefaults === 'never' || alphaDefaults === 'only-hidden') && alphaMatches) {
for (let i = 0; i < alphaMatches.length; i++) {
const colorHashAlpha = alphaMatches[i];
const alphaValue = colorHashAlpha.slice(-2).toLowerCase();
if (alphaValue === 'ff' || (alphaDefaults === 'never' && alphaValue === '00')) {
diagnostics.push(messages.expectedColorAlphaDefaults(range));
diagnostics.push(messages.expectedColorAlphaDefaults(location));
}
}
}
}
}

function validateColorCase(matches: RegExpMatchArray, range: Range, diagnostics: (Omit<BsDiagnostic, 'file'>)[], colorCase: RuleColorCase, colorFormat: RuleColorFormat) {
function validateColorCase(matches: RegExpMatchArray, location: Location, diagnostics: (BsDiagnostic)[], colorCase: RuleColorCase, colorFormat: RuleColorFormat) {
const validateColorCase = colorCase === 'upper' || colorCase === 'lower';
if (validateColorCase && matches) {
let colorValue = matches[0];
const charsToStrip = (colorFormat === 'hash-hex') ? 1 : 2;
colorValue = colorValue.substring(charsToStrip);
if (colorCase === 'lower' && colorValue !== colorValue.toLowerCase()) {
diagnostics.push(messages.expectedColorCase(range));
diagnostics.push(messages.expectedColorCase(location));
}
if (colorCase === 'upper' && colorValue !== colorValue.toUpperCase()) {
diagnostics.push(messages.expectedColorCase(range));
diagnostics.push(messages.expectedColorCase(location));
}
}
}

function validateColorCertCompliance(matches: RegExpMatchArray, range: Range, diagnostics: (Omit<BsDiagnostic, 'file'>)[], colorFormat: RuleColorFormat, certCompliant: RuleColorCertCompliant) {
function validateColorCertCompliance(matches: RegExpMatchArray, location: Location, diagnostics: (BsDiagnostic)[], colorFormat: RuleColorFormat, certCompliant: RuleColorCertCompliant) {
const validateCertCompliant = certCompliant === 'always';
if (validateCertCompliant && matches) {
const BROADCAST_SAFE_BLACK = '161616';
Expand All @@ -90,7 +90,7 @@ function validateColorCertCompliance(matches: RegExpMatchArray, range: Range, di
colorValue = colorValue.substring(charsToStrip);
const colorLuma = getColorLuma(colorValue);
if (colorLuma > MAX_WHITE_LUMA || colorLuma < MAX_BLACK_LUMA) {
diagnostics.push(messages.colorCertCompliance(range));
diagnostics.push(messages.colorCertCompliance(location));
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/plugins/checkUsage/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AfterFileValidateEvent, AfterProgramValidateEvent, AfterScopeValidateEvent, CompilerPlugin, createVisitor, DiagnosticSeverity, isBrsFile, isXmlFile, Range, TokenKind, WalkMode, XmlFile, FunctionExpression, BscFile, isFunctionExpression, Cache } from 'brighterscript';
import { AfterFileValidateEvent, AfterProgramValidateEvent, AfterScopeValidateEvent, CompilerPlugin, createVisitor, DiagnosticSeverity, isBrsFile, isXmlFile, Range, TokenKind, WalkMode, XmlFile, FunctionExpression, BscFile, isFunctionExpression, Cache, util } from 'brighterscript';
import { SGNode } from 'brighterscript/dist/parser/SGTypes';
import { PluginContext } from '../../util';
import { BsLintDiagnosticContext } from '../../Linter';
Expand Down Expand Up @@ -209,16 +209,14 @@ export default class CheckUsage implements CompilerPlugin {
severity: DiagnosticSeverity.Warning,
code: UnusedCode.UnusedScript,
message: `Script '${v.file.pkgPath}' does not seem to be used`,
range: Range.create(0, 0, 1, 0),
file: v.file
location: util.createLocationFromFileRange(v.file, util.createRange(0, 0, 1, 0))
}, BsLintDiagnosticContext);
} else if (isXmlFile(v.file) && v.file.componentName?.location.range) {
v.file.program.diagnostics.register({
severity: DiagnosticSeverity.Warning,
code: UnusedCode.UnusedComponent,
message: `Component '${v.file.pkgPath}' does not seem to be used`,
range: v.file.componentName.location.range,
file: v.file
location: v.file.componentName.location
}, BsLintDiagnosticContext);
}
}
Expand Down
Loading

0 comments on commit 8aa3b31

Please sign in to comment.