Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Clean up array-type and consider 'object' as a simple type #2353

Merged
merged 4 commits into from
Mar 29, 2017

Conversation

andy-hanson
Copy link
Contributor

@andy-hanson andy-hanson commented Mar 16, 2017

PR checklist

Overview of change:

Cleaned up the implementation and added 'object' to the list of simple types.

Copy link
Contributor

@ajafff ajafff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.
Just some minor nits

@@ -51,83 +53,116 @@ export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING_GENERIC_SIMPLE = "Array type using 'T[]' is forbidden for non-simple types. Use 'Array<T>' instead.";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const alignWalker = new ArrayTypeWalker(sourceFile, this.getOptions());
return this.applyWithWalker(alignWalker);
const option = this.getOptions().ruleArguments[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use this.ruleArguments

return true;
} else {
case 1:
return utils.isIdentifier(typeName) && typeName.text === "Array" && isSimpleType(typeArguments[0]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use typeName.kind === ts.SyntaxKind.Identifier instead of isIdentifier. typeName is a discriminated union so this narrows the type.

const OPTION_GENERIC = "generic";
const OPTION_ARRAY_SIMPLE = "array-simple";
type Option = "array" | "generic" | "array-simple";
const OPTION_ARRAY: Option = "array";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for the type annotation

const alignWalker = new ArrayTypeWalker(sourceFile, this.getOptions());
return this.applyWithWalker(alignWalker);
const option = this.getOptions().ruleArguments[0];
return this.applyWithFunction(sourceFile, (ctx) => walk(ctx, option));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

option can be passed as 3rd parameter and destructured inside walk

function checkTypeReference(node: ts.TypeReferenceNode): void {
const { typeName, typeArguments } = node;

if (option === "generic" || !isArrayIdentifier(typeName)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be consistent. you should use the constants. e.g. OPTION_GENERIC here and in all other comparisons below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

option is a string union, so any misspellings would be caught. It's like an enum.

@nchen63 nchen63 added this to the TSLint 5.0 milestone Mar 27, 2017
@adidahiya
Copy link
Contributor

@andy-hanson could you please resolve the new merge conflicts?

@adidahiya adidahiya removed this from the TSLint 5.0 milestone Mar 29, 2017
@adidahiya adidahiya self-assigned this Mar 29, 2017
@adidahiya adidahiya merged commit 0658694 into palantir:master Mar 29, 2017
@andy-hanson andy-hanson deleted the array-type branch March 30, 2017 00:12
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants