|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright (c) 2020 The Polymer Project Authors. All rights reserved. |
| 4 | + * This code may only be used under the BSD style license found at |
| 5 | + * http://polymer.github.io/LICENSE.txt The complete set of authors may be found |
| 6 | + * at http://polymer.github.io/AUTHORS.txt The complete set of contributors may |
| 7 | + * be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by |
| 8 | + * Google as part of the polymer project is also subject to an additional IP |
| 9 | + * rights grant found at http://polymer.github.io/PATENTS.txt |
| 10 | + */ |
| 11 | + |
| 12 | +import {Message} from '../messages'; |
| 13 | +import {Locale} from '../locales'; |
| 14 | +import {Config} from '../config'; |
| 15 | +import * as ts from 'typescript'; |
| 16 | +import * as pathLib from 'path'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Configuration specific to the `transform` output mode. |
| 20 | + */ |
| 21 | +export interface TransformOutputConfig { |
| 22 | + mode: 'transform'; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Compile and emit the given TypeScript program using the lit-localize |
| 27 | + * transformer. |
| 28 | + */ |
| 29 | +export function transformOutput( |
| 30 | + translationsByLocale: Map<Locale, Message[]>, |
| 31 | + config: Config, |
| 32 | + program: ts.Program |
| 33 | +) { |
| 34 | + // TODO(aomarks) It doesn't seem that it's possible for a TypeScript |
| 35 | + // transformer to emit a new file, so we just have to emit for each locale. |
| 36 | + // Need to do some more investigation into the best way to integrate this |
| 37 | + // transformation into a real project so that the user can still use --watch |
| 38 | + // and other tsc flags. It would also be nice to support the language server, |
| 39 | + // so that diagnostics will show up immediately in the editor. |
| 40 | + const opts = program.getCompilerOptions(); |
| 41 | + const outRoot = opts.outDir || '.'; |
| 42 | + for (const locale of [config.sourceLocale, ...config.targetLocales]) { |
| 43 | + let translations; |
| 44 | + if (locale !== config.sourceLocale) { |
| 45 | + translations = new Map<string, Message>(); |
| 46 | + for (const message of translationsByLocale.get(locale) || []) { |
| 47 | + translations.set(message.name, message); |
| 48 | + } |
| 49 | + } |
| 50 | + opts.outDir = pathLib.join(outRoot, '/', locale); |
| 51 | + program.emit(undefined, undefined, undefined, undefined, { |
| 52 | + before: [litLocalizeTransform(translations)], |
| 53 | + }); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Return a TypeScript TransformerFactory for the lit-localize transformer. |
| 59 | + */ |
| 60 | +export function litLocalizeTransform( |
| 61 | + translations: Map<string, Message> | undefined |
| 62 | +): ts.TransformerFactory<ts.SourceFile> { |
| 63 | + return (context) => { |
| 64 | + const transformer = new Transformer(context, translations); |
| 65 | + return (file) => ts.visitNode(file, transformer.boundVisitNode); |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Implementation of the lit-localize TypeScript transformer. |
| 71 | + */ |
| 72 | +class Transformer { |
| 73 | + private context: ts.TransformationContext; |
| 74 | + boundVisitNode = this.visitNode.bind(this); |
| 75 | + |
| 76 | + constructor( |
| 77 | + context: ts.TransformationContext, |
| 78 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 79 | + _translations: Map<string, Message> | undefined |
| 80 | + ) { |
| 81 | + this.context = context; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Top-level delegating visitor for all nodes. |
| 86 | + */ |
| 87 | + visitNode(node: ts.Node): ts.VisitResult<ts.Node> { |
| 88 | + // TODO(aomarks) The transformer! |
| 89 | + return ts.visitEachChild(node, this.boundVisitNode, this.context); |
| 90 | + } |
| 91 | +} |
0 commit comments