From cdb6f009afee9583a82e069e704d118c17a1026c Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 1 May 2020 16:46:50 +0300 Subject: [PATCH] refactor(@putout/engine-runner) watermark: Set --- .madrun.js | 1 + package.json | 4 +- .../engine-runner/lib/replace/find-path.js | 22 ++++-- packages/engine-runner/lib/replace/index.js | 8 +- .../engine-runner/lib/replace/watermark.js | 74 ++++++++++--------- packages/engine-runner/package.json | 3 +- packages/engine-runner/test/replace.js | 36 +++++++++ .../lib/exports/index.spec.js | 16 ++-- .../lib/require/index.spec.js | 18 ++--- .../test/convert-commonjs-to-esm.js | 6 +- .../test/fixture/only-only-fix.js | 2 +- .../test/fixture/skip-skip-fix.js | 2 +- 12 files changed, 125 insertions(+), 67 deletions(-) diff --git a/.madrun.js b/.madrun.js index 6230733ded..2b7a0fb3a3 100644 --- a/.madrun.js +++ b/.madrun.js @@ -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')}`, diff --git a/package.json b/package.json index a1cfc61fd2..3e08443c99 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" diff --git a/packages/engine-runner/lib/replace/find-path.js b/packages/engine-runner/lib/replace/find-path.js index 4bc52d4e82..624eab5651 100644 --- a/packages/engine-runner/lib/replace/find-path.js +++ b/packages/engine-runner/lib/replace/find-path.js @@ -1,6 +1,7 @@ 'use strict'; const {entries} = Object; +const {isArray} = Array; module.exports = (parentPath) => { let current = { @@ -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 ''; } diff --git a/packages/engine-runner/lib/replace/index.js b/packages/engine-runner/lib/replace/index.js index c7f468d53a..55d9f07165 100644 --- a/packages/engine-runner/lib/replace/index.js +++ b/packages/engine-runner/lib/replace/index.js @@ -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)) @@ -85,7 +87,7 @@ const fix = (from, to, path) => { path: newPath, }); - watermark.set(from, to, path); + mark.add(); }; const getFix = (items) => (path) => { diff --git a/packages/engine-runner/lib/replace/watermark.js b/packages/engine-runner/lib/replace/watermark.js index 699c69203d..56eb88a520 100644 --- a/packages/engine-runner/lib/replace/watermark.js +++ b/packages/engine-runner/lib/replace/watermark.js @@ -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, @@ -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; -}; +} diff --git a/packages/engine-runner/package.json b/packages/engine-runner/package.json index e0890baebb..cda94a6fd7 100644 --- a/packages/engine-runner/package.json +++ b/packages/engine-runner/package.json @@ -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", diff --git a/packages/engine-runner/test/replace.js b/packages/engine-runner/test/replace.js index f47fb3be0b..c3b33f0f03 100644 --- a/packages/engine-runner/test/replace.js +++ b/packages/engine-runner/test/replace.js @@ -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(); +}); + diff --git a/packages/plugin-convert-commonjs-to-esm/lib/exports/index.spec.js b/packages/plugin-convert-commonjs-to-esm/lib/exports/index.spec.js index ca62ee4d3f..170fb64891 100644 --- a/packages/plugin-convert-commonjs-to-esm/lib/exports/index.spec.js +++ b/packages/plugin-convert-commonjs-to-esm/lib/exports/index.spec.js @@ -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(); }); diff --git a/packages/plugin-convert-commonjs-to-esm/lib/require/index.spec.js b/packages/plugin-convert-commonjs-to-esm/lib/require/index.spec.js index 99da5f4b8e..6a9408c9ee 100644 --- a/packages/plugin-convert-commonjs-to-esm/lib/require/index.spec.js +++ b/packages/plugin-convert-commonjs-to-esm/lib/require/index.spec.js @@ -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(); }); diff --git a/packages/plugin-convert-commonjs-to-esm/test/convert-commonjs-to-esm.js b/packages/plugin-convert-commonjs-to-esm/test/convert-commonjs-to-esm.js index 1742e0d98d..99d322a1f1 100644 --- a/packages/plugin-convert-commonjs-to-esm/test/convert-commonjs-to-esm.js +++ b/packages/plugin-convert-commonjs-to-esm/test/convert-commonjs-to-esm.js @@ -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(); }); diff --git a/packages/plugin-remove-only/test/fixture/only-only-fix.js b/packages/plugin-remove-only/test/fixture/only-only-fix.js index fd020bd973..7ca8e3fe7d 100644 --- a/packages/plugin-remove-only/test/fixture/only-only-fix.js +++ b/packages/plugin-remove-only/test/fixture/only-only-fix.js @@ -1,4 +1,4 @@ -test('should pass', (t) => { +test.only('should pass', (t) => { t.pass('ok'); t.end(); }); diff --git a/packages/plugin-remove-skip/test/fixture/skip-skip-fix.js b/packages/plugin-remove-skip/test/fixture/skip-skip-fix.js index fd020bd973..72bfe5080d 100644 --- a/packages/plugin-remove-skip/test/fixture/skip-skip-fix.js +++ b/packages/plugin-remove-skip/test/fixture/skip-skip-fix.js @@ -1,4 +1,4 @@ -test('should pass', (t) => { +test.skip('should pass', (t) => { t.pass('ok'); t.end(); });