Skip to content

Commit

Permalink
refactor(@putout/engine-runner) move out watermark
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed May 1, 2020
1 parent 9012d0d commit 387a13f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 15 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const {isProgram} = require('@babel/types');
const {template} = require('@putout/engine-parser');
const {replaceWith} = require('@putout/operate');
const {
Expand All @@ -10,8 +9,8 @@ const {
setValues,
} = require('@putout/compare');

const maybeArray = require('./maybe-array');
const findPath = require('./find-path');
const maybeArray = require('../maybe-array');
const watermark = require('./watermark');

const {keys, entries} = Object;

Expand Down Expand Up @@ -55,18 +54,10 @@ const parseTo = (to, values, path) => isFn(to) ? to(values, path) : to;

const fix = (from, to, path) => {
const nodeFrom = template.ast(from);
const watermark = `${from} -> ${to}`;
const highWatermark = `${watermark} :> ${findPath(path)}`;

path._putout = path._putout || [];
watermark.init(from, to, path);

const file = path.findParent(isProgram);
file._putout = file._putout || [];

if (path._putout.includes(watermark))
return;

if (file._putout.includes(highWatermark))
if (watermark.has(from, to, path))
return;

if (!compare(path, nodeFrom))
Expand Down Expand Up @@ -94,8 +85,7 @@ const fix = (from, to, path) => {
path: newPath,
});

path._putout.push(watermark);
file._putout.push(highWatermark);
watermark.set(from, to, path);
};

const getFix = (items) => (path) => {
Expand Down
58 changes: 58 additions & 0 deletions packages/engine-runner/lib/replace/watermark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

const {isProgram} = require('@babel/types');
const findPath = require('./find-path');

const create = (from, to, path) => {
const watermark = `${from} -> ${to}`;
const highWatermark = `${watermark} :> ${findPath(path)}`;

return {
watermark,
highWatermark,
};
};

module.exports.init = (from, to, path) => {
path._putout = path._putout || new Map();

const file = path.findParent(isProgram);
file._putout = file._putout || new Map();
};

module.exports.set = (from, to, path) => {
const {
watermark,
highWatermark,
} = create(
from,
to,
path,
);
const file = path.findParent(isProgram);

path._putout.set(watermark);
file._putout.set(highWatermark);
};

module.exports.has = (from, to, path) => {
const {
watermark,
highWatermark,
} = create(
from,
to,
path,
);

if (path._putout.has(watermark))
return true;

const file = path.findParent(isProgram);

if (file._putout.has(highWatermark))
return true;

return false;
};

0 comments on commit 387a13f

Please sign in to comment.