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

Fix: prefer-namespace-keyword false positive (fixes #127) #132

Merged
merged 1 commit into from
Nov 1, 2018
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
62 changes: 22 additions & 40 deletions lib/rules/prefer-namespace-keyword.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,32 @@ module.exports = {
create(context) {
const sourceCode = context.getSourceCode();

//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------

/**
* Determines if node is a TypeScript module declaration (instead of a namespace/module).
* @param {ASTNode} node the node to be evaluated.
* @returns {boolean} true when node is an external declaration.
* @private
*/
function isTypeScriptModuleDeclaration(node) {
return node.id && node.id.type === "Literal";
}

/**
* Gets the start index of the keyword `module`.
* @param {TSNode} node the node to be evaluated.
* @returns {number} the start index.
* @private
*/
function getStartIndex(node) {
if (
node.modifiers &&
node.modifiers.length > 0 &&
node.modifiers[0].type === "TSDeclareKeyword"
) {
return node.range[0] + "declare".length + 1;
}
return node.range[0];
}

//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
return {
TSModuleDeclaration(node) {
const declaration = sourceCode.getText(node);
// Get tokens of the declaration header.
const firstToken = sourceCode.getFirstToken(node);
const tokens = [firstToken].concat(
sourceCode.getTokensBetween(
firstToken,
sourceCode.getFirstToken(node.body)
)
);

if (
isTypeScriptModuleDeclaration(node) ||
/\bnamespace\b/.test(declaration)
) {
// 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") {
return;
}

Expand All @@ -77,10 +61,8 @@ module.exports = {
message:
"Use 'namespace' instead of 'module' to declare custom TypeScript modules",
fix(fixer) {
const start = getStartIndex(node);

return fixer.replaceTextRange(
[start, start + "module".length],
return fixer.replaceText(
moduleKeywordToken,
"namespace"
);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/rules/prefer-namespace-keyword.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ ruleTester.run("prefer-namespace-keyword", rule, {
valid: [
"declare module 'foo' { }",
"namespace foo { }",
"declare namespace foo { }"
"declare namespace foo { }",
"declare global { }"
],
invalid: [
{
Expand Down