-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(data-point-codemods): Add codemod for changing PathReducer from …
- Loading branch information
1 parent
ca72293
commit bb6c7bc
Showing
7 changed files
with
170 additions
and
0 deletions.
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
22 changes: 22 additions & 0 deletions
22
...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,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 | $.") |
22 changes: 22 additions & 0 deletions
22
...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,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 | $") |
21 changes: 21 additions & 0 deletions
21
...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,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 | $.') |
21 changes: 21 additions & 0 deletions
21
...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,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 | $') |
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 }) | ||
} |