From 355b3b436d7fd4334284a1747843599830c47794 Mon Sep 17 00:00:00 2001 From: Simeon Cheeseman Date: Tue, 2 Apr 2019 13:18:05 +0900 Subject: [PATCH] feat: support interfaces for delimiter-dangle fix #381 --- .README/rules/delimiter-dangle.md | 4 +++- src/rules/delimiterDangle.js | 16 +++++++++++----- tests/rules/assertions/delimiterDangle.js | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/.README/rules/delimiter-dangle.md b/.README/rules/delimiter-dangle.md index 46eb1445..63282113 100644 --- a/.README/rules/delimiter-dangle.md +++ b/.README/rules/delimiter-dangle.md @@ -4,7 +4,9 @@ _The `--fix` option on the command line automatically fixes problems reported by Enforces consistent use of trailing commas in Object and Tuple annotations. -This rule takes one argument which mirrors ESLint's default `comma-dangle` rule. +This rule takes two arguments which both mirror ESLint's default `comma-dangle` rule. +The first argument is for Object and Tuble annotations. +The second argument is used for Interface annotations as ESLint's default `comma-dangle` doesn't apply to interfaces. If it is `'never'` then a problem is raised when there is a trailing comma. diff --git a/src/rules/delimiterDangle.js b/src/rules/delimiterDangle.js index 7397f5db..2a03939f 100644 --- a/src/rules/delimiterDangle.js +++ b/src/rules/delimiterDangle.js @@ -1,6 +1,10 @@ import _ from 'lodash'; const schema = [ + { + enum: ['always', 'always-multiline', 'only-multiline', 'never'], + type: 'string' + }, { enum: ['always', 'always-multiline', 'only-multiline', 'never'], type: 'string' @@ -9,6 +13,7 @@ const schema = [ const create = (context) => { const option = context.options[0] || 'never'; + const interfaceOption = context.options[1] || option; const sourceCode = context.getSourceCode(); const reporter = (node, message, fix) => { @@ -43,32 +48,33 @@ const create = (context) => { const isMultiLine = penultimateToken.loc.start.line !== lastToken.loc.start.line; const report = makeReporters(lastChildNode, penultimateToken); + const nodeOption = node.parent.type === 'InterfaceDeclaration' ? interfaceOption : option; - if (option === 'always' && !isDangling) { + if (nodeOption === 'always' && !isDangling) { report.noDangle(); return; } - if (option === 'never' && isDangling) { + if (nodeOption === 'never' && isDangling) { report.dangle(); return; } - if (option === 'always-multiline' && !isDangling && isMultiLine) { + if (nodeOption === 'always-multiline' && !isDangling && isMultiLine) { report.noDangle(); return; } - if (option === 'always-multiline' && isDangling && !isMultiLine) { + if (nodeOption === 'always-multiline' && isDangling && !isMultiLine) { report.dangle(); return; } - if (option === 'only-multiline' && isDangling && !isMultiLine) { + if (nodeOption === 'only-multiline' && isDangling && !isMultiLine) { report.dangle(); } }; diff --git a/tests/rules/assertions/delimiterDangle.js b/tests/rules/assertions/delimiterDangle.js index dd590ea9..460b22dc 100644 --- a/tests/rules/assertions/delimiterDangle.js +++ b/tests/rules/assertions/delimiterDangle.js @@ -54,6 +54,14 @@ const OBJECT_TYPE_ANNOTATION = { output: 'type X = { foo: string }' }, + // interface override + { + code: 'interface X { foo: string; }', + errors: [{message: 'Unexpected trailing delimiter'}], + options: ['always', 'never'], + output: 'interface X { foo: string }' + }, + // Only indexers... { code: 'type X = { [key: string]: number, }', @@ -269,6 +277,12 @@ const OBJECT_TYPE_ANNOTATION = { options: ['only-multiline'] }, + // interface override + { + code: 'interface X { foo: string; }', + options: ['never', 'always'] + }, + // Empty... { code: 'type X = {}',