Skip to content

Commit

Permalink
add no-print rule (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: Sam Heavner <[email protected]>
  • Loading branch information
slheavner and Sam Heavner authored Apr 16, 2021
1 parent 23fbc21 commit 13157bd
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type BsLintConfig = Pick<BsConfig, 'project' | 'rootDir' | 'files' | 'cwd
'named-function-style'?: RuleFunction;
'anon-function-style'?: RuleFunction;
'type-annotations'?: RuleTypeAnnotations;
'no-print'?: RuleSeverity;
};
};

Expand All @@ -49,6 +50,7 @@ export interface BsLintRules {
namedFunctionStyle: RuleFunction;
anonFunctionStyle: RuleFunction;
typeAnnotations: RuleTypeAnnotations;
noPrint: BsLintSeverity;
}

export { Linter };
Expand Down
7 changes: 7 additions & 0 deletions src/plugins/codeStyle/diagnosticMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum CodeStyleError {
FunctionKeywordExpected = 'LINT3009',
ReturnTypeAnnotation = 'LINT3010',
TypeAnnotation = 'LINT3011',
NoPrint = 'LINT3012'
}

const CS = 'Code style:';
Expand Down Expand Up @@ -77,6 +78,12 @@ const messages = {
code: CodeStyleError.TypeAnnotation,
message: `${ST} type annotation required`,
range
}),
noPrint: (range: Range, severity: DiagnosticSeverity) => ({
severity: severity,
code: CodeStyleError.NoPrint,
message: `${CS} Avoid using direct Print statements`,
range
})
};

Expand Down
68 changes: 51 additions & 17 deletions src/plugins/codeStyle/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ describe('codeStyle', () => {
rules: {
'inline-if-style': 'off',
'block-if-style': 'off',
'condition-style': 'off'
'condition-style': 'off',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -55,7 +56,8 @@ describe('codeStyle', () => {
rules: {
'inline-if-style': 'never',
'block-if-style': 'off',
'condition-style': 'off'
'condition-style': 'off',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -73,7 +75,8 @@ describe('codeStyle', () => {
rules: {
'inline-if-style': 'then',
'block-if-style': 'off',
'condition-style': 'off'
'condition-style': 'off',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -90,7 +93,8 @@ describe('codeStyle', () => {
rules: {
'inline-if-style': 'no-then',
'block-if-style': 'off',
'condition-style': 'off'
'condition-style': 'off',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -110,7 +114,8 @@ describe('codeStyle', () => {
rules: {
'inline-if-style': 'off',
'block-if-style': 'off',
'condition-style': 'off'
'condition-style': 'off',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -125,7 +130,8 @@ describe('codeStyle', () => {
rules: {
'inline-if-style': 'off',
'block-if-style': 'then',
'condition-style': 'off'
'condition-style': 'off',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -143,7 +149,8 @@ describe('codeStyle', () => {
rules: {
'inline-if-style': 'off',
'block-if-style': 'no-then',
'condition-style': 'off'
'condition-style': 'off',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -164,7 +171,8 @@ describe('codeStyle', () => {
rules: {
'inline-if-style': 'off',
'block-if-style': 'off',
'condition-style': 'off'
'condition-style': 'off',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -179,7 +187,8 @@ describe('codeStyle', () => {
rules: {
'inline-if-style': 'off',
'block-if-style': 'off',
'condition-style': 'group'
'condition-style': 'group',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -197,7 +206,8 @@ describe('codeStyle', () => {
rules: {
'inline-if-style': 'off',
'block-if-style': 'off',
'condition-style': 'no-group'
'condition-style': 'no-group',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -216,7 +226,8 @@ describe('codeStyle', () => {
files: ['source/no-function-style.brs'],
rules: {
'named-function-style': 'no-function',
'anon-function-style': 'no-function'
'anon-function-style': 'no-function',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -233,7 +244,8 @@ describe('codeStyle', () => {
files: ['source/no-sub-style.brs'],
rules: {
'named-function-style': 'no-sub',
'anon-function-style': 'no-sub'
'anon-function-style': 'no-sub',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -250,7 +262,8 @@ describe('codeStyle', () => {
files: ['source/auto-function-style.brs'],
rules: {
'named-function-style': 'auto',
'anon-function-style': 'auto'
'anon-function-style': 'auto',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -274,7 +287,8 @@ describe('codeStyle', () => {
rules: {
'named-function-style': 'off',
'anon-function-style': 'off',
'type-annotations': 'off'
'type-annotations': 'off',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -290,7 +304,8 @@ describe('codeStyle', () => {
rules: {
'named-function-style': 'off',
'anon-function-style': 'off',
'type-annotations': 'return'
'type-annotations': 'return',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -307,7 +322,8 @@ describe('codeStyle', () => {
rules: {
'named-function-style': 'off',
'anon-function-style': 'off',
'type-annotations': 'args'
'type-annotations': 'args',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -325,7 +341,8 @@ describe('codeStyle', () => {
rules: {
'named-function-style': 'off',
'anon-function-style': 'off',
'type-annotations': 'all'
'type-annotations': 'all',
'no-print': 'off'
}
});
const actual = fmtDiagnostics(diagnostics);
Expand All @@ -336,4 +353,21 @@ describe('codeStyle', () => {
];
expect(actual).deep.equal(expected);
});


it('enforce no print', async () => {
const diagnostics = await linter.run({
...project1,
files: ['source/no-print.brs'],
rules: {
'no-print': 'error'
}
});
const actual = fmtDiagnostics(diagnostics);
const expected = [
`02:LINT3012:Code style: Avoid using direct Print statements`,
`03:LINT3012:Code style: Avoid using direct Print statements`
];
expect(actual).deep.equal(expected);
});
});
5 changes: 4 additions & 1 deletion src/plugins/codeStyle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class CodeStyle {
}

const diagnostics: (Omit<BsDiagnostic, 'file'>)[] = [];
const { inlineIfStyle, blockIfStyle, conditionStyle } = this.lintContext.severity;
const { inlineIfStyle, blockIfStyle, conditionStyle, noPrint } = this.lintContext.severity;
const validateInlineIf = inlineIfStyle !== 'off';
const disallowInlineIf = inlineIfStyle === 'never';
const requireInlineIfThen = inlineIfStyle === 'then';
Expand Down Expand Up @@ -56,6 +56,9 @@ export default class CodeStyle {
);
}
}
},
PrintStatement: s => {
diagnostics.push(messages.noPrint(s.tokens.print.range, noPrint));
}
}), { walkMode: WalkMode.visitStatementsRecursive });

Expand Down
6 changes: 4 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export function getDefaultRules(): BsLintConfig['rules'] {
'condition-style': 'no-group',
'named-function-style': 'auto',
'anon-function-style': 'auto',
'type-annotations': 'off'
'type-annotations': 'off',
'no-print': 'warn'
};
}

Expand Down Expand Up @@ -123,7 +124,8 @@ function rulesToSeverity(rules: BsLintConfig['rules']) {
conditionStyle: rules['condition-style'],
namedFunctionStyle: rules['named-function-style'],
anonFunctionStyle: rules['anon-function-style'],
typeAnnotations: rules['type-annotations']
typeAnnotations: rules['type-annotations'],
noPrint: ruleToSeverity(rules['no-print'])
};
}

Expand Down
2 changes: 1 addition & 1 deletion test/project1/bslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"rules": {
"unreachable-code": "info"
}
}
}
4 changes: 4 additions & 0 deletions test/project1/source/no-print.brs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sub myFunc()
print "my log statement"
? "my other log"
end sub

0 comments on commit 13157bd

Please sign in to comment.