From 58d8f91eb2a0018c200bb4dd90b7594daaf30a9d Mon Sep 17 00:00:00 2001 From: Marcosld Date: Sat, 28 Mar 2020 21:28:03 +0100 Subject: [PATCH] Fix/require exact type nested (#442) * fix: require-exact-type for interfaces * fix: require-exact-type for interfaces, add more tests --- src/rules/requireExactType.js | 2 +- tests/rules/assertions/requireExactType.js | 72 ++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/rules/requireExactType.js b/src/rules/requireExactType.js index 1039bd45..c49975dc 100644 --- a/src/rules/requireExactType.js +++ b/src/rules/requireExactType.js @@ -17,7 +17,7 @@ const create = (context) => { ObjectTypeAnnotation (node) { const {exact, indexers} = node; - if (always && !exact && indexers.length === 0) { + if (node.parent.type !== 'InterfaceDeclaration' && always && !exact && indexers.length === 0) { context.report({ fix: (fixer) => { return [ diff --git a/tests/rules/assertions/requireExactType.js b/tests/rules/assertions/requireExactType.js index fe4fb511..26ff36a1 100644 --- a/tests/rules/assertions/requireExactType.js +++ b/tests/rules/assertions/requireExactType.js @@ -40,6 +40,26 @@ export default { options: ['always'], output: '(foo: Array<{|bar: string|}>) => {};', }, + { + code: `interface StackFrame { + colno?: number; + lineno?: number; + filename?: string; + function?: { name: string }; + }`, + errors: [ + { + message: 'Object type must be exact.', + }, + ], + options: ['always'], + output: `interface StackFrame { + colno?: number; + lineno?: number; + filename?: string; + function?: {| name: string |}; + }`, + }, // Never @@ -93,6 +113,26 @@ export default { options: ['never'], output: '(foo: Array<{ bar: string }>) => {};', }, + { + code: `interface StackFrame { + colno?: number; + lineno?: number; + filename?: string; + function?: {| name: string |}; + }`, + errors: [ + { + message: 'Object type must not be exact.', + }, + ], + options: ['never'], + output: `interface StackFrame { + colno?: number; + lineno?: number; + filename?: string; + function?: { name: string }; + }`, + }, ], valid: [ @@ -131,6 +171,22 @@ export default { options: ['always'], }, + { + code: `interface StackFrame { + colno?: number; + lineno?: number; + filename?: string; + function?: {| name: string |}; + }`, + options: ['always'], + output: `interface StackFrame { + colno?: number; + lineno?: number; + filename?: string; + function?: {| name: string |}; + }`, + }, + // Never { @@ -153,5 +209,21 @@ export default { code: 'type foo = number;', options: ['never'], }, + + { + code: `interface StackFrame { + colno?: number; + lineno?: number; + filename?: string; + function?: {| name: string |}; + }`, + options: ['always'], + output: `interface StackFrame { + colno?: number; + lineno?: number; + filename?: string; + function?: {| name: string |}; + }`, + }, ], };