Skip to content

Commit

Permalink
feat: support interfaces for delimiter-dangle
Browse files Browse the repository at this point in the history
  • Loading branch information
SimeonC committed Apr 10, 2019
1 parent 52b0c00 commit 355b3b4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .README/rules/delimiter-dangle.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
16 changes: 11 additions & 5 deletions src/rules/delimiterDangle.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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) => {
Expand Down Expand Up @@ -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();
}
};
Expand Down
14 changes: 14 additions & 0 deletions tests/rules/assertions/delimiterDangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, }',
Expand Down Expand Up @@ -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 = {}',
Expand Down

0 comments on commit 355b3b4

Please sign in to comment.