Skip to content
This repository has been archived by the owner on Jul 19, 2019. It is now read-only.

Commit

Permalink
Merge pull request #5 from rackt/link-props
Browse files Browse the repository at this point in the history
Add transform for Link props
  • Loading branch information
taion committed Dec 7, 2015
2 parents 6e4f0d5 + 28145ca commit 329f768
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
10 changes: 4 additions & 6 deletions modules/history/deprecate-createPath-createHref-query.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import parsePath from 'history/lib/parsePath'

function replace(source, api, methodName) {
const j = api.jscodeshift

function replace(source, j, methodName) {
return j(source)
.find(j.CallExpression, {
callee: { property: { name: methodName } } }
Expand Down Expand Up @@ -41,10 +39,10 @@ function replace(source, api, methodName) {
.toSource()
}

export default (file, api) => {
export default (file, { jscodeshift: j }) => {
let { source } = file
source = replace(source, api, 'createPath')
source = replace(source, api, 'createHref')
source = replace(source, j, 'createPath')
source = replace(source, j, 'createHref')

return source
}
10 changes: 4 additions & 6 deletions modules/history/deprecate-pushState-replaceState.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import parsePath from 'history/lib/parsePath'

function replace(source, api, flavor) {
const j = api.jscodeshift

function replace(source, j, flavor) {
return j(source)
.find(j.CallExpression, {
callee: { property: { name: `${flavor}State` } } }
Expand Down Expand Up @@ -48,10 +46,10 @@ function replace(source, api, flavor) {
.toSource()
}

export default (file, api) => {
export default (file, { jscodeshift: j }) => {
let { source } = file
source = replace(source, api, 'push')
source = replace(source, api, 'replace')
source = replace(source, j, 'push')
source = replace(source, j, 'replace')

return source
}
63 changes: 63 additions & 0 deletions modules/react-router/deprecate-Link-location-props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function isDeprecatedAttribute(attribute) {
const { name } = attribute.name
return [ 'query', 'hash', 'state' ].includes(name)
}

function getValueExpression(value) {
if (value.type === 'Literal') {
return value
}

return value.expression
}

function replace(source, j, componentName) {
return j(source)
.find(j.JSXOpeningElement, { name: { name: componentName } })
.forEach(p => {
const { name, attributes, selfClosing } = p.value

const deprecatedAttributes = attributes.filter(isDeprecatedAttribute)
if (!deprecatedAttributes.length) {
return
}

const newAttributes = attributes
.filter(attribute => !isDeprecatedAttribute(attribute))
.map(attribute => {
if (attribute.name.name !== 'to') {
return attribute
}

const properties = [
j.property(
'init', j.identifier('pathname'),
getValueExpression(attribute.value)
)
]

properties.push(...deprecatedAttributes.map(({ name, value }) => (
j.property(
'init', j.identifier(name.name),
getValueExpression(value)
)
)))

return j.jsxAttribute(
j.jsxIdentifier('to'),
j.jsxExpressionContainer(j.objectExpression(properties))
)
})

j(p).replaceWith(j.jsxOpeningElement(name, newAttributes, selfClosing))
})
.toSource()
}

export default (file, { jscodeshift: j }) => {
let { source } = file
source = replace(source, j, 'Link')
source = replace(source, j, 'IndexLink')

return source
}

0 comments on commit 329f768

Please sign in to comment.