|
| 1 | +import * as fluentSyntax from "@fluent/syntax"; |
| 2 | +import type { Plugin } from "prettier"; |
| 3 | + |
| 4 | +const plugin: Plugin<fluentSyntax.Resource> = { |
| 5 | + languages: [ |
| 6 | + { |
| 7 | + name: "Fluent", |
| 8 | + parsers: ["fluent"], |
| 9 | + extensions: [".ftl"], |
| 10 | + linguistLanguageId: 206353404, // see https://github.com/github-linguist/linguist/blob/main/lib/linguist/languages.yml |
| 11 | + }, |
| 12 | + ], |
| 13 | + parsers: { |
| 14 | + fluent: { |
| 15 | + parse(text) { |
| 16 | + const resource = fluentSyntax.parse(text, { withSpans: true }); |
| 17 | + const firstJunkEntry = resource.body.find( |
| 18 | + entry => entry instanceof fluentSyntax.Junk |
| 19 | + ); |
| 20 | + if (firstJunkEntry) { |
| 21 | + throw createParseError(text, firstJunkEntry); |
| 22 | + } |
| 23 | + return resource; |
| 24 | + }, |
| 25 | + astFormat: "fluent-ast", |
| 26 | + hasIgnorePragma(text) { |
| 27 | + return Boolean( |
| 28 | + text.trimStart().match(/^#{1,3}\s*(?:@noformat|@noprettier)\b/) |
| 29 | + ); |
| 30 | + }, |
| 31 | + locStart(node) { |
| 32 | + return node.span?.start ?? 0; |
| 33 | + }, |
| 34 | + locEnd(node) { |
| 35 | + return node.span?.end ?? 0; |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + printers: { |
| 40 | + "fluent-ast": { |
| 41 | + print(path) { |
| 42 | + return fluentSyntax.serialize(sortResource(path.node), {}); |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | +}; |
| 47 | +export default plugin; |
| 48 | + |
| 49 | +function sortResource(resource: fluentSyntax.Resource): fluentSyntax.Resource { |
| 50 | + type SortableEntry = fluentSyntax.Message | fluentSyntax.Term; |
| 51 | + function compare(a: SortableEntry, b: SortableEntry): number { |
| 52 | + return a.id.name.localeCompare(b.id.name); |
| 53 | + } |
| 54 | + const entries: fluentSyntax.Entry[] = []; |
| 55 | + const pending: SortableEntry[] = []; |
| 56 | + for (const entry of resource.body) { |
| 57 | + if ( |
| 58 | + entry instanceof fluentSyntax.Message || |
| 59 | + entry instanceof fluentSyntax.Term |
| 60 | + ) { |
| 61 | + pending.push(entry); |
| 62 | + continue; |
| 63 | + } |
| 64 | + if (pending.length) { |
| 65 | + entries.push(...pending.sort(compare)); |
| 66 | + pending.length = 0; |
| 67 | + } |
| 68 | + entries.push(entry); |
| 69 | + } |
| 70 | + entries.push(...pending.sort(compare)); |
| 71 | + return new fluentSyntax.Resource(entries); |
| 72 | +} |
| 73 | + |
| 74 | +type FluentParseError = Error & { |
| 75 | + // Optional, but Prettier gives nicer error messages when set. |
| 76 | + loc?: { start: { line: number; column: number } }; |
| 77 | +}; |
| 78 | + |
| 79 | +function createParseError( |
| 80 | + text: string, |
| 81 | + junk: fluentSyntax.Junk |
| 82 | +): FluentParseError { |
| 83 | + const annotation = junk.annotations[0]; |
| 84 | + const offset = annotation?.span?.start ?? junk.span?.start ?? 0; |
| 85 | + const line = fluentSyntax.lineOffset(text, offset) + 1; |
| 86 | + const column = fluentSyntax.columnOffset(text, offset) + 1; |
| 87 | + const details = annotation |
| 88 | + ? `${annotation.code}: ${annotation.message}` |
| 89 | + : "Invalid Fluent syntax"; |
| 90 | + const error: FluentParseError = new Error(`${details} (${line}:${column})`); |
| 91 | + error.loc = { start: { line, column } }; |
| 92 | + return error; |
| 93 | +} |
0 commit comments