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

fix(eslint-plugin): [ban-types] add option extendDefaults #1379

Merged
merged 4 commits into from Feb 28, 2020
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
60 changes: 32 additions & 28 deletions packages/eslint-plugin/src/rules/ban-types.ts
Expand Up @@ -13,7 +13,7 @@ type Types = Record<

type Options = [
{
types: Types;
types?: Types;
},
];
type MessageIds = 'bannedTypeMessage';
Expand Down Expand Up @@ -47,6 +47,35 @@ function getCustomMessage(
return '';
}

/*
Defaults for this rule should be treated as an "all or nothing"
merge, so we need special handling here.

See: https://github.com/typescript-eslint/typescript-eslint/issues/686
*/
const defaultTypes = {
String: {
message: 'Use string instead',
fixWith: 'string',
},
Boolean: {
message: 'Use boolean instead',
fixWith: 'boolean',
},
Number: {
message: 'Use number instead',
fixWith: 'number',
},
Object: {
message: 'Use Record<string, any> instead',
fixWith: 'Record<string, any>',
},
Symbol: {
message: 'Use symbol instead',
fixWith: 'symbol',
},
};

export default util.createRule<Options, MessageIds>({
name: 'ban-types',
meta: {
Expand Down Expand Up @@ -86,33 +115,8 @@ export default util.createRule<Options, MessageIds>({
},
],
},
defaultOptions: [
{
types: {
String: {
message: 'Use string instead',
fixWith: 'string',
},
Boolean: {
message: 'Use boolean instead',
fixWith: 'boolean',
},
Number: {
message: 'Use number instead',
fixWith: 'number',
},
Object: {
message: 'Use Record<string, any> instead',
fixWith: 'Record<string, any>',
},
Symbol: {
message: 'Use symbol instead',
fixWith: 'symbol',
},
},
},
],
create(context, [{ types }]) {
defaultOptions: [{}],
create(context, [{ types = defaultTypes }]) {
const bannedTypes: Types = Object.keys(types).reduce(
(res, type) => ({ ...res, [removeSpaces(type)]: types[type] }),
{},
Expand Down
24 changes: 24 additions & 0 deletions packages/eslint-plugin/tests/rules/ban-types.test.ts
Expand Up @@ -53,8 +53,32 @@ ruleTester.run('ban-types', rule, {
code: 'let a: NS.Bad._',
options,
},
// Replace default options instead of merging
{
code: 'let a: String;',
options: [
{
types: {
Number: {
message: 'Use number instead.',
fixWith: 'number',
},
},
},
],
},
],
invalid: [
{
code: 'let a: String;',
errors: [
{
messageId: 'bannedTypeMessage',
line: 1,
column: 8,
},
],
},
{
code: 'let a: Object;',
errors: [
Expand Down