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 886
Add match-default-export-name
rule
#2117
Merged
nchen63
merged 4 commits into
palantir:master
from
andy-hanson:match_default_export_name
Jan 29, 2017
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
edf6207
Add `match-default-export-name` rule
andy-hanson 38b8ecd
Merge branch 'master' of https://github.com/palantir/tslint into matc…
andy-hanson 9c486b0
Use TypedRule and change failure string
andy-hanson 0480c26
`requiresTypeInfo` not needed
nchen63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @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 * as ts from "typescript"; | ||
|
||
import * as Lint from "../index"; | ||
|
||
export class Rule extends Lint.Rules.TypedRule { | ||
/* tslint:disable:object-literal-sort-keys */ | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: "match-default-export-name", | ||
description: Lint.Utils.dedent` | ||
Requires that a default import have the same name as the declaration it imports. | ||
Does nothing for anonymous default exports.`, | ||
optionsDescription: "Not configurable.", | ||
options: null, | ||
optionExamples: ["true"], | ||
type: "style", | ||
typescriptOnly: true, | ||
}; | ||
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING(importName: string, exportName: string): string { | ||
return `Expected import '${importName}' to match the default export '${exportName}'.`; | ||
} | ||
|
||
public applyWithProgram(sourceFile: ts.SourceFile, langSvc: ts.LanguageService): Lint.RuleFailure[] { | ||
return this.applyWithWalker(new Walker(sourceFile, this.getOptions(), langSvc.getProgram())); | ||
} | ||
} | ||
|
||
class Walker extends Lint.ProgramAwareRuleWalker { | ||
public visitSourceFile(node: ts.SourceFile) { | ||
for (const statement of node.statements) { | ||
if (statement.kind !== ts.SyntaxKind.ImportDeclaration) { | ||
continue; | ||
} | ||
|
||
const { importClause } = statement as ts.ImportDeclaration; | ||
if (importClause && importClause.name) { | ||
this.checkDefaultImport(importClause.name); | ||
} | ||
} | ||
} | ||
|
||
private checkDefaultImport(defaultImport: ts.Identifier) { | ||
const { declarations } = this.getTypeChecker().getAliasedSymbol( | ||
this.getTypeChecker().getSymbolAtLocation(defaultImport)); | ||
const name = declarations && declarations[0] && declarations[0].name; | ||
if (name && name.kind === ts.SyntaxKind.Identifier && defaultImport.text !== name.text) { | ||
this.addFailureAtNode(defaultImport, Rule.FAILURE_STRING(defaultImport.text, name.text)); | ||
} | ||
} | ||
} |
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 @@ | ||
export default 0; |
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 @@ | ||
export default function a() {} |
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,3 @@ | ||
import b from "./named"; | ||
~ [Expected import 'b' to match the default export 'a'.] | ||
import anyName from "./anonymous"; | ||
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,8 @@ | ||
{ | ||
"linterOptions": { | ||
"typeCheck": true | ||
}, | ||
"rules": { | ||
"match-default-export-name": 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.
add test for
import a as b
. Users look at tests to understand how the rule worksThere 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.
That's not legal syntax anyway.