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 887
Add 'type-literal-delimiter' rule #2374
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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,76 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 Palantir Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { isTypeLiteralNode } from "tsutils"; | ||
import * as ts from "typescript"; | ||
|
||
import * as Lint from "../index"; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
/* tslint:disable:object-literal-sort-keys */ | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: "type-literal-delimiter", | ||
description: Lint.Utils.dedent` | ||
Checks that type literal members are separated by commas. | ||
Does not check the last member, as that is done by 'trailing-comma'.`, | ||
optionsDescription: "Not configurable.", | ||
options: null, | ||
optionExamples: [true], | ||
type: "style", | ||
typescriptOnly: true, | ||
}; | ||
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING_MISSING = | ||
"Expected type literal to use ',' to separate members."; | ||
public static FAILURE_STRING_SEMICOLON = | ||
"Expected type literal to use ',' instead of ';'."; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithFunction(sourceFile, walk); | ||
} | ||
} | ||
|
||
function walk(ctx: Lint.WalkContext<void>): void { | ||
const { sourceFile } = ctx; | ||
ts.forEachChild(sourceFile, function cb(node: ts.Node): void { | ||
if (isTypeLiteralNode(node)) { | ||
check(node); | ||
} | ||
ts.forEachChild(node, cb); | ||
}); | ||
|
||
function check({ members }: ts.TypeLiteralNode): void { | ||
members.forEach((member, idx) => { | ||
const end = member.end - 1; | ||
const delimiter = sourceFile.text[end]; | ||
switch (delimiter) { | ||
case ",": | ||
break; | ||
case ";": | ||
ctx.addFailureAt(end, 1, Rule.FAILURE_STRING_SEMICOLON); | ||
break; | ||
default: | ||
// Allow missing trailing ',' | ||
if (idx !== members.length - 1) { | ||
ctx.addFailureAt(end, 1, Rule.FAILURE_STRING_MISSING); | ||
} | ||
break; | ||
} | ||
}); | ||
} | ||
} |
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,28 @@ | ||
type T = { | ||
x: number | ||
~ [MISSING] | ||
y: string | ||
} | ||
|
||
type T = { | ||
x: number, | ||
// Doesn't care about missing final comma; trailing-comma will handle. | ||
y: string | ||
} | ||
|
||
// Does care about semicolon. | ||
type T = { | ||
x: number, | ||
y: string; | ||
~ [SEMI] | ||
} | ||
|
||
type T = { | ||
x: number; | ||
~ [SEMI] | ||
y: string | ||
} | ||
|
||
|
||
[MISSING]: Expected type literal to use ',' to separate members. | ||
[SEMI]: Expected type literal to use ',' instead of ';'. |
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,5 @@ | ||
{ | ||
"rules": { | ||
"type-literal-delimiter": true | ||
} | ||
} |
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.
does it handle space before/after delimiter?
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.
Not currently, no. I think that would be more appropriate for the
whitespace
rule.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.
I'm asking if it would erroneously flag
{ a: b , c: d }
or{ a: b, c: d }
. Should probably have a unit testThere 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.
Oh, gotcha. Added a test.