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

Add support for ES6 bare imports #468

Merged
merged 1 commit into from
Jun 26, 2015
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
3 changes: 2 additions & 1 deletion src/rules/noUnusedVariableRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class NoUnusedVariablesWalker extends Lint.RuleWalker {
const importClause = node.importClause;

// named imports & namespace imports handled by other walker methods
if (importClause.name != null) {
// importClause will be null for bare imports
if (importClause != null && importClause.name != null) {
const variableIdentifier = importClause.name;
this.validateReferencesForVariable(variableIdentifier.text, variableIdentifier.getStart());
}
Expand Down
13 changes: 6 additions & 7 deletions src/rules/noUseBeforeDeclareRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ class NoUseBeforeDeclareWalker extends Lint.ScopeAwareRuleWalker<VisitedVariable
}

public visitImportDeclaration(node: ts.ImportDeclaration) {
if (node.importClause != null) {
const importClause = node.importClause;
const importClause = node.importClause;

// named imports & namespace imports handled by other walker methods
if (importClause.name != null) {
const variableIdentifier = importClause.name;
this.validateUsageForVariable(variableIdentifier.text, variableIdentifier.getStart());
}
// named imports & namespace imports handled by other walker methods
// importClause will be null for bare imports
if (importClause != null && importClause.name != null) {
const variableIdentifier = importClause.name;
Copy link
Contributor

Choose a reason for hiding this comment

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

it's inappropriate to use const for these variables. we have no way of enforcing these -- someone can still do variableIdentifier.setRandomValue(123) and TypeScript wouldn't know. we should be reserving const for literal values only.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doesn't matter though. I'm strongly in favor of using the strictest specifier whenever possible for variables. It signals to the developer that the variable binding should never change, and devs are expected to know JS/TS semantics enough to know that the values inside that object might change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, that's unrelated to this change. Bring it up in a separate issue/PR.

this.validateUsageForVariable(variableIdentifier.text, variableIdentifier.getStart());
}

super.visitImportDeclaration(node);
Expand Down
2 changes: 2 additions & 0 deletions test/files/rules/nounusedvariable-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ import defaultExport, { namedExport } from "libD"; // failure on 'defaultExport'
bar.someFunc();
baz();
namedExport();

import "jquery";
1 change: 1 addition & 0 deletions test/files/rules/nousebeforedeclare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ import { default as foo1 } from "lib";
import foo2 from "lib";
import _, { map, foldl } from "underscore";
import * as foo3 from "lib";
import "lib";
1 change: 1 addition & 0 deletions test/files/rules/whitespace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ export function each(obj, iterator, context) {
}

export {each as forEach};
import "libE";