Skip to content

Commit

Permalink
feature(@putout/plugin-extract-sequance-expressions) add ability to k…
Browse files Browse the repository at this point in the history
…eep comments
  • Loading branch information
coderaiser committed Jun 14, 2019
1 parent b90d9f1 commit 50f4f3b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

const {replaceWithMultiple} = require('putout').operate;

module.exports.report = () => 'sequence expressions should not be used';

module.exports.fix = (path) => {
const {expressions} = path.node;

path.replaceWithMultiple(expressions);
replaceWithMultiple(path, expressions);
};

module.exports.find = (ast, {push, traverse}) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-extract-sequence-expressions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"supertape": "^1.0.3"
},
"peerDependencies": {
"putout": ">=4.3.2"
"putout": ">=4.28"
},
"license": "MIT",
"engines": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// hello
t = 5;

// world
m = 2;

// how
c = 3;

Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// hello
t = 5,
m = 2;
// world
m = 2,
// how
c = 3;

9 changes: 9 additions & 0 deletions packages/putout/lib/operate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ module.exports.replaceWith = (path, node) => {
path.node.comments = comments;
};

module.exports.replaceWithMultiple = (path, nodes) => {
const {comments} = path.parentPath.node;

const newPath = path.replaceWithMultiple(nodes);
newPath[0].node.comments = comments;

return newPath;
};

module.exports.insertAfter = (path, node) => {
const {comments} = path.node;

Expand Down
47 changes: 47 additions & 0 deletions packages/putout/test/operate.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,50 @@ test('putout: operate: insertAfter: comments', (t) => {
t.end();
});

test('putout: operate: replaceWithMultiple', (t) => {
const node = {};
const comments = [];

const replaceWithMultiple = stub().returns([{
node,
}]);
const parentPath = {
node: {
comments,
},
};

const path = {
parentPath,
replaceWithMultiple,
};

operate.replaceWithMultiple(path, node);

t.ok(replaceWithMultiple.calledWith(node), 'should call reporter');
t.end();
});

test('putout: operate: replaceWithMultiple: comments', (t) => {
const node = {};
const replaceWithMultiple = stub().returns([{
node,
}]);
const comments = [];
const parentPath = {
node: {
comments,
},
};

const path = {
parentPath,
replaceWithMultiple,
};

const newPath = operate.replaceWithMultiple(path, node);

t.deepEqual(newPath[0].node.comments, comments, 'should call reporter');
t.end();
});

0 comments on commit 50f4f3b

Please sign in to comment.