This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 889
space-before-function-paren: Rewrite #2700
Merged
nchen63
merged 3 commits into
palantir:master
from
andy-hanson:space-before-function-paren
May 8, 2017
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
|
@@ -61,130 +61,64 @@ export class Rule extends Lint.Rules.AbstractRule { | |
public static MISSING_WHITESPACE_ERROR = "Missing whitespace before function parens"; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker(new FunctionWalker(sourceFile, this.getOptions())); | ||
return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments[0] as Option | Options | undefined)); | ||
} | ||
} | ||
|
||
type OptionName = "anonymous" | "asyncArrow" | "constructor" | "method" | "named"; | ||
|
||
const optionNames: OptionName[] = ["anonymous", "asyncArrow", "constructor", "method", "named"]; | ||
type Option = "always" | "never"; | ||
type Options = Partial<Record<OptionName, Option>>; | ||
|
||
interface CachedOptions { | ||
anonymous?: Option; | ||
asyncArrow?: Option; | ||
constructor?: Option; | ||
method?: Option; | ||
named?: Option; | ||
} | ||
|
||
class FunctionWalker extends Lint.RuleWalker { | ||
private scanner: ts.Scanner; | ||
// assign constructor now to avoid typescript assuming its a function type | ||
private cachedOptions: CachedOptions = {constructor: undefined}; | ||
|
||
constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) { | ||
super(sourceFile, options); | ||
this.scanner = ts.createScanner(ts.ScriptTarget.ES5, false, ts.LanguageVariant.Standard, sourceFile.text); | ||
this.cacheOptions(); | ||
} | ||
|
||
protected visitArrowFunction(node: ts.ArrowFunction): void { | ||
const option = this.getOption("asyncArrow"); | ||
const syntaxList = Lint.childOfKind(node, ts.SyntaxKind.SyntaxList)!; | ||
const isAsyncArrow = syntaxList.getStart() === node.getStart() && syntaxList.getText() === "async"; | ||
const openParen = isAsyncArrow ? Lint.childOfKind(node, ts.SyntaxKind.OpenParenToken) : undefined; | ||
this.evaluateRuleAt(openParen, option); | ||
|
||
super.visitArrowFunction(node); | ||
} | ||
|
||
protected visitConstructorDeclaration(node: ts.ConstructorDeclaration): void { | ||
const option = this.getOption("constructor"); | ||
const openParen = Lint.childOfKind(node, ts.SyntaxKind.OpenParenToken); | ||
this.evaluateRuleAt(openParen, option); | ||
|
||
super.visitConstructorDeclaration(node); | ||
} | ||
|
||
protected visitFunctionDeclaration(node: ts.FunctionDeclaration): void { | ||
this.visitFunction(node); | ||
super.visitFunctionDeclaration(node); | ||
} | ||
|
||
protected visitFunctionExpression(node: ts.FunctionExpression): void { | ||
this.visitFunction(node); | ||
super.visitFunctionExpression(node); | ||
} | ||
|
||
protected visitMethodDeclaration(node: ts.MethodDeclaration): void { | ||
this.visitMethod(node); | ||
super.visitMethodDeclaration(node); | ||
function parseOptions(json: Option | Options | undefined): Options { | ||
// Need to specify constructor or it will be Object | ||
const options: Options = { constructor: undefined }; | ||
for (const optionName of optionNames) { | ||
options[optionName] = typeof json === "object" ? json[optionName] : json === undefined ? "always" : json; | ||
} | ||
return options; | ||
} | ||
|
||
protected visitMethodSignature(node: ts.SignatureDeclaration): void { | ||
this.visitMethod(node); | ||
super.visitMethodSignature(node); | ||
} | ||
|
||
private cacheOptions(): void { | ||
const options = (this.getOptions() as any[])[0] as Option | { [K in OptionName]: Option } | undefined; | ||
const optionNames: OptionName[] = ["anonymous", "asyncArrow", "constructor", "method", "named"]; | ||
|
||
optionNames.forEach((optionName) => { | ||
switch (options) { | ||
case undefined: | ||
case "always": | ||
this.cachedOptions[optionName] = "always"; | ||
break; | ||
|
||
case "never": | ||
this.cachedOptions[optionName] = "never"; | ||
break; | ||
|
||
default: | ||
this.cachedOptions[optionName] = options[optionName]; | ||
function walk(ctx: Lint.WalkContext<Options>): void { | ||
const { options, sourceFile } = ctx; | ||
ts.forEachChild(sourceFile, function cb(node: ts.Node): void { | ||
const option = getOption(node, options); | ||
if (option !== undefined) { | ||
const openParen = Lint.childOfKind(node, ts.SyntaxKind.OpenParenToken)!; | ||
const hasSpace = sourceFile.text[openParen.pos] !== "("; | ||
|
||
if (hasSpace && option === "never") { | ||
const pos = openParen.getStart() - 1; | ||
ctx.addFailureAt(pos, 1, Rule.INVALID_WHITESPACE_ERROR, Lint.Replacement.deleteText(pos, 1)); | ||
} else if (!hasSpace && option === "always") { | ||
const pos = openParen.getStart(); | ||
ctx.addFailureAt(pos, 1, Rule.MISSING_WHITESPACE_ERROR, Lint.Replacement.appendText(pos, " ")); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
private getOption(optionName: OptionName): Option | undefined { | ||
return this.cachedOptions[optionName]; | ||
} | ||
ts.forEachChild(node, cb); | ||
}); | ||
} | ||
|
||
private evaluateRuleAt(openParen?: ts.Node, option?: Option): void { | ||
if (openParen === undefined || option === undefined) { | ||
return; | ||
} | ||
function getOption(node: ts.Node, options: Options): Option | undefined { | ||
switch (node.kind) { | ||
case ts.SyntaxKind.ArrowFunction: | ||
return Lint.hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword) ? options.asyncArrow : undefined; | ||
|
||
const hasSpace = this.isSpaceAt(openParen.getStart() - 1); | ||
case ts.SyntaxKind.Constructor: | ||
return options.constructor; | ||
|
||
if (hasSpace && option === "never") { | ||
const pos = openParen.getStart() - 1; | ||
this.addFailureAt(pos, 1, Rule.INVALID_WHITESPACE_ERROR, this.deleteText(pos, 1)); | ||
} else if (!hasSpace && option === "always") { | ||
const pos = openParen.getStart(); | ||
this.addFailureAt(pos, 1, Rule.MISSING_WHITESPACE_ERROR, this.appendText(pos, " ")); | ||
} | ||
} | ||
case ts.SyntaxKind.FunctionDeclaration: | ||
return options.named; | ||
|
||
private isSpaceAt(textPos: number): boolean { | ||
this.scanner.setTextPos(textPos); | ||
const prevTokenKind = this.scanner.scan(); | ||
return prevTokenKind === ts.SyntaxKind.WhitespaceTrivia; | ||
} | ||
case ts.SyntaxKind.FunctionExpression: | ||
return (node as ts.FunctionExpression).name !== undefined ? options.named : options.anonymous; | ||
|
||
private visitFunction(node: ts.Node): void { | ||
const identifier = Lint.childOfKind(node, ts.SyntaxKind.Identifier); | ||
const hasIdentifier = identifier !== undefined && (identifier.getEnd() !== identifier.getStart()); | ||
const optionName = hasIdentifier ? "named" : "anonymous"; | ||
const option = this.getOption(optionName); | ||
const openParen = Lint.childOfKind(node, ts.SyntaxKind.OpenParenToken); | ||
this.evaluateRuleAt(openParen, option); | ||
} | ||
case ts.SyntaxKind.MethodDeclaration: | ||
case ts.SyntaxKind.MethodSignature: | ||
return options.method; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you add |
||
|
||
private visitMethod(node: ts.Node): void { | ||
const option = this.getOption("method"); | ||
const openParen = Lint.childOfKind(node, ts.SyntaxKind.OpenParenToken); | ||
this.evaluateRuleAt(openParen, option); | ||
default: | ||
return undefined; | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this line should be changed to handle comments: