Skip to content

Commit

Permalink
refactor(@putout/engine-runner) watermark: Set
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed May 1, 2020
1 parent 387a13f commit cdb6f00
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 67 deletions.
1 change: 1 addition & 0 deletions .madrun.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const dirs = getDirs(workspaces);

module.exports = {
'test': () => `FORCE_COLOR=3 ${run('test:fast')}`,
'test:fail': () => `${run('test')} | tap-pessimist`,
'test:fast': () => `tape '${dirs}/*/test/*.js' '${dirs}/*/lib/**/*.spec.js'`,
'test:slow': () => 'FORCE_COLOR=3 lerna run test',
'coverage:long': () => `FORCE_COLOR=3 nyc --check-coverage ${run('test:fast')}`,
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"private": true,
"scripts": {
"test": "madrun test",
"test:fail": "madrun test:fail",
"test:fast": "madrun test:fast",
"test:slow": "madrun test:slow",
"coverage:long": "madrun coverage:long",
Expand Down Expand Up @@ -35,7 +36,8 @@
"husky": "^4.2.1",
"lerna": "^3.8.5",
"madrun": "^5.2.0",
"supertape": "^1.2.4"
"supertape": "^1.2.4",
"tap-pessimist": "^1.0.1"
},
"engines": {
"node": ">=8.3"
Expand Down
22 changes: 15 additions & 7 deletions packages/engine-runner/lib/replace/find-path.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {entries} = Object;
const {isArray} = Array;

module.exports = (parentPath) => {
let current = {
Expand All @@ -9,19 +10,26 @@ module.exports = (parentPath) => {
const path = [];

while (current = current.parentPath) {
path.unshift(findKey(current, current.parentPath));
path.unshift(findKey(current, current.parent));
}

return path.join('.');
};
function findKey(next, parentPath) {
parentPath = parentPath || {};
function findKey(path, parent) {
const {node} = path;

for (const [key, value] of entries(parentPath)) {
if (value === next)
for (const [key, value] of entries(parent)) {
if (isArray(value)) {
const index = value.indexOf(node);

if (index >= 0)
return `${key}.${index}`;

continue;
}

if (value === node)
return key;
}

return '';
}

8 changes: 5 additions & 3 deletions packages/engine-runner/lib/replace/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ const parseTo = (to, values, path) => isFn(to) ? to(values, path) : to;
const fix = (from, to, path) => {
const nodeFrom = template.ast(from);

watermark.init(from, to, path);
const mark = watermark(from, to, path);

if (watermark.has(from, to, path))
mark.init();

if (mark.has())
return;

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

watermark.set(from, to, path);
mark.add();
};

const getFix = (items) => (path) => {
Expand Down
74 changes: 41 additions & 33 deletions packages/engine-runner/lib/replace/watermark.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
'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,
};
};
const wraptile = require('wraptile');

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

const file = path.findParent(isProgram);
file._putout = file._putout || new Map();
};
const findPath = require('./find-path');

module.exports.set = (from, to, path) => {
module.exports = (from, to, path) => {
const {
watermark,
highWatermark,
Expand All @@ -29,30 +14,53 @@ module.exports.set = (from, to, path) => {
to,
path,
);
const file = path.findParent(isProgram);
const program = path.findParent(isProgram);

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

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

return {
init: wraptile(init, options),
has: wraptile(has, options),
add: wraptile(add, options),
};
};

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

return {
watermark,
highWatermark,
};
}

module.exports.init = init;
function init({path, program}) {
path._putout = path._putout || new Set();
program._putout = program._putout || new Set();
}

module.exports.add = add;
function add({path, program, watermark, highWatermark}) {
path._putout.add(watermark);
program._putout.add(highWatermark);
}

module.exports.has = has;
function has({path, program, watermark, highWatermark}) {
if (path._putout.has(watermark))
return true;

const file = path.findParent(isProgram);

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

return false;
};
}

3 changes: 2 additions & 1 deletion packages/engine-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"jessy": "^3.0.0",
"nessy": "^3.0.0",
"once": "^1.4.0",
"try-catch": "^3.0.0"
"try-catch": "^3.0.0",
"wraptile": "^3.0.0"
},
"keywords": [
"putout",
Expand Down
36 changes: 36 additions & 0 deletions packages/engine-runner/test/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,39 @@ test('putout: runner: replace: same', (t) => {
t.deepEqual(code, expected, 'should equal');
t.end();
});

test('putout: runner: replace: same path, new transform', (t) => {
const convert = {
report: () => '',
replace: () => ({
'module.exports.__a = __b': 'export const __a = __b',
}),
};

const source = [
'module.exports.set = () => {',
'};',
'',
'module.exports.get = () => {',
'}',
].join('\n');

const {code} = putout(source, {
runPlugins,
plugins: [
['convert', convert],
],
});

const expected = [
'export const set = () => {',
'};',
'',
'export const get = () => {',
'};',
].join('\n');

t.deepEqual(code, expected, 'should equal');
t.end();
});

Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,42 @@ const test = require('@putout/test')(__dirname, {
'convert-commonjs-to-esm/exports': convert,
});

test('plugin-convert-esm-to-commonjs: exports: transform: report', (t) => {
test('plugin-convert-commonjs-to-esm: exports: transform: report', (t) => {
t.report('exports', 'ESM should be used insted of Commonjs');
t.end();
});

test('plugin-convert-esm-to-commonjs: exports: transform', (t) => {
test('plugin-convert-commonjs-to-esm: exports: transform', (t) => {
t.transform('exports');
t.end();
});

test('plugin-convert-esm-to-commonjs: exports: transform: string', (t) => {
test('plugin-convert-commonjs-to-esm: exports: transform: string', (t) => {
t.transform('exports-string');
t.end();
});

test('plugin-convert-esm-to-commonjs: exports: transform: named', (t) => {
test('plugin-convert-commonjs-to-esm: exports: transform: named', (t) => {
t.transform('named');
t.end();
});

test('plugin-convert-esm-to-commonjs: exports: transform: multi', (t) => {
test('plugin-convert-commonjs-to-esm: exports: transform: multi', (t) => {
t.transform('multi');
t.end();
});

test('plugin-convert-esm-to-commonjs: exports: transform: no member expression', (t) => {
test('plugin-convert-commonjs-to-esm: exports: transform: no member expression', (t) => {
t.noTransform('no-member-expression');
t.end();
});

test('plugin-convert-esm-to-commonjs: exports: transform: no exports', (t) => {
test('plugin-convert-commonjs-to-esm: exports: transform: no exports', (t) => {
t.noTransform('no-exports');
t.end();
});

test('plugin-convert-esm-to-commonjs: exports: transform: sequance', (t) => {
test('plugin-convert-commonjs-to-esm: exports: transform: sequance', (t) => {
t.noTransform('sequence');
t.end();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,47 @@ const test = require('@putout/test')(__dirname, {
'convert-commonjs-to-esm/require': convert,
});

test('plugin-convert-esm-to-commonjs: require: transform: report', (t) => {
test('plugin-convert-commonjs-to-esm: require: transform: report', (t) => {
t.report('require', 'ESM should be used insted of Commonjs');
t.end();
});

test('plugin-convert-esm-to-commonjs: require: transform', (t) => {
test('plugin-convert-commonjs-to-esm: require: transform', (t) => {
t.transform('require');
t.end();
});

test('plugin-convert-esm-to-commonjs: require: transform: destructuring', (t) => {
test('plugin-convert-commonjs-to-esm: require: transform: destructuring', (t) => {
t.transform('destructuring');
t.end();
});

test('plugin-convert-esm-to-commonjs: require: transform: comment', (t) => {
test('plugin-convert-commonjs-to-esm: require: transform: comment', (t) => {
t.transform('comment');
t.end();
});

test('plugin-convert-esm-to-commonjs: require: transform: relative', (t) => {
test('plugin-convert-commonjs-to-esm: require: transform: relative', (t) => {
t.transform('relative');
t.end();
});

test('plugin-convert-esm-to-commonjs: require: transform: no require', (t) => {
test('plugin-convert-commonjs-to-esm: require: transform: no require', (t) => {
t.noTransform('no-require');
t.end();
});

test('plugin-convert-esm-to-commonjs: require: transform: no call', (t) => {
test('plugin-convert-commonjs-to-esm: require: transform: no call', (t) => {
t.noTransform('no-call');
t.end();
});

test('plugin-convert-esm-to-commonjs: require: no transform: not literal argument', (t) => {
test('plugin-convert-commonjs-to-esm: require: no transform: not literal argument', (t) => {
t.noTransform('not-literal-argument');
t.end();
});

test('plugin-convert-esm-to-commonjs: require: no report: not literal argument', (t) => {
test('plugin-convert-commonjs-to-esm: require: no report: not literal argument', (t) => {
t.noReport('not-literal-argument');
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ const test = require('@putout/test')(__dirname, {
'convert-commonjs-to-esm': convert,
});

test('plugin-convert-esm-to-commonjs: transform: report', (t) => {
test('plugin-convert-commonjs-to-esm: transform: report', (t) => {
t.report('exports', 'ESM should be used insted of Commonjs');
t.end();
});

test('plugin-convert-esm-to-commonjs: transform: export', (t) => {
test('plugin-convert-commonjs-to-esm: transform: export', (t) => {
t.transform('exports');
t.end();
});

test('plugin-convert-esm-to-commonjs: transform: export: string', (t) => {
test('plugin-convert-commonjs-to-esm: transform: export: string', (t) => {
t.transform('exports-string');
t.end();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-remove-only/test/fixture/only-only-fix.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test('should pass', (t) => {
test.only('should pass', (t) => {
t.pass('ok');
t.end();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-remove-skip/test/fixture/skip-skip-fix.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test('should pass', (t) => {
test.skip('should pass', (t) => {
t.pass('ok');
t.end();
});
Expand Down

0 comments on commit cdb6f00

Please sign in to comment.