Skip to content

Commit

Permalink
feat(data-point-codemods): Add codemod for changing PathReducer from …
Browse files Browse the repository at this point in the history
…$. to $ (#183)

closes #179
  • Loading branch information
paulmolluzzo authored and acatl committed Jan 27, 2018
1 parent ca72293 commit bb6c7bc
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/data-point-codemods/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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 | $.")
Original file line number Diff line number Diff line change
@@ -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 | $")
Original file line number Diff line number Diff line change
@@ -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 | $.')
Original file line number Diff line number Diff line change
@@ -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 | $')
Original file line number Diff line number Diff line change
@@ -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'
)
Original file line number Diff line number Diff line change
@@ -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 })
}

0 comments on commit bb6c7bc

Please sign in to comment.