Skip to content

Commit

Permalink
Migrate typescript-eslint-parser to 21.0.2 (typescript-eslint#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 authored and JamesHenry committed Jan 18, 2019
1 parent e59671e commit a58846c
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 397 deletions.
76 changes: 3 additions & 73 deletions packages/eslint-plugin-typescript/lib/rules/no-explicit-any.js
Expand Up @@ -18,89 +18,19 @@ module.exports = {
extraDescription: [util.tslintRule("no-any")],
category: "TypeScript",
url:
"https://github.com/nzakas/eslint-plugin-typescript/blob/master/docs/rules/no-explicit-any.md",
"https://github.com/bradzacher/eslint-plugin-typescript/blob/master/docs/rules/no-explicit-any.md",
},
schema: [],
},

create(context) {
//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------

/**
* Checks if the node has a type annotation of type any.
* @param {ASTNode} node The node being validated.
* @returns {void}
* @private
*/
function checkGenericNodeForAnnotation(node) {
if (node.type === "TSAnyKeyword") {
return {
TSAnyKeyword(node) {
context.report({
node,
message: "Unexpected any. Specify a different type.",
});
} else if (node.type === "TSArrayType") {
checkGenericNodeForAnnotation(node.elementType);
} else if (
node.type === "TSUnionType" ||
node.type === "TSIntersectionType"
) {
node.types.forEach(type => {
checkGenericNodeForAnnotation(type);
});
} else if (node.type === "TSTypeReference") {
if (node.typeParameters) {
// handles generics
node.typeParameters.params.forEach(param => {
checkGenericNodeForAnnotation(param);
});
} else if (node.typeName) {
// handles non generics
checkGenericNodeForAnnotation(node.typeName);
}
} else if (node.type === "GenericTypeAnnotation") {
if (node.typeParameters) {
node.typeParameters.params.forEach(param => {
checkGenericNodeForAnnotation(param);
});
} else {
checkGenericNodeForAnnotation(node.id);
}
}
}

/**
* Checks if a function node used the any type
* @param {ASTNode} node The node representing a function.
* @returns {void}
* @private
*/
function checkFunctionReturnTypeForAnnotation(node) {
if (node.returnType) {
checkGenericNodeForAnnotation(node.returnType.typeAnnotation);
}
}

//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
return {
Identifier(node) {
if (node.typeAnnotation) {
checkGenericNodeForAnnotation(
node.typeAnnotation.typeAnnotation
);
}
},
TSTypeAnnotation(node) {
if (node.typeAnnotation) {
checkGenericNodeForAnnotation(node.typeAnnotation);
}
},
FunctionDeclaration: checkFunctionReturnTypeForAnnotation,
FunctionExpression: checkFunctionReturnTypeForAnnotation,
ArrowFunctionExpression: checkFunctionReturnTypeForAnnotation,
};
},
};

0 comments on commit a58846c

Please sign in to comment.