From bb6c7bc1e672ea598c7e1f08046d79b53e1ccf6e Mon Sep 17 00:00:00 2001 From: Paul Molluzzo Date: Sat, 27 Jan 2018 08:19:57 -0500 Subject: [PATCH] feat(data-point-codemods): Add codemod for changing PathReducer from $. to $ (#183) closes #179 --- packages/data-point-codemods/README.md | 1 + ...-accessing-root-path-double-quote.input.js | 22 +++++++ ...accessing-root-path-double-quote.output.js | 22 +++++++ ...-accessing-root-path-single-quote.input.js | 21 +++++++ ...accessing-root-path-single-quote.output.js | 21 +++++++ ...e-path-reducer-accessing-root-path.test.js | 21 +++++++ ...change-path-reducer-accessing-root-path.js | 62 +++++++++++++++++++ 7 files changed, 170 insertions(+) create mode 100644 packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-double-quote.input.js create mode 100644 packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-double-quote.output.js create mode 100644 packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-single-quote.input.js create mode 100644 packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-single-quote.output.js create mode 100644 packages/data-point-codemods/transforms/__tests__/change-path-reducer-accessing-root-path.test.js create mode 100644 packages/data-point-codemods/transforms/change-path-reducer-accessing-root-path.js diff --git a/packages/data-point-codemods/README.md b/packages/data-point-codemods/README.md index c609be65..2df82360 100644 --- a/packages/data-point-codemods/README.md +++ b/packages/data-point-codemods/README.md @@ -38,6 +38,7 @@ data-point-codemods --help - ([codemod](transforms/reducer-args-acc-to-val-acc.js)) Refactor ReducerFunctions for value as first parameter eg. `(acc)` → `(input, acc)`. For more info please look at the [input](transforms/__testfixtures__/reducer-args-acc-to-val-acc.input.js)/[output](transforms/__testfixtures__/reducer-args-acc-to-val-acc.output.js) tests. +- ([codemod](transforms/change-path-reducer-accessing-root-path.js)) Refactor PathReducers that access the root with `$.` to use `$`. For more info please look at the [input](transforms/__testfixtures__/change-path-reducer-accessing-root-path.input.js)/[output](transforms/__testfixtures__/change-path-reducer-accessing-root-path.output.js) tests. ## WARNING diff --git a/packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-double-quote.input.js b/packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-double-quote.input.js new file mode 100644 index 00000000..125d4091 --- /dev/null +++ b/packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-double-quote.input.js @@ -0,0 +1,22 @@ +/* eslint-disable */ +/* eslint-disable prettier */ + +// prettier-ignore +const entities = { + 'transform:a': `$.`, + 'transform:b': `$. | foo | $.`, + 'transform:c': `foo | bar | baz`, + 'transform:d': "$.", + 'transform:e': "$. | foo", + 'transform:f': "$. | $. | $.", + 'transform:g': "foo | bar | baz", + nested: { + object: { + a: "$. | $." + }, + array: ["$.", "'$.", "$."] + } +} + +// prettier-ignore +dataPoint.transform("foo | $. | baz | $.") diff --git a/packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-double-quote.output.js b/packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-double-quote.output.js new file mode 100644 index 00000000..12624ba8 --- /dev/null +++ b/packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-double-quote.output.js @@ -0,0 +1,22 @@ +/* eslint-disable */ +/* eslint-disable prettier */ + +// prettier-ignore +const entities = { + 'transform:a': `$`, + 'transform:b': `$ | foo | $`, + 'transform:c': `foo | bar | baz`, + 'transform:d': "$", + 'transform:e': "$ | foo", + 'transform:f': "$ | $ | $", + 'transform:g': "foo | bar | baz", + nested: { + object: { + a: "$ | $" + }, + array: ["$", "'$", "$"] + } +} + +// prettier-ignore +dataPoint.transform("foo | $ | baz | $") diff --git a/packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-single-quote.input.js b/packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-single-quote.input.js new file mode 100644 index 00000000..bbe82741 --- /dev/null +++ b/packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-single-quote.input.js @@ -0,0 +1,21 @@ +/* eslint-disable */ +/* eslint-disable prettier */ + +// prettier-ignore +const entities = { + 'transform:a': `$.`, + 'transform:b': `$. | foo | $.`, + 'transform:c': `foo | bar | baz`, + 'transform:d': '$.', + 'transform:e': '$. | foo', + 'transform:f': '$. | $. | $.', + 'transform:g': 'foo | bar | baz', + nested: { + object: { + a: '$. | $.' + }, + array: ['$.', '$.', '$.'] + } +} + +dataPoint.transform('foo | $. | baz | $.') diff --git a/packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-single-quote.output.js b/packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-single-quote.output.js new file mode 100644 index 00000000..d56ce5aa --- /dev/null +++ b/packages/data-point-codemods/transforms/__testfixtures__/change-path-reducer-accessing-root-path-single-quote.output.js @@ -0,0 +1,21 @@ +/* eslint-disable */ +/* eslint-disable prettier */ + +// prettier-ignore +const entities = { + 'transform:a': `$`, + 'transform:b': `$ | foo | $`, + 'transform:c': `foo | bar | baz`, + 'transform:d': '$', + 'transform:e': '$ | foo', + 'transform:f': '$ | $ | $', + 'transform:g': 'foo | bar | baz', + nested: { + object: { + a: '$ | $' + }, + array: ['$', '$', '$'] + } +} + +dataPoint.transform('foo | $ | baz | $') diff --git a/packages/data-point-codemods/transforms/__tests__/change-path-reducer-accessing-root-path.test.js b/packages/data-point-codemods/transforms/__tests__/change-path-reducer-accessing-root-path.test.js new file mode 100644 index 00000000..551f84b4 --- /dev/null +++ b/packages/data-point-codemods/transforms/__tests__/change-path-reducer-accessing-root-path.test.js @@ -0,0 +1,21 @@ +/* eslint-env jest */ + +'use strict' + +const { defineTest } = require('jscodeshift/dist/testUtils') + +// single quote +defineTest( + __dirname, + 'change-path-reducer-accessing-root-path', + null, + 'change-path-reducer-accessing-root-path-single-quote' +) + +// double quote +defineTest( + __dirname, + 'change-path-reducer-accessing-root-path', + null, + 'change-path-reducer-accessing-root-path-double-quote' +) diff --git a/packages/data-point-codemods/transforms/change-path-reducer-accessing-root-path.js b/packages/data-point-codemods/transforms/change-path-reducer-accessing-root-path.js new file mode 100644 index 00000000..d21c8099 --- /dev/null +++ b/packages/data-point-codemods/transforms/change-path-reducer-accessing-root-path.js @@ -0,0 +1,62 @@ +module.exports = (file, api) => { + const j = api.jscodeshift + const rootPathWithDotRegex = /\$\.(\s|$)/g + + const root = j(file.source) + + function detectQuoteStyle (j, root) { + let detectedQuoting = 'single' + + root + .find(j.Literal, { + value: v => typeof v === 'string', + raw: v => typeof v === 'string' + }) + .forEach(p => { + // The raw value is from the original babel source + if (p.value.raw[0] === "'") { + detectedQuoting = 'single' + } + + if (p.value.raw[0] === '"') { + detectedQuoting = 'double' + } + }) + + return detectedQuoting + } + + // transforms a string from '$. | $. | $.' -> '$ | $ | $' + function transformLiteral (node) { + const originalValue = node.value.value + if (rootPathWithDotRegex.test(originalValue)) { + node.value.value = node.value.value.replace(rootPathWithDotRegex, '$$$1') + } + } + + // transforms a template literal from `$. | $. | $.` -> `$ | $ | $` + function transformTemplateElement (node) { + const originalValue = node.value.value.raw + if (rootPathWithDotRegex.test(originalValue)) { + node.value.value.raw = node.value.value.raw.replace( + rootPathWithDotRegex, + '$$$1' + ) + } + } + + function refactorReducerMatches (nodeType, transform) { + root.find(nodeType).forEach(transform) + } + + refactorReducerMatches(j.Literal, transformLiteral) + refactorReducerMatches(j.TemplateElement, transformTemplateElement) + + // As Recast is not preserving original quoting, we try to detect it, + // and default to something sane. + // See https://github.com/benjamn/recast/issues/171 + // and https://github.com/facebook/jscodeshift/issues/143 + // credit to @skovhus: https://github.com/avajs/ava-codemods/pull/28 + const quote = detectQuoteStyle(j, root) + return root.toSource({ quote }) +}