Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): [ban-types] Support namespaced type #616

Merged
merged 3 commits into from Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 41 additions & 36 deletions packages/eslint-plugin/src/rules/ban-types.ts
@@ -1,8 +1,4 @@
import {
TSESLint,
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils';
import * as util from '../util';

type Options = [
Expand All @@ -20,6 +16,31 @@ type Options = [
];
type MessageIds = 'bannedTypeMessage';

function stringifyTypeName(
node: TSESTree.EntityName,
sourceCode: TSESLint.SourceCode,
): string {
return sourceCode.getText(node).replace(/ /g, '');
}

function getCustomMessage(
bannedType: null | string | { message?: string; fixWith?: string },
) {
if (bannedType === null) {
return '';
}

if (typeof bannedType === 'string') {
return ` ${bannedType}`;
}

if (bannedType.message) {
return ` ${bannedType.message}`;
}

return '';
}

export default util.createRule<Options, MessageIds>({
name: 'ban-types',
meta: {
Expand Down Expand Up @@ -87,39 +108,23 @@ export default util.createRule<Options, MessageIds>({
],
create(context, [{ types: bannedTypes }]) {
return {
'TSTypeReference Identifier'(node: TSESTree.Identifier) {
if (
node.parent &&
node.parent.type !== AST_NODE_TYPES.TSQualifiedName
) {
if (node.name in bannedTypes) {
let customMessage = '';
const bannedCfgValue = bannedTypes[node.name];
TSTypeReference({ typeName }) {
const name = stringifyTypeName(typeName, context.getSourceCode());

let fix: TSESLint.ReportFixFunction | null = null;
if (name in bannedTypes) {
const bannedType = bannedTypes[name];
const customMessage = getCustomMessage(bannedType);
const fixWith = bannedType && (bannedType as any).fixWith;

if (typeof bannedCfgValue === 'string') {
customMessage += ` ${bannedCfgValue}`;
} else if (bannedCfgValue !== null) {
if (bannedCfgValue.message) {
customMessage += ` ${bannedCfgValue.message}`;
}
if (bannedCfgValue.fixWith) {
const fixWith = bannedCfgValue.fixWith;
fix = fixer => fixer.replaceText(node, fixWith);
}
}

context.report({
node,
messageId: 'bannedTypeMessage',
data: {
name: node.name,
customMessage,
},
fix,
});
}
context.report({
node: typeName,
messageId: 'bannedTypeMessage',
data: {
name: name,
customMessage,
},
fix: fixWith ? fixer => fixer.replaceText(typeName, fixWith) : null,
});
}
},
};
Expand Down
47 changes: 47 additions & 0 deletions packages/eslint-plugin/tests/rules/ban-types.test.ts
Expand Up @@ -16,6 +16,10 @@ const options: InferOptionsTypeFromRule<typeof rule> = [
Object: "Use '{}' instead.",
Array: null,
F: null,
'NS.Bad': {
message: 'Use NS.Good instead.',
fixWith: 'NS.Good',
},
},
},
];
Expand All @@ -39,6 +43,14 @@ ruleTester.run('ban-types', rule, {
code: 'let e: foo.String;',
options,
},
{
code: 'let a: _.NS.Bad',
options,
},
{
code: 'let a: NS.Bad._',
options,
},
],
invalid: [
{
Expand All @@ -56,6 +68,25 @@ ruleTester.run('ban-types', rule, {
],
options,
},
{
code: 'let aa: Foo;',
errors: [
{
messageId: 'bannedTypeMessage',
data: {
name: 'Foo',
customMessage: '',
},
},
],
options: [
{
types: {
Foo: { message: '' },
},
},
],
},
{
code: 'let b: {c: String};',
output: 'let b: {c: string};',
Expand Down Expand Up @@ -217,5 +248,21 @@ class Foo<F = string> extends Bar<string> implements Baz<Object> {
],
options,
},
{
code: 'let a: NS.Bad;',
output: 'let a: NS.Good;',
errors: [
{
messageId: 'bannedTypeMessage',
data: {
name: 'NS.Bad',
customMessage: ' Use NS.Good instead.',
},
line: 1,
column: 8,
},
],
options,
},
],
});