Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Remove absolute path from rules error messages. #641

Merged
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
14 changes: 3 additions & 11 deletions src/exportNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,15 @@ export class ExportNameWalker extends Lint.RuleWalker {
private validateExport(exportedName: string, node: ts.Node): void {
const flags = Rule.getIgnoreCase(this.getOptions()) ? 'i' : '';
const regex: RegExp = new RegExp(exportedName + '..*', flags); // filename must be exported name plus any extension
if (!regex.test(this.getFilename())) {
const fileName = Utils.fileBasename(this.getSourceFile().fileName);
if (!regex.test(fileName)) {
if (!this.isSuppressed(exportedName)) {
const failureString: string = Rule.FAILURE_STRING + this.getSourceFile().fileName + ' and ' + exportedName;
const failureString: string = Rule.FAILURE_STRING + fileName + ' and ' + exportedName;
this.addFailureAt(node.getStart(), node.getWidth(), failureString);
}
}
}

private getFilename(): string {
const filename = this.getSourceFile().fileName;
const lastSlash = filename.lastIndexOf('/');
if (lastSlash > -1) {
return filename.substring(lastSlash + 1);
}
return filename;
}

private isSuppressed(exportedName: string): boolean {
const allExceptions = Rule.getExceptions(this.getOptions());

Expand Down
5 changes: 2 additions & 3 deletions src/missingJsdocRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Rule extends Lint.Rules.AbstractRule {
recommendation: 'false,'
};

public static FAILURE_STRING: string = 'File missing JSDoc comment at the top-level: ';
public static FAILURE_STRING: string = 'File missing JSDoc comment at the top-level.';

private static isWarningShown: boolean = false;

Expand All @@ -35,8 +35,7 @@ export class Rule extends Lint.Rules.AbstractRule {
class MissingJSDocWalker extends Lint.RuleWalker {
protected visitSourceFile(node: ts.SourceFile): void {
if (!/^\/\*\*\s*$/gm.test(node.getFullText())) {
const failureString = Rule.FAILURE_STRING + this.getSourceFile().fileName;
this.addFailureAt(node.getStart(), node.getWidth(), failureString);
this.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_STRING);
}
// do not continue walking
}
Expand Down
5 changes: 1 addition & 4 deletions src/reactNoDangerousHtmlRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ class NoDangerousHtmlWalker extends Lint.RuleWalker {
const failureString =
'Invalid call to dangerouslySetInnerHTML in method "' +
this.currentMethodName +
'"\n' +
' of source file ' +
this.getSourceFile().fileName +
'"\n' +
'".\n' +
' Do *NOT* add a suppression for this warning. If you absolutely must use this API then you need\n' +
' to review the usage with a security expert/QE representative. If they decide that this is an\n' +
' acceptable usage then add the exception to xss_exceptions.json';
Expand Down
44 changes: 10 additions & 34 deletions src/tests/ExportNameRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ describe('exportNameRule', (): void => {
{
failure:
'The exported module or identifier name must match the file name. ' +
'Found: test-data/ExportName/ExportNameRuleFailingTestInput.ts and ThisIsNotTheNameOfTheFile',
'Found: ExportNameRuleFailingTestInput.ts and ThisIsNotTheNameOfTheFile',
name: 'test-data/ExportName/ExportNameRuleFailingTestInput.ts',
ruleName: 'export-name',
startPosition: { character: 10, line: 2 }
Expand All @@ -257,7 +257,7 @@ describe('exportNameRule', (): void => {
{
failure:
'The exported module or identifier name must match the file name. ' +
'Found: test-data/ExportName/ExportNameRuleFailingTestInput2.tsx and ThisIsNotTheNameOfTheFile',
'Found: ExportNameRuleFailingTestInput2.tsx and ThisIsNotTheNameOfTheFile',
name: 'test-data/ExportName/ExportNameRuleFailingTestInput2.tsx',
ruleName: 'export-name',
startPosition: { character: 10, line: 2 }
Expand All @@ -274,10 +274,7 @@ describe('exportNameRule', (): void => {
`;
TestHelper.assertViolations(ruleName, inputScript, [
{
failure:
'The exported module or identifier name must match the file name. Found: ' +
Utils.absolutePath('file.ts') +
' and NotMatching',
failure: 'The exported module or identifier name must match the file name. Found: file.ts and NotMatching',
name: Utils.absolutePath('file.ts'),
ruleName: 'export-name',
startPosition: { character: 21, line: 3 }
Expand All @@ -293,10 +290,7 @@ describe('exportNameRule', (): void => {

TestHelper.assertViolations(ruleName, script, [
{
failure:
'The exported module or identifier name must match the file name. Found: ' +
Utils.absolutePath('file.ts') +
' and Example1',
failure: 'The exported module or identifier name must match the file name. Found: file.ts and Example1',
name: Utils.absolutePath('file.ts'),
ruleName: 'export-name',
startPosition: {
Expand All @@ -315,10 +309,7 @@ describe('exportNameRule', (): void => {

TestHelper.assertViolations(ruleName, script, [
{
failure:
'The exported module or identifier name must match the file name. Found: ' +
Utils.absolutePath('file.ts') +
' and Example2',
failure: 'The exported module or identifier name must match the file name. Found: file.ts and Example2',
name: Utils.absolutePath('file.ts'),
ruleName: 'export-name',
startPosition: { character: 17, line: 2 }
Expand All @@ -334,10 +325,7 @@ describe('exportNameRule', (): void => {

TestHelper.assertViolations(ruleName, script, [
{
failure:
'The exported module or identifier name must match the file name. Found: ' +
Utils.absolutePath('file.ts') +
' and Example3',
failure: 'The exported module or identifier name must match the file name. Found: file.ts and Example3',
name: Utils.absolutePath('file.ts'),
ruleName: 'export-name',
startPosition: { character: 17, line: 2 }
Expand All @@ -354,10 +342,7 @@ describe('exportNameRule', (): void => {

TestHelper.assertViolations(ruleName, script, [
{
failure:
'The exported module or identifier name must match the file name. Found: ' +
Utils.absolutePath('file.ts') +
' and Example3a',
failure: 'The exported module or identifier name must match the file name. Found: file.ts and Example3a',
name: Utils.absolutePath('file.ts'),
ruleName: 'export-name',
startPosition: { character: 17, line: 3 }
Expand All @@ -373,10 +358,7 @@ describe('exportNameRule', (): void => {

TestHelper.assertViolations(ruleName, script, [
{
failure:
'The exported module or identifier name must match the file name. Found: ' +
Utils.absolutePath('file.ts') +
' and Example4',
failure: 'The exported module or identifier name must match the file name. Found: file.ts and Example4',
name: Utils.absolutePath('file.ts'),
ruleName: 'export-name',
startPosition: { character: 28, line: 2 }
Expand All @@ -392,10 +374,7 @@ describe('exportNameRule', (): void => {

TestHelper.assertViolations(ruleName, script, [
{
failure:
'The exported module or identifier name must match the file name. Found: ' +
Utils.absolutePath('file.ts') +
' and Example5',
failure: 'The exported module or identifier name must match the file name. Found: file.ts and Example5',
name: Utils.absolutePath('file.ts'),
ruleName: 'export-name',
startPosition: { character: 30, line: 2 }
Expand All @@ -411,10 +390,7 @@ describe('exportNameRule', (): void => {

TestHelper.assertViolations(ruleName, script, [
{
failure:
'The exported module or identifier name must match the file name. Found: ' +
Utils.absolutePath('file.ts') +
' and Example6',
failure: 'The exported module or identifier name must match the file name. Found: file.ts and Example6',
name: Utils.absolutePath('file.ts'),
ruleName: 'export-name',
startPosition: { character: 28, line: 2 }
Expand Down
10 changes: 5 additions & 5 deletions src/tests/MissingJsdocRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function whatever() { }`;

TestHelper.assertViolations(ruleName, script, [
{
failure: 'File missing JSDoc comment at the top-level: ' + Utils.absolutePath('file.ts'),
failure: 'File missing JSDoc comment at the top-level.',
name: Utils.absolutePath('file.ts'),
ruleName: 'missing-jsdoc',
startPosition: { character: 1, line: 2 }
Expand All @@ -47,7 +47,7 @@ function whatever() { }`;

TestHelper.assertViolations(ruleName, script, [
{
failure: 'File missing JSDoc comment at the top-level: ' + Utils.absolutePath('file.ts'),
failure: 'File missing JSDoc comment at the top-level.',
name: Utils.absolutePath('file.ts'),
ruleName: 'missing-jsdoc',
startPosition: { character: 1, line: 5 }
Expand All @@ -64,7 +64,7 @@ function whatever() { }`;

TestHelper.assertViolations(ruleName, script, [
{
failure: 'File missing JSDoc comment at the top-level: ' + Utils.absolutePath('file.ts'),
failure: 'File missing JSDoc comment at the top-level.',
name: Utils.absolutePath('file.ts'),
ruleName: 'missing-jsdoc',
startPosition: { character: 1, line: 5 }
Expand All @@ -81,7 +81,7 @@ function whatever() { }`;

TestHelper.assertViolations(ruleName, script, [
{
failure: 'File missing JSDoc comment at the top-level: ' + Utils.absolutePath('file.ts'),
failure: 'File missing JSDoc comment at the top-level.',
name: Utils.absolutePath('file.ts'),
ruleName: 'missing-jsdoc',
startPosition: { character: 1, line: 5 }
Expand All @@ -98,7 +98,7 @@ function whatever() { }`;

TestHelper.assertViolations(ruleName, script, [
{
failure: 'File missing JSDoc comment at the top-level: ' + Utils.absolutePath('file.ts'),
failure: 'File missing JSDoc comment at the top-level.',
name: Utils.absolutePath('file.ts'),
ruleName: 'missing-jsdoc',
startPosition: { character: 5, line: 5 }
Expand Down
10 changes: 4 additions & 6 deletions src/tests/ReactNoDangerousHtmlRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ function someFunction() {
[
{
failure:
'Invalid call to dangerouslySetInnerHTML in method "<unknown>"\n of source file ' +
Utils.absolutePath('file.tsx') +
'"\n Do *NOT* add a suppression for this warning. ' +
'Invalid call to dangerouslySetInnerHTML in method "<unknown>".' +
'\n Do *NOT* add a suppression for this warning. ' +
'If you absolutely must use this API then you need\n to review the usage with a security expert/QE ' +
'representative. If they decide that this is an\n acceptable usage then add the exception ' +
'to xss_exceptions.json',
Expand All @@ -34,9 +33,8 @@ function someFunction() {
},
{
failure:
'Invalid call to dangerouslySetInnerHTML in method "<unknown>"\n of source file ' +
Utils.absolutePath('file.tsx') +
'"\n Do *NOT* add a suppression for this warning. ' +
'Invalid call to dangerouslySetInnerHTML in method "<unknown>".' +
'\n Do *NOT* add a suppression for this warning. ' +
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
'If you absolutely must use this API then you need\n to review the usage with a security expert/QE ' +
'representative. If they decide that this is an\n acceptable usage then add the exception ' +
'to xss_exceptions.json',
Expand Down
4 changes: 4 additions & 0 deletions src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,9 @@ export namespace Utils {
// Replaces \ with / to match format of ts.SourceFile.fileName on Windows
return path.resolve(relativePath).replace(/\\/g, '/');
}

export function fileBasename(relativePath: string): string {
return path.basename(relativePath);
}
}
/* tslint:enable:no-increment-decrement */