Skip to content

Commit

Permalink
[FIX] declare module without body in prefer-namespace-keyword (typesc…
Browse files Browse the repository at this point in the history
…ript-eslint#195)

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

* output should be in context of test not errors
  • Loading branch information
armano2 authored and JamesHenry committed Jan 18, 2019
1 parent 87606b9 commit 43ea9f7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 41 deletions.
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
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

0 comments on commit 43ea9f7

Please sign in to comment.