-
Notifications
You must be signed in to change notification settings - Fork 80
Add @fluent/prettier-plugin package to autoformat Fluent resources #668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wbolster
wants to merge
1
commit into
projectfluent:main
Choose a base branch
from
wbolster:prettier-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+498
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,3 @@ | ||
| esm/* | ||
| !esm/package.json | ||
| /index.js |
This file contains hidden or 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,6 @@ | ||
| .nyc_output | ||
| coverage | ||
| esm/.compiled | ||
| src | ||
| test | ||
| tsconfig.json |
This file contains hidden or 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,5 @@ | ||
| # Changelog | ||
|
|
||
| ## @fluent/prettier-plugin 0.1.0 | ||
|
|
||
| - Initial release |
This file contains hidden or 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,88 @@ | ||
| # @fluent/prettier-plugin  | ||
|
|
||
| `@fluent/prettier-plugin` is a [Prettier](https://prettier.io/) plugin | ||
| built on top of `@fluent/syntax` to format Project Fluent `.ftl` | ||
| files. It's part of [Project Fluent][]. | ||
|
|
||
| [project fluent]: https://projectfluent.org | ||
|
|
||
| The formatter normalizes valid Fluent syntax, such as indentation, | ||
| newlines, and spacing within placeables and functions. Invalid input | ||
| is rejected with an error. | ||
|
|
||
| Terms and messages sort alphabetically, giving deterministic output | ||
| that is easy to read and helps minimize merge conflicts. Example: | ||
|
|
||
| ```fluent | ||
| account = Account | ||
| -brand-name = Foo 3000 | ||
| welcome = Welcome, { $name }, to { -brand-name }! | ||
| ``` | ||
|
|
||
| Both stand-alone comments and comments bound to messages (see the | ||
| [syntax guide](https://projectfluent.org/fluent/guide/comments.html)) | ||
| are preserved, and stand-alone comments keep their original order. | ||
| Groups of messages and terms delineated by stand-alone comments are | ||
| sorted separately. | ||
|
|
||
| See the unit tests for more formatting examples. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```sh | ||
| npm install --save-dev prettier @fluent/prettier-plugin | ||
| ``` | ||
|
|
||
| ## How to use | ||
|
|
||
| ```sh | ||
| npx prettier --plugin=@fluent/prettier-plugin --write "**/*.ftl" | ||
| ``` | ||
|
|
||
| Add the plugin to the project Prettier config to automatically use it: | ||
|
|
||
| ```json | ||
| { | ||
| "plugins": ["@fluent/prettier-plugin"] | ||
| } | ||
| ``` | ||
|
|
||
| Individual files can opt out of formatting via a `@noformat` or | ||
| `@noprettier` ‘pragma’ comment at the top of the file. Example: | ||
|
|
||
| ```fluent | ||
| # @noformat | ||
| beta=Beta | ||
| alpha=Alpha | ||
| ``` | ||
|
|
||
| ## Vue support | ||
|
|
||
| When using [fluent-vue](https://github.com/fluent-vue/fluent-vue) and | ||
| per-component messages in Vue single-file components (SFC), add a | ||
| `lang="fluent"` attribute on `<fluent>` custom blocks to tell Prettier which | ||
| formatter to use: | ||
|
|
||
| ```vue | ||
| <fluent locale="en" lang="fluent"> | ||
| account = Account | ||
| greeting = Hello, { $name } | ||
| </fluent> | ||
| ``` | ||
|
|
||
| The [eslint-plugin-vue](https://eslint.vuejs.org/) | ||
| [`vue/block-lang`](https://eslint.vuejs.org/rules/block-lang) rule can | ||
| be used to enforce this: | ||
|
|
||
| ```json | ||
| { | ||
| "vue/block-lang": [ | ||
| "error", | ||
| { | ||
| "fluent": { | ||
| "lang": "fluent" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ``` |
This file contains hidden or 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,3 @@ | ||
| { | ||
| "type": "module" | ||
| } |
This file contains hidden or 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,49 @@ | ||
| { | ||
| "name": "@fluent/prettier-plugin", | ||
| "description": "Prettier plugin for Fluent files", | ||
| "version": "0.1.0", | ||
| "homepage": "https://projectfluent.org", | ||
| "author": "Mozilla <l10n-drivers@mozilla.org>", | ||
| "license": "Apache-2.0", | ||
| "contributors": [ | ||
| { | ||
| "name": "wouter bolsterlee", | ||
| "email": "wouter@bolsterl.ee" | ||
| } | ||
| ], | ||
| "main": "./index.js", | ||
| "module": "./esm/index.js", | ||
| "types": "./esm/index.d.ts", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/projectfluent/fluent.js.git" | ||
| }, | ||
| "keywords": [ | ||
| "fluent", | ||
| "format", | ||
| "ftl", | ||
| "i18n", | ||
| "internationalization", | ||
| "l10n", | ||
| "localization", | ||
| "prettier", | ||
| "prettier-plugin", | ||
| "translation" | ||
| ], | ||
| "scripts": { | ||
| "build": "tsc", | ||
| "postbuild": "rollup -c ../rollup.config.mjs --globals @fluent/syntax:FluentSyntax" | ||
| }, | ||
| "engines": { | ||
| "node": "^20.19 || ^22.12 || >=24" | ||
| }, | ||
| "dependencies": { | ||
| "@fluent/syntax": "^0.19.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@fluent/dedent": "^0.5.0" | ||
| }, | ||
| "peerDependencies": { | ||
| "prettier": "^3.0.0" | ||
| } | ||
| } |
This file contains hidden or 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,93 @@ | ||
| import * as fluentSyntax from "@fluent/syntax"; | ||
| import type { Plugin } from "prettier"; | ||
|
|
||
| const plugin: Plugin<fluentSyntax.Resource> = { | ||
| languages: [ | ||
| { | ||
| name: "Fluent", | ||
| parsers: ["fluent"], | ||
| extensions: [".ftl"], | ||
| linguistLanguageId: 206353404, // see https://github.com/github-linguist/linguist/blob/main/lib/linguist/languages.yml | ||
| }, | ||
| ], | ||
| parsers: { | ||
| fluent: { | ||
| parse(text) { | ||
| const resource = fluentSyntax.parse(text, { withSpans: true }); | ||
| const firstJunkEntry = resource.body.find( | ||
| entry => entry instanceof fluentSyntax.Junk | ||
| ); | ||
| if (firstJunkEntry) { | ||
| throw createParseError(text, firstJunkEntry); | ||
| } | ||
| return resource; | ||
| }, | ||
| astFormat: "fluent-ast", | ||
| hasIgnorePragma(text) { | ||
| return Boolean( | ||
| text.trimStart().match(/^#{1,3}\s*(?:@noformat|@noprettier)\b/) | ||
| ); | ||
| }, | ||
| locStart(node) { | ||
| return node.span?.start ?? 0; | ||
| }, | ||
| locEnd(node) { | ||
| return node.span?.end ?? 0; | ||
| }, | ||
| }, | ||
| }, | ||
| printers: { | ||
| "fluent-ast": { | ||
| print(path) { | ||
| return fluentSyntax.serialize(sortResource(path.node), {}); | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| export default plugin; | ||
|
|
||
| function sortResource(resource: fluentSyntax.Resource): fluentSyntax.Resource { | ||
| type SortableEntry = fluentSyntax.Message | fluentSyntax.Term; | ||
| function compare(a: SortableEntry, b: SortableEntry): number { | ||
| return a.id.name.localeCompare(b.id.name); | ||
| } | ||
| const entries: fluentSyntax.Entry[] = []; | ||
| const pending: SortableEntry[] = []; | ||
| for (const entry of resource.body) { | ||
| if ( | ||
| entry instanceof fluentSyntax.Message || | ||
| entry instanceof fluentSyntax.Term | ||
| ) { | ||
| pending.push(entry); | ||
| continue; | ||
| } | ||
| if (pending.length) { | ||
| entries.push(...pending.sort(compare)); | ||
| pending.length = 0; | ||
| } | ||
| entries.push(entry); | ||
| } | ||
| entries.push(...pending.sort(compare)); | ||
| return new fluentSyntax.Resource(entries); | ||
| } | ||
|
|
||
| type FluentParseError = Error & { | ||
| // Optional, but Prettier gives nicer error messages when set. | ||
| loc?: { start: { line: number; column: number } }; | ||
| }; | ||
|
|
||
| function createParseError( | ||
| text: string, | ||
| junk: fluentSyntax.Junk | ||
| ): FluentParseError { | ||
| const annotation = junk.annotations[0]; | ||
| const offset = annotation?.span?.start ?? junk.span?.start ?? 0; | ||
| const line = fluentSyntax.lineOffset(text, offset) + 1; | ||
| const column = fluentSyntax.columnOffset(text, offset) + 1; | ||
| const details = annotation | ||
| ? `${annotation.code}: ${annotation.message}` | ||
| : "Invalid Fluent syntax"; | ||
| const error: FluentParseError = new Error(`${details} (${line}:${column})`); | ||
| error.loc = { start: { line, column } }; | ||
| return error; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this call
toLocaleLowerCaseon theidname? I would prefer it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, i'm open to changing that. but now i wonder in general whether this would need a fixed locale, b/c as i understand it
localeCompare()will fallback to the ‘current’ (whatever that may be) locale, which is nondeterministic.perhaps this can use the locale
und(undefined language, e.g.new Intl.Locale('und')) instead of hardcoding (some variation of) english.wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh yeah, good call. i don't remember all of the subtleties with choosing locales. we could also "just" choose
"en", as it's (sadly) the default language in most cases, but maybe that will have other negatives i've not thought of yet.