Skip to content
This repository has been archived by the owner on May 24, 2019. It is now read-only.

Commit

Permalink
Pretty: Remove line containing multiple removed tokens (#34)
Browse files Browse the repository at this point in the history
When --pretty mode is enabled, if multiple tokens exist on a line and all are removed, then the full line should be removed as well. This PR adds that behavior.
  • Loading branch information
leebyron authored Jan 27, 2017
1 parent 9d79769 commit cb5ec8c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
54 changes: 50 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,7 @@ function getTrailingLineNode(context, node) {
}
end++;

var ast = state.ast;
var idx = findTokenIndex(ast.tokens, node.start);
var prevToken = ast.tokens[idx - 1];
if (prevToken.loc.end.line !== node.loc.end.line) {
if (isLastNodeRemovedFromLine(context, node)) {
return {
start: start,
end: end,
Expand All @@ -297,6 +294,55 @@ function getTrailingLineNode(context, node) {
}
}

// Returns true if node is the last to be removed from a line.
function isLastNodeRemovedFromLine(context, node) {
var tokens = context.ast.tokens;
var priorTokenIdx = findTokenIndex(tokens, node.start) - 1;
var token = tokens[priorTokenIdx];
var line = node.loc.end.line;

// Find previous token that was not removed on the same line.
while (priorTokenIdx >= 0 &&
token.loc.end.line === line &&
isRemovedToken(context, token)) {
token = tokens[--priorTokenIdx]
}

// If there's no prior token (start of file), or the prior token is on another
// line, this line must be fully removed.
return !token || token.loc.end.line !== line;
}

// Returns true if the provided token was previously marked as removed.
function isRemovedToken(context, token) {
var removedNodes = context.removedNodes;
var nodeIdx = removedNodes.length - 1;

// Find the last removed node which could possibly contain this token.
while (nodeIdx >= 0 && removedNodes[nodeIdx].start > token.start) {
nodeIdx--;
}

var node = removedNodes[nodeIdx];

// This token couldn't be removed if not contained within the removed node.
if (nodeIdx === -1 || node.end < token.end) {
return false;
}

// Iterate through the tokens contained by the removed node to find a match.
var tokens = context.ast.tokens;
var tokenIdx = findTokenIndex(tokens, node.start);
while (tokens[tokenIdx].end <= node.end) {
if (token === tokens[tokenIdx]) {
return true;
}
tokenIdx++;
}

return false;
}

// Given the AST output of babylon parse, walk through in a depth-first order,
// calling methods on the given visitor, providing context as the first argument.
function visit(ast, context, visitor) {
Expand Down
3 changes: 1 addition & 2 deletions test/expected-pretty-inlinemap.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion test/expected-pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Regular import
import {
Something,

} from 'some-module';

// Import types
Expand Down
1 change: 0 additions & 1 deletion test/expected-with-maps/test/source.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/expected-with-maps/test/source.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cb5ec8c

Please sign in to comment.