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

Add transform for Link props #5

Merged
merged 1 commit into from
Dec 7, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
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
}