Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Color format checking for bslint #94

Merged
merged 37 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
9b77bc6
Color format checking - first step/ example
charlie-abbott-deltatre Jul 27, 2023
504ae4a
Color format checking
charlie-abbott-deltatre Jul 27, 2023
4566bdd
Color format checking
charlie-abbott-deltatre Jul 27, 2023
b8ac58f
Color format, alpha, default alpha, case and color cert requirement c…
charlie-abbott-deltatre Jul 28, 2023
fcd7117
Adding alpha to luma check and removing todo
charlie-abbott-deltatre Jul 28, 2023
0fd23b3
Updating readme
charlie-abbott-deltatre Jul 28, 2023
994bb34
Moving color validate functions to util for use in multiple files
charlie-abbott-deltatre Jul 30, 2023
56247f9
Updating readme
charlie-abbott-deltatre Jul 30, 2023
a4e3941
Removing isXMLFile checking
charlie-abbott-deltatre Jul 30, 2023
d1036eb
Temp example for traversing to XML node arrtibute values
charlie-abbott-deltatre Jul 31, 2023
c2b5dd3
Moving XML color validation to codeStyle
charlie-abbott-deltatre Aug 2, 2023
a35513c
Moving XML color validation to codeStyle
charlie-abbott-deltatre Aug 2, 2023
5f9769b
Moving XML color validation to codeStyle
charlie-abbott-deltatre Aug 2, 2023
77563e5
Starting refactor
charlie-abbott-deltatre Aug 3, 2023
bac6d4c
Continuing refactor
charlie-abbott-deltatre Aug 4, 2023
abdcfc3
Continuing refactor
charlie-abbott-deltatre Aug 23, 2023
7e4c8fe
Continuing refactor
charlie-abbott-deltatre Sep 15, 2023
e615f8c
Merge branch 'master' into color-style-checking
TwitchBronBron Sep 18, 2023
a70a3bd
Removing XML file checks - will add these in a seperate PR
charlie-abbott-deltatre Sep 18, 2023
8b1a3f9
Continuing refactor
charlie-abbott-deltatre Sep 19, 2023
ec353ae
PR comment fixes
charlie-abbott-deltatre Sep 19, 2023
3c4426d
Continuing refactor
charlie-abbott-deltatre Sep 19, 2023
01d09ae
Continuing refactor
charlie-abbott-deltatre Sep 20, 2023
8143d12
Continuing refactor
charlie-abbott-deltatre Sep 20, 2023
ec2393e
Fixing build errors
charlie-abbott-deltatre Sep 23, 2023
0071fa8
Including non color string checks
charlie-abbott-deltatre Sep 23, 2023
6ba9eb8
Removing initial code for fixes. Due to add in a future PR
charlie-abbott-deltatre Sep 24, 2023
6699af0
Including non color string checks
charlie-abbott-deltatre Sep 24, 2023
fa402c4
Continuing refactor
charlie-abbott-deltatre Sep 24, 2023
64727f9
Move all test brs/bs code inline
TwitchBronBron Sep 25, 2023
283a1c6
Merge branch 'master' of https://github.com/rokucommunity/bslint into…
TwitchBronBron Sep 25, 2023
e22c357
Continuing refactor
charlie-abbott-deltatre Sep 25, 2023
a6b708d
Fix templatestring quasi handling
TwitchBronBron Sep 25, 2023
776ed2f
Merge branch 'color-style-checking' of https://github.com/disc7/bslin…
TwitchBronBron Sep 25, 2023
77a9ea0
Remove unused color test files
TwitchBronBron Sep 25, 2023
7b30db8
Removing breakpoint
charlie-abbott-deltatre Sep 25, 2023
2f20601
only validate string-like template strings
TwitchBronBron Sep 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type RuleFunction = 'no-function' | 'no-sub' | 'auto' | 'off';
export type RuleAAComma = 'always' | 'no-dangling' | 'never' | 'off';
export type RuleTypeAnnotations = 'all' | 'return' | 'args' | 'off';
export type RuleEolLast = 'always' | 'never' | 'off';
export type RuleColorFormat = 'hash' | 'zero-x' | 'off';

export type BsLintConfig = Pick<BsConfig, 'project' | 'rootDir' | 'files' | 'cwd' | 'watch'> & {
lintConfig?: string;
Expand All @@ -39,6 +40,7 @@ export type BsLintConfig = Pick<BsConfig, 'project' | 'rootDir' | 'files' | 'cwd
// Will be transformed to RegExp type when program context is created.
'todo-pattern'?: string;
'eol-last'?: RuleEolLast;
'color-format'?: RuleColorFormat;
TwitchBronBron marked this conversation as resolved.
Show resolved Hide resolved
};
globals?: string[];
ignores?: string[];
Expand Down Expand Up @@ -67,6 +69,7 @@ export interface BsLintRules {
noTodo: BsLintSeverity;
noStop: BsLintSeverity;
eolLast: RuleEolLast;
colorFormat: RuleColorFormat;
}

export { Linter };
Expand Down
10 changes: 9 additions & 1 deletion src/plugins/codeStyle/diagnosticMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export enum CodeStyleError {
NoTodo = 'LINT3015',
NoStop = 'LINT3016',
EolLastMissing = 'LINT3017',
EolLastFound = 'LINT3018'
EolLastFound = 'LINT3018',
ColorFormat = 'LINT3019'
}

const CS = 'Code style:';
Expand Down Expand Up @@ -159,5 +160,12 @@ export const messages = {
source: 'bslint',
message: `${CS} File should not end with a newline`,
range
}),
expectedColorFormat: (range: Range) => ({
severity: DiagnosticSeverity.Error,
code: CodeStyleError.ColorFormat,
source: 'bslint',
message: `${CS} File should follow color format`,
range
})
};
14 changes: 14 additions & 0 deletions src/plugins/codeStyle/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,20 @@ describe('codeStyle', () => {
expect(actualSrc).to.equal(expectedSrc);
});

it('color formatting should all be zero-x (0x) format', async () => {
const diagnostics = await linter.run({
...project1,
files: ['source/color-should-be-zero-x.brs'],
rules: {
'color-format': 'zero-x'
},
fix: true
});
const actual = fmtDiagnostics(diagnostics);
const expected = [];
expect(actual).deep.equal(expected);
});

it('add missing aa comma, no dangling', async () => {
const diagnostics = await linter.run({
...project1,
Expand Down
28 changes: 26 additions & 2 deletions src/plugins/codeStyle/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BscFile, BsDiagnostic, createVisitor, FunctionExpression, isBrsFile, isGroupingExpression, TokenKind, WalkMode, CancellationTokenSource, DiagnosticSeverity, OnGetCodeActionsEvent, isCommentStatement, AALiteralExpression, AAMemberExpression } from 'brighterscript';
import { RuleAAComma } from '../..';
import { RuleAAComma, RuleColorFormat } from '../..';
import { addFixesToEvent } from '../../textEdit';
import { PluginContext } from '../../util';
import { messages } from './diagnosticMessages';
Expand All @@ -24,11 +24,12 @@ export default class CodeStyle {

const diagnostics: (Omit<BsDiagnostic, 'file'>)[] = [];
const { severity, fix } = this.lintContext;
const { inlineIfStyle, blockIfStyle, conditionStyle, noPrint, noTodo, noStop, aaCommaStyle, eolLast } = severity;
const { inlineIfStyle, blockIfStyle, conditionStyle, noPrint, noTodo, noStop, aaCommaStyle, eolLast, colorFormat } = severity;
const validatePrint = noPrint !== DiagnosticSeverity.Hint;
const validateTodo = noTodo !== DiagnosticSeverity.Hint;
const validateNoStop = noStop !== DiagnosticSeverity.Hint;
const validateInlineIf = inlineIfStyle !== 'off';
const validateColorFormat = colorFormat !== 'off';
const disallowInlineIf = inlineIfStyle === 'never';
const requireInlineIfThen = inlineIfStyle === 'then';
const validateBlockIf = blockIfStyle !== 'off';
Expand All @@ -43,6 +44,11 @@ export default class CodeStyle {
// Check if the file is empty by going backwards from the last token,
// meaning there are tokens other than `Eof` and `Newline`.
const { tokens } = file.parser;

if (validateColorFormat) {
this.validateColorFormat(file.fileContents, diagnostics, colorFormat);
}

let isFileEmpty = true;
for (let i = tokens.length - 1; i >= 0; i--) {
if (tokens[i].kind !== TokenKind.Eof &&
Expand Down Expand Up @@ -164,6 +170,24 @@ export default class CodeStyle {
file.addDiagnostics(bsDiagnostics);
}

validateColorFormat(fileContents: string, diagnostics: (Omit<BsDiagnostic, 'file'>)[], colorFormat: RuleColorFormat) {
disc7 marked this conversation as resolved.
Show resolved Hide resolved
const colorHashRegex = /#[0-9A-Fa-f]{6}/g;
const colorZeroXRegex = /0x[0-9A-Fa-f]{6}/g;
const colorHashMatches = fileContents.match(colorHashRegex);
const colorZeroXRegexMatches = fileContents.match(colorZeroXRegex);
if (colorFormat === 'hash') {
if (colorZeroXRegexMatches !== null) {
// Color formatting set to hash and has zero-x color formatting values!
debugger;
}
} else if (colorFormat === 'zero-x') {
if (colorHashMatches !== null) {
// Color formatting set to zero-x and has hash color formatting values!
debugger;
}
}
}

validateAAStyle(aa: AALiteralExpression, aaCommaStyle: RuleAAComma, diagnostics: (Omit<BsDiagnostic, 'file'>)[]) {
const indexes = collectWrappingAAMembersIndexes(aa);
const last = indexes.length - 1;
Expand Down
13 changes: 12 additions & 1 deletion src/plugins/codeStyle/styleFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export function getFixes(diagnostic: BsDiagnostic): ChangeEntry {
return addEolLast(diagnostic);
case CodeStyleError.EolLastFound:
return removeEolLast(diagnostic);
case CodeStyleError.ColorFormat:
return expectedColorFormat(diagnostic);
default:
return null;
}
Expand Down Expand Up @@ -79,7 +81,7 @@ function addConditionGroup(diagnostic: BsDiagnostic) {
}

function removeConditionGroup(diagnostic: BsDiagnostic) {
const stat: (IfStatement | WhileStatement) & { condition: GroupingExpression} = diagnostic.data;
const stat: (IfStatement | WhileStatement) & { condition: GroupingExpression } = diagnostic.data;
const { left, right } = stat.condition.tokens;
const spaceBefore = left.leadingWhitespace?.length > 0 ? '' : ' ';
let spaceAfter = '';
Expand Down Expand Up @@ -170,3 +172,12 @@ function removeEolLast(diagnostic: BsDiagnostic): ChangeEntry {
]
};
}

function expectedColorFormat(diagnostic: BsDiagnostic): ChangeEntry {
return {
diagnostic,
changes: [
replaceText(diagnostic.range, '')
disc7 marked this conversation as resolved.
Show resolved Hide resolved
]
};
}
3 changes: 2 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ function rulesToSeverity(rules: BsLintConfig['rules']) {
noPrint: ruleToSeverity(rules['no-print']),
noTodo: ruleToSeverity(rules['no-todo']),
noStop: ruleToSeverity(rules['no-stop']),
eolLast: rules['eol-last']
eolLast: rules['eol-last'],
colorFormat: rules['color-format']
};
}

Expand Down
3 changes: 3 additions & 0 deletions test/project1/source/color-should-be-zero-x.brs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sub init()
color = "#ff0000"
end sub