Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Fix issue with declare module without body in prefer-namespace-keyword #195

Merged
merged 4 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
53 changes: 20 additions & 33 deletions lib/rules/prefer-namespace-keyword.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @fileoverview Enforces the use of the keyword `namespace` over `module` to declare custom TypeScript modules.
* @author Patricio Trevino
* @author Armano <https://github.com/armano2>
*/
"use strict";

Expand Down Expand Up @@ -32,41 +33,27 @@ module.exports = {
//----------------------------------------------------------------------
return {
TSModuleDeclaration(node) {
// Get tokens of the declaration header.
const firstToken = sourceCode.getFirstToken(node);
const tokens = [firstToken].concat(
sourceCode.getTokensBetween(
firstToken,
sourceCode.getFirstToken(node.body)
)
);

// Get 'module' token and the next one.
const moduleKeywordIndex = tokens.findIndex(
t => t.type === "Identifier" && t.value === "module"
);
const moduleKeywordToken =
moduleKeywordIndex === -1
? null
: tokens[moduleKeywordIndex];
const moduleNameToken = tokens[moduleKeywordIndex + 1];

// Do nothing if the 'module' token was not found or the module name is a string.
if (!moduleKeywordToken || moduleNameToken.type === "String") {
// Do nothing if the name is a string.
if (!node.id || node.id.type === "Literal") {
return;
}

context.report({
node,
message:
"Use 'namespace' instead of 'module' to declare custom TypeScript modules",
fix(fixer) {
return fixer.replaceText(
moduleKeywordToken,
"namespace"
);
},
});
// Get tokens of the declaration header.
const moduleType = sourceCode.getTokenBefore(node.id);

if (
moduleType &&
moduleType.type === "Identifier" &&
moduleType.value === "module"
) {
context.report({
node,
message:
"Use 'namespace' instead of 'module' to declare custom TypeScript modules",
fix(fixer) {
return fixer.replaceText(moduleType, "namespace");
},
});
}
},
};
},
Expand Down
17 changes: 9 additions & 8 deletions tests/lib/rules/prefer-namespace-keyword.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @fileoverview Enforces the use of the keyword `namespace` over `module` to declare custom TypeScript modules.
* @author Patricio Trevino
* @author Armano <https://github.com/armano2>
*/
"use strict";

Expand All @@ -21,6 +22,7 @@ const ruleTester = new RuleTester({

ruleTester.run("prefer-namespace-keyword", rule, {
valid: [
"declare module 'foo'",
"declare module 'foo' { }",
"namespace foo { }",
"declare namespace foo { }",
Expand All @@ -29,23 +31,23 @@ ruleTester.run("prefer-namespace-keyword", rule, {
invalid: [
{
code: "module foo { }",
output: "namespace foo { }",
errors: [
{
message:
"Use 'namespace' instead of 'module' to declare custom TypeScript modules",
output: "namespace foo { }",
row: 1,
column: 1,
},
],
},
{
code: "declare module foo { }",
output: "declare namespace foo { }",
errors: [
{
message:
"Use 'namespace' instead of 'module' to declare custom TypeScript modules",
output: "declare namespace foo { }",
row: 1,
column: 1,
},
Expand All @@ -55,24 +57,23 @@ ruleTester.run("prefer-namespace-keyword", rule, {
code: `
declare module foo {
declare module bar { }
}
`,
output: `
declare namespace foo {
declare namespace bar { }
}
`,
errors: [
{
message:
"Use 'namespace' instead of 'module' to declare custom TypeScript modules",
output: "declare namespace foo { }",
row: 2,
column: 1,
},
{
message:
"Use 'namespace' instead of 'module' to declare custom TypeScript modules",
output: `
declare namespace foo {
declare namespace bar { }
}
`,
row: 3,
column: 5,
},
Expand Down