Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(data-point-codemods): Add codemod for changing PathReducer from $. to $ #183

Merged
merged 11 commits into from
Jan 27, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,13 @@
/* 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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* 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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* 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'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* 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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we have any tests where the $. is nested inside some other kind of reducer?

// just for example
{
  a: ['$.']
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can add that. Also something like dataPoint.transform('$. | foo', input) too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't hurt

}
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 })
}