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

add no-print rule to enforce use of logger #19

Merged
merged 1 commit into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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