Skip to content

Commit

Permalink
Fix ASI problem
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 13, 2021
1 parent 87911db commit 038279e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
7 changes: 5 additions & 2 deletions rules/no-array-push-push.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const {hasSideEffect, isCommaToken, isOpeningParenToken} = require('eslint-utils');
const {hasSideEffect, isCommaToken, isOpeningParenToken, isSemicolonToken} = require('eslint-utils');
const getDocumentationUrl = require('./utils/get-documentation-url');
const methodSelector = require('./utils/method-selector');

Expand Down Expand Up @@ -83,9 +83,12 @@ function create(context) {
));
}

const shouldKeepSemicolon = !isSemicolonToken(sourceCode.getLastToken(firstExpression)) &&
isSemicolonToken(sourceCode.getLastToken(secondExpression));

yield fixer.replaceTextRange(
[firstExpression.range[1], secondExpression.range[1]],
''
shouldKeepSemicolon ? ';' : ''
);
};

Expand Down
6 changes: 6 additions & 0 deletions test/no-array-push-push.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ test.visualize({
foo.push(1);
foo.push(2);
}
`,
// ASI
outdent`
foo.push(1)
foo.push(2)
;[foo].forEach(bar)
`
]
});
20 changes: 20 additions & 0 deletions test/snapshots/no-array-push-push.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,23 @@ Generated by [AVA](https://avajs.dev).
| ^^^^ Do not call `Array#push()` multiple times.␊
4 | }␊
`

## Invalid #21
1 | foo.push(1)
2 | foo.push(2)
3 | ;[foo].forEach(bar)

> Output
`␊
1 | foo.push(1, 2);[foo].forEach(bar)␊
`

> Error 1/1
`␊
1 | foo.push(1)␊
> 2 | foo.push(2)␊
| ^^^^ Do not call `Array#push()` multiple times.␊
3 | ;[foo].forEach(bar)␊
`
Binary file modified test/snapshots/no-array-push-push.js.snap
Binary file not shown.

0 comments on commit 038279e

Please sign in to comment.