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

prefer-const: don't check ambient declarations #2391

Merged
merged 1 commit into from
Apr 12, 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
13 changes: 11 additions & 2 deletions src/rules/preferConstRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,21 @@ interface DestructuringInfo {
class PreferConstWalker extends Lint.AbstractWalker<Options> {
private scope: Scope;
public walk(sourceFile: ts.SourceFile) {
// don't check anything on declaration files
if (sourceFile.isDeclarationFile) {
return;
}

this.scope = new Scope();
const cb = (node: ts.Node): void => {
const savedScope = this.scope;
const boundary = utils.isScopeBoundary(node);
if (boundary !== utils.ScopeBoundary.None) {
if (boundary === utils.ScopeBoundary.Function) {
if (node.kind === ts.SyntaxKind.ModuleDeclaration && utils.hasModifier(node.modifiers, ts.SyntaxKind.DeclareKeyword)) {
// don't check ambient namespaces
return;
}
this.scope = new Scope();
if (utils.isFunctionDeclaration(node) ||
utils.isMethodDeclaration(node) ||
Expand Down Expand Up @@ -240,11 +249,11 @@ class PreferConstWalker extends Lint.AbstractWalker<Options> {
let declarationInfo: DeclarationInfo;
const kind = utils.getVariableDeclarationKind(declarationList);
if (kind === utils.VariableDeclarationKind.Const ||
utils.hasModifier(declarationList.parent!.modifiers, ts.SyntaxKind.ExportKeyword)) {
utils.hasModifier(declarationList.parent!.modifiers, ts.SyntaxKind.ExportKeyword, ts.SyntaxKind.DeclareKeyword)) {

declarationInfo = {
canBeConst: false,
isBlockScoped: true,
isBlockScoped: kind !== utils.VariableDeclarationKind.Var,
};
} else {
declarationInfo = {
Expand Down
5 changes: 5 additions & 0 deletions test/rules/prefer-const/default/ambient.d.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// no errors in declaration files
let foo = 0;
namespace bar {
let foo = 0;
}
9 changes: 8 additions & 1 deletion test/rules/prefer-const/default/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,11 @@ for (let i = 1, l = arr.length; i < l; ++i) {

// cannot fix, because of uninitialized variables
let uninitialized;
let initialized1 = 0, uninitialized2;
let initialized1 = 0, uninitialized2;

// ambient declarations are not checked
declare let ambient = 0;

declare namespace ambient {
let foo = 0;
}
9 changes: 8 additions & 1 deletion test/rules/prefer-const/default/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,11 @@ let uninitialized;
~~~~~~~~~~~~~ [Identifier 'uninitialized' is never reassigned; use 'const' instead of 'let'.]
let initialized1 = 0, uninitialized2;
~~~~~~~~~~~~ [Identifier 'initialized1' is never reassigned; use 'const' instead of 'let'.]
~~~~~~~~~~~~~~ [Identifier 'uninitialized2' is never reassigned; use 'const' instead of 'let'.]
~~~~~~~~~~~~~~ [Identifier 'uninitialized2' is never reassigned; use 'const' instead of 'let'.]

// ambient declarations are not checked
declare let ambient = 0;

declare namespace ambient {
let foo = 0;
}