This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #349
- Loading branch information
Showing
6 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as ts from 'typescript'; | ||
import * as Lint from 'tslint'; | ||
|
||
import {ExtendedMetadata} from './utils/ExtendedMetadata'; | ||
|
||
const FAILURE_STRING_EMPTY: string = 'This file is empty and should be deleted.'; | ||
const FAILURE_STRING_COMMENTS: string = 'This file only contains comments and should be deleted.'; | ||
|
||
/** | ||
* Implementation of the no-useless-files rule. | ||
*/ | ||
export class Rule extends Lint.Rules.AbstractRule { | ||
|
||
public static metadata: ExtendedMetadata = { | ||
ruleName: 'no-useless-files', | ||
type: 'maintainability', | ||
description: 'Locates files that only contain commented out code, whitespace characters, or have no content', | ||
options: null, | ||
optionsDescription: '', | ||
typescriptOnly: false, | ||
issueClass: 'Non-SDL', | ||
issueType: 'Warning', | ||
severity: 'Low', | ||
level: 'Opportunity for Excellence', | ||
group: 'Clarity', | ||
commonWeaknessEnumeration: '398' //Indicator of Poor Code Quality | ||
}; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
const ruleFailures: Lint.RuleFailure[] = []; | ||
|
||
const fileContent = sourceFile.getFullText().trim(); | ||
const fileContentNoComments = sourceFile.getText().trim(); | ||
|
||
if (fileContent.length === 0) { | ||
//This file only contains whitespace characters, a totally empty & useless file | ||
ruleFailures.push(new Lint.RuleFailure(sourceFile, 0, 0, FAILURE_STRING_EMPTY, this.getOptions().ruleName)); | ||
} else if (fileContentNoComments.length === 0) { | ||
//This file only contains code comments, not empty but completely useless | ||
ruleFailures.push(new Lint.RuleFailure(sourceFile, 0, 0, FAILURE_STRING_COMMENTS, this.getOptions().ruleName)); | ||
} | ||
return ruleFailures; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import {TestHelper} from './TestHelper'; | ||
|
||
/** | ||
* Unit tests. | ||
*/ | ||
describe('noUselessFilesRule', () : void => { | ||
|
||
const ruleName : string = 'no-useless-files'; | ||
|
||
it('should pass on a normal file that contains code', () : void => { | ||
const script : string = ` | ||
export class MyClass { | ||
constructor () { | ||
console.log("foo"); | ||
} | ||
}`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ ]); | ||
}); | ||
|
||
it('should fail on a file that only contains single-line comments', () : void => { | ||
const script : string = `// This is the only comment in this file`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [{ | ||
'failure': 'This file only contains comments and should be deleted.', | ||
'name': 'file.ts', | ||
'ruleName': ruleName, | ||
'startPosition': { 'character': 1, 'line': 1 } | ||
}]); | ||
}); | ||
|
||
it('should fail on a file that only contains multi-line comments', () : void => { | ||
const script : string = `/* | ||
export class MyClass { | ||
constructor () { | ||
console.log("foo"); | ||
} | ||
} | ||
*/`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [{ | ||
'failure': 'This file only contains comments and should be deleted.', | ||
'name': 'file.ts', | ||
'ruleName': ruleName, | ||
'startPosition': { 'character': 1, 'line': 1 } | ||
}]); | ||
}); | ||
|
||
it('should fail on a file that only contains several comments', () : void => { | ||
const script : string = `/* | ||
export class MyClass { | ||
constructor () { | ||
console.log("foo"); | ||
} | ||
} | ||
*/ | ||
// here is a single line comment | ||
/* and another | ||
multline comment */`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [{ | ||
'failure': 'This file only contains comments and should be deleted.', | ||
'name': 'file.ts', | ||
'ruleName': ruleName, | ||
'startPosition': { 'character': 1, 'line': 1 } | ||
}]); | ||
}); | ||
|
||
it('should fail on a file that only contains whitespace', () : void => { | ||
//The below string contains spaces, tabs, and linebreaks | ||
const script : string = ` | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [{ | ||
'failure': 'This file is empty and should be deleted.', | ||
'name': 'file.ts', | ||
'ruleName': ruleName, | ||
'startPosition': { 'character': 1, 'line': 1 } | ||
}]); | ||
}); | ||
|
||
it('should fail on a file that has no content', () : void => { | ||
const script : string = ``; | ||
|
||
TestHelper.assertViolations(ruleName, script, [{ | ||
'failure': 'This file is empty and should be deleted.', | ||
'name': 'file.ts', | ||
'ruleName': ruleName, | ||
'startPosition': { 'character': 1, 'line': 1 } | ||
}]); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -285,6 +285,7 @@ | |
"check-operator", | ||
"check-separator", | ||
"check-type" | ||
] | ||
], | ||
"no-useless-files": true | ||
} | ||
} |