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

Add match-default-export-name rule #2117

Merged
merged 4 commits into from
Jan 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/rules/matchDefaultExportNameRule.ts
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));
}
}
}
1 change: 1 addition & 0 deletions test/rules/match-default-export-name/anonymous.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 0;
1 change: 1 addition & 0 deletions test/rules/match-default-export-name/named.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function a() {}
3 changes: 3 additions & 0 deletions test/rules/match-default-export-name/test.ts.lint
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";
Copy link
Contributor

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 works

Copy link
Contributor Author

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.

8 changes: 8 additions & 0 deletions test/rules/match-default-export-name/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"linterOptions": {
"typeCheck": true
},
"rules": {
"match-default-export-name": true
}
}