Last active
January 24, 2025 19:31
-
-
Save JuanCaicedo/2b06885487a9a0ead5d76f404c80ba93 to your computer and use it in GitHub Desktop.
AI prompt for creating message files
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
# Prompt | |
Can you write a jscodeshift transform that turns a file like | |
``` | |
import { defineMessages } from 'react-intl'; | |
export default defineMessages({ | |
"title.juan": "Screening Results", | |
"subtitle.juan": "Your {{- date}} results indicate you may have {{level}} {{resultType}}.", | |
}); | |
``` | |
Into something like | |
``` | |
import { defineMessages } from 'react-intl'; | |
export default defineMessages({ | |
"title.test": { | |
"id": "title.test", | |
defaultMessage: "Screening Results", | |
"description": "" | |
} | |
"subtitle.juan": { | |
"id":"subtitle.juan", | |
defaultMessage: "Your { date} results indicate you may have {level} {resultType}.", | |
}, | |
description: "" | |
}); | |
``` | |
Can you be sure to include something that transform parts like {{-date}} to {date} and {{level}} to {level} inside of the strings? | |
# Response | |
Sure! Below is a jscodeshift transform that accomplishes the task you described. This transform will convert the messages to the desired format and replace the placeholders in the strings. | |
```javascript | |
module.exports = function(fileInfo, api, options) { | |
const j = api.jscodeshift; | |
const root = j(fileInfo.source); | |
// Function to transform the message string | |
function transformMessageString(message) { | |
return message.replace(/{{-?\s*(\w+)\s*}}/g, '{$1}'); | |
} | |
// Find the default export that uses defineMessages | |
root.find(j.ExportDefaultDeclaration) | |
.find(j.CallExpression, { callee: { name: 'defineMessages' } }) | |
.forEach(path => { | |
const properties = path.node.arguments[0].properties; | |
properties.forEach(property => { | |
const key = property.key.name || property.key.value; | |
const value = property.value.value; | |
// Transform the message string | |
const transformedMessage = transformMessageString(value); | |
// Create the new object structure | |
property.value = j.objectExpression([ | |
j.property('init', j.identifier('id'), j.literal(key)), | |
j.property('init', j.identifier('defaultMessage'), j.literal(transformedMessage)), | |
j.property('init', j.identifier('description'), j.literal('')) | |
]); | |
}); | |
}); | |
return root.toSource(); | |
}; | |
``` | |
This transform script does the following: | |
1. It finds the default export that uses `defineMessages`. | |
2. It iterates over the properties of the object passed to `defineMessages`. | |
3. It transforms the message strings by replacing `{{- date}}` with `{date}` and similar placeholders. | |
4. It creates a new object structure for each message with `id`, `defaultMessage`, and `description` properties. | |
To run this transform, you would use jscodeshift with the script provided. For example: | |
```sh | |
jscodeshift -t path/to/transform.js path/to/your/file.js | |
``` | |
This will apply the transformation to your file and produce the desired output. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment