Visual Studio Code extension to help introducing formatjs into an existing codebase. Select a string or template string then format it automatically to follow formatjs spec.
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
This is a Visual Studio Code extension to help introducing Format.JS into an existing codebase. Select a string or template string then format it automatically to follow formatjs spec.
In js and ts files
Usage
Select a string in your code file, including quotes!
Open the command palette (cmd + shif + P)
Run the command "Refactor the selected message to formatjs"
// before
`Hello ${username}`
// after
intl.formatMessage({ defaultMessage: 'Hello {username}' }, { username })
Features
This extension supports:
Literal strings (between single or double quotes)
Template strings with or without any code expressions
Multiline template strings
Limitations
The built options don't necessarily have a perfect name. Complex code in a template string turns to "optionN".
If you use the same code twice in a template string, the same option will be generated twice.
The current version doesn't handle backquotes well.
In Handlebars templates
Additionnally to the strict Format.js API, this extension also comes with a solution for Handlebars templates.
It supposes that you would implement a formatMessage helper that internally relies on Format.js, so you can
also extract and format bare strings in templates.
Usage
Select the exact piece of string you want to be formatted
Open the command palette (cmd + shif + P)
Run the command "Refactor the selected HTML with a formatjs-like Handlebars helper"
// before
Hello {{username}}
// after
{{formatMessage "Hello {username}" username=username}}
Features
This extension supports:
Presence of double quotes in the formatted string
Insertion of triple curlies to allow HTML in the formatted string
Multiline strings
Limitations
The built options don't necessarily have a perfect name. Complex code in a template string turns to "optionN".
If you use the same code twice in a handlebars variable, the same option will be generated twice.
Calls to Handlebars helpers in the formatted piece are not supported.
Extension Settings
N/A
Release Notes
0.2.0 - Add a command to refactor Handlebars templates.
0.1.0 - Supports template strings without code expressions and multiline template strings.