-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
128 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/** | ||
* @fileoverview Array types should always be written in generic syntax to avoid | ||
* any confusion with array assignments, array indexer, or type selectors. | ||
* @author Sophie Bremer | ||
*/ | ||
|
||
|
||
'use strict'; | ||
|
||
|
||
/* * | ||
* | ||
* Imports | ||
* | ||
* */ | ||
|
||
|
||
import * as FS from 'fs'; | ||
import * as TS from 'typescript'; | ||
import RuleContext from '../RuleContext'; | ||
import RuleOptions from '../RuleOptions'; | ||
import SourceLine from '../SourceLine'; | ||
import SourceToken from '../SourceToken'; | ||
|
||
|
||
/* * | ||
* | ||
* Declarations | ||
* | ||
* */ | ||
|
||
|
||
type GenericArrayTypeContext = RuleContext<RuleOptions>; | ||
|
||
|
||
/* * | ||
* | ||
* Constants | ||
* | ||
* */ | ||
|
||
|
||
const message = 'Do not use the [] shortcut for the array type. Instead write the generic Array<...> to improve readability.'; | ||
|
||
const optionsDefaults: RuleOptions = {}; | ||
|
||
const optionsSchema = {}; | ||
|
||
const pattern = /_?[A-Z]\w*\[\]/u; | ||
|
||
|
||
/* * | ||
* | ||
* Functions | ||
* | ||
* */ | ||
|
||
|
||
// function fix( | ||
// context: GenericArrayTypeContext | ||
// ): void { | ||
// FS.writeFileSync( | ||
// context.sourceCode.fileName + '.json', | ||
// JSON.stringify( | ||
// docletASTs, | ||
// void 0, | ||
// ' ' | ||
// ) | ||
// ); | ||
// } | ||
|
||
function lint ( | ||
context: GenericArrayTypeContext | ||
) { | ||
const sourceLines = context.sourceCode.lines; | ||
|
||
for ( | ||
let line = 0, | ||
lineEnd = sourceLines.length, | ||
sourceLine: SourceLine, | ||
sourceLineTokens: Array<SourceToken>; | ||
line < lineEnd; | ||
++line | ||
) { | ||
sourceLine = sourceLines[line]; | ||
sourceLineTokens = sourceLine.tokens; | ||
|
||
for ( | ||
let index = 0, | ||
indexEnd = sourceLineTokens.length - 2, | ||
firstToken: SourceToken, | ||
secondToken: SourceToken, | ||
thirdToken: SourceToken; | ||
index < indexEnd; | ||
++index | ||
) { | ||
firstToken = sourceLineTokens[index]; | ||
secondToken = sourceLineTokens[index+1]; | ||
thirdToken = sourceLineTokens[index+2]; | ||
|
||
if ( | ||
firstToken.kind === TS.SyntaxKind.Identifier && | ||
secondToken.kind === TS.SyntaxKind.OpenBracketToken && | ||
thirdToken.kind === TS.SyntaxKind.CloseBracketToken | ||
) { | ||
const position = sourceLine.getPosition(context.sourceCode, secondToken); | ||
|
||
if (position) { | ||
context.report(position.line, position.column, message); | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
/* * | ||
* | ||
* Default Export | ||
* | ||
* */ | ||
|
||
export = RuleContext.setupRuleExport( | ||
'layout', | ||
optionsSchema, | ||
optionsDefaults, | ||
lint | ||
); |