-
Notifications
You must be signed in to change notification settings - Fork 34
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
acatl
merged 11 commits into
ViacomInc:master
from
paulmolluzzo:add-codemod-for-pathreducer
Jan 27, 2018
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
39f4479
feat(data-point-codemods): Add codemod for changing PathReducer from …
paulmolluzzo 2ff373e
docs(data-point-codemods): Add documentation of the new codemod to th…
paulmolluzzo 6785824
Merge branch 'master' into add-codemod-for-pathreducer
acatl d6e6ec0
refactor(data-point-codemods): Add tests for change-path-reducer-acce…
paulmolluzzo 1a33bcf
Merge branch 'add-codemod-for-pathreducer' of github.com:paulmolluzzo…
paulmolluzzo abde15f
Merge branch 'master' into add-codemod-for-pathreducer
paulmolluzzo 065119e
Merge branch 'master' into add-codemod-for-pathreducer
acatl 3e73041
refactor(data-point-codemods): Add more scenarios to tests for change…
paulmolluzzo 0aac650
Merge branch 'add-codemod-for-pathreducer' of github.com:paulmolluzzo…
paulmolluzzo 0a9063a
fix(codemods): dont prettify change-path-reducer-accessing-root-path
paulmolluzzo 508a836
Merge branch 'master' into add-codemod-for-pathreducer
acatl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...transforms/__testfixtures__/change-path-reducer-accessing-root-path-double-quote.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
13 changes: 13 additions & 0 deletions
13
...ransforms/__testfixtures__/change-path-reducer-accessing-root-path-double-quote.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
13 changes: 13 additions & 0 deletions
13
...transforms/__testfixtures__/change-path-reducer-accessing-root-path-single-quote.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
13 changes: 13 additions & 0 deletions
13
...ransforms/__testfixtures__/change-path-reducer-accessing-root-path-single-quote.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
21 changes: 21 additions & 0 deletions
21
.../data-point-codemods/transforms/__tests__/change-path-reducer-accessing-root-path.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
) |
62 changes: 62 additions & 0 deletions
62
packages/data-point-codemods/transforms/change-path-reducer-accessing-root-path.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't hurt