-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix null and RegExp literal values being allowed (#31)
* Fix toString raising runtime error on null * Add rule for unspecified literals * Import rule in index/rules * Update explanation
- Loading branch information
Showing
3 changed files
with
52 additions
and
0 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,48 @@ | ||
import * as es from 'estree' | ||
|
||
import { ErrorSeverity, ErrorType, Rule, SourceError } from '../types' | ||
|
||
const specifiedLiterals = ['boolean', 'string', 'number'] | ||
|
||
export class NoUnspecifiedLiteral implements SourceError { | ||
public type = ErrorType.SYNTAX | ||
public severity = ErrorSeverity.ERROR | ||
|
||
constructor(public node: es.Literal) {} | ||
|
||
get location() { | ||
return this.node.loc! | ||
} | ||
|
||
public explain() { | ||
/** | ||
* A check is used for RegExp to ensure that only null and RegExp are caught. | ||
* Any other unspecified literal value should not be caught. | ||
*/ | ||
const literal = this.node.value === null ? 'null' | ||
: this.node.value instanceof RegExp ? 'RegExp' | ||
: '' | ||
return ( | ||
`'${literal}' literals are not allowed` | ||
) | ||
} | ||
|
||
public elaborate() { | ||
return this.explain() | ||
} | ||
} | ||
|
||
const noUnspecifiedLiteral: Rule<es.Literal> = { | ||
name: 'no-unspecified-literal', | ||
checkers: { | ||
Literal(node: es.Literal) { | ||
if (!specifiedLiterals.includes(typeof node.value)) { | ||
return [new NoUnspecifiedLiteral(node)] | ||
} else { | ||
return [] | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default noUnspecifiedLiteral |