refactor(localize): update to use new file-system interfaces (#40281)

Now that `ReadonlyFileSystem` and `PathManipulation` interfaces are
available, this commit updates the localize package to use these more
focussed interfaces.

PR Close #40281
This commit is contained in:
Pete Bacon Darwin 2020-12-30 17:04:04 +00:00 committed by Andrew Scott
parent d100a15998
commit bb6d791dab
19 changed files with 43 additions and 51 deletions

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AbsoluteFsPath, FileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {AbsoluteFsPath, PathManipulation} from '@angular/compiler-cli/src/ngtsc/file_system';
import {ɵMessageId, ɵParsedMessage} from '@angular/localize';
import {DiagnosticHandlingStrategy, Diagnostics} from '../diagnostics';
@ -17,7 +17,7 @@ import {serializeLocationPosition} from '../source_file_utils';
* object (as necessary).
*/
export function checkDuplicateMessages(
fs: FileSystem, messages: ɵParsedMessage[],
fs: PathManipulation, messages: ɵParsedMessage[],
duplicateMessageHandling: DiagnosticHandlingStrategy, basePath: AbsoluteFsPath): Diagnostics {
const diagnostics = new Diagnostics();
if (duplicateMessageHandling === 'ignore') return diagnostics;
@ -47,7 +47,7 @@ export function checkDuplicateMessages(
* Serialize the given `message` object into a string.
*/
function serializeMessage(
fs: FileSystem, basePath: AbsoluteFsPath, message: ɵParsedMessage): string {
fs: PathManipulation, basePath: AbsoluteFsPath, message: ɵParsedMessage): string {
if (message.location === undefined) {
return ` - "${message.text}"`;
} else {

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AbsoluteFsPath, FileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {AbsoluteFsPath, ReadonlyFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {Logger} from '@angular/compiler-cli/src/ngtsc/logging';
import {SourceFile, SourceFileLoader} from '@angular/compiler-cli/src/ngtsc/sourcemaps';
import {ɵParsedMessage, ɵSourceLocation} from '@angular/localize';
@ -33,7 +33,7 @@ export class MessageExtractor {
private loader: SourceFileLoader;
constructor(
private fs: FileSystem, private logger: Logger,
private fs: ReadonlyFileSystem, private logger: Logger,
{basePath, useSourceMaps = true, localizeName = '$localize'}: ExtractionOptions) {
this.basePath = basePath;
this.useSourceMaps = useSourceMaps;

View File

@ -6,7 +6,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {setFileSystem, NodeJSFileSystem, AbsoluteFsPath, FileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {setFileSystem, NodeJSFileSystem, AbsoluteFsPath, FileSystem, PathManipulation} from '@angular/compiler-cli/src/ngtsc/file_system';
import {ConsoleLogger, Logger, LogLevel} from '@angular/compiler-cli/src/ngtsc/logging';
import {ɵParsedMessage} from '@angular/localize';
import * as glob from 'glob';
@ -214,7 +214,7 @@ export function extractTranslations({
export function getSerializer(
format: string, sourceLocale: string, rootPath: AbsoluteFsPath, useLegacyIds: boolean,
formatOptions: FormatOptions = {}, fs: FileSystem): TranslationSerializer {
formatOptions: FormatOptions = {}, fs: PathManipulation): TranslationSerializer {
switch (format) {
case 'xlf':
case 'xlif':

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {FileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {PathManipulation} from '@angular/compiler-cli/src/ngtsc/file_system';
import {ɵParsedMessage, ɵparseMessage} from '@angular/localize';
import {NodePath, PluginObj} from '@babel/core';
import {TaggedTemplateExpression} from '@babel/types';
@ -13,7 +13,7 @@ import {TaggedTemplateExpression} from '@babel/types';
import {getLocation, isGlobalIdentifier, isNamedIdentifier, unwrapExpressionsFromTemplateLiteral, unwrapMessagePartsFromTemplateLiteral} from '../../source_file_utils';
export function makeEs2015ExtractPlugin(
fs: FileSystem, messages: ɵParsedMessage[], localizeName = '$localize'): PluginObj {
fs: PathManipulation, messages: ɵParsedMessage[], localizeName = '$localize'): PluginObj {
return {
visitor: {
TaggedTemplateExpression(path: NodePath<TaggedTemplateExpression>) {

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {FileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {PathManipulation} from '@angular/compiler-cli/src/ngtsc/file_system';
import {ɵParsedMessage, ɵparseMessage} from '@angular/localize';
import {NodePath, PluginObj} from '@babel/core';
import {CallExpression} from '@babel/types';
@ -13,7 +13,7 @@ import {CallExpression} from '@babel/types';
import {getLocation, isGlobalIdentifier, isNamedIdentifier, unwrapMessagePartsFromLocalizeCall, unwrapSubstitutionsFromLocalizeCall} from '../../source_file_utils';
export function makeEs5ExtractPlugin(
fs: FileSystem, messages: ɵParsedMessage[], localizeName = '$localize'): PluginObj {
fs: PathManipulation, messages: ɵParsedMessage[], localizeName = '$localize'): PluginObj {
return {
visitor: {
CallExpression(callPath: NodePath<CallExpression>) {

View File

@ -5,9 +5,8 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AbsoluteFsPath, FileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {AbsoluteFsPath, PathManipulation} from '@angular/compiler-cli/src/ngtsc/file_system';
import {ɵParsedMessage, ɵSourceLocation} from '@angular/localize';
import {ArbJsonObject, ArbLocation, ArbMetadata} from '../../translate/translation_files/translation_parsers/arb_translation_parser';
import {TranslationSerializer} from './translation_serializer';
import {consolidateMessages, hasLocation} from './utils';
@ -45,7 +44,8 @@ import {consolidateMessages, hasLocation} from './utils';
*/
export class ArbTranslationSerializer implements TranslationSerializer {
constructor(
private sourceLocale: string, private basePath: AbsoluteFsPath, private fs: FileSystem) {}
private sourceLocale: string, private basePath: AbsoluteFsPath,
private fs: PathManipulation) {}
serialize(messages: ɵParsedMessage[]): string {
const messageGroups = consolidateMessages(messages, message => getMessageId(message));

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AbsoluteFsPath, FileSystem, getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {AbsoluteFsPath, getFileSystem, PathManipulation} from '@angular/compiler-cli/src/ngtsc/file_system';
import {ɵParsedMessage, ɵSourceLocation} from '@angular/localize';
import {FormatOptions, validateOptions} from './format_options';
@ -29,7 +29,7 @@ const LEGACY_XLIFF_MESSAGE_LENGTH = 40;
export class Xliff1TranslationSerializer implements TranslationSerializer {
constructor(
private sourceLocale: string, private basePath: AbsoluteFsPath, private useLegacyIds: boolean,
private formatOptions: FormatOptions = {}, private fs: FileSystem = getFileSystem()) {
private formatOptions: FormatOptions = {}, private fs: PathManipulation = getFileSystem()) {
validateOptions('Xliff1TranslationSerializer', [['xml:space', ['preserve']]], formatOptions);
}

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AbsoluteFsPath, FileSystem, getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {AbsoluteFsPath, getFileSystem, PathManipulation} from '@angular/compiler-cli/src/ngtsc/file_system';
import {ɵParsedMessage, ɵSourceLocation} from '@angular/localize';
import {FormatOptions, validateOptions} from './format_options';
@ -29,7 +29,7 @@ export class Xliff2TranslationSerializer implements TranslationSerializer {
private currentPlaceholderId = 0;
constructor(
private sourceLocale: string, private basePath: AbsoluteFsPath, private useLegacyIds: boolean,
private formatOptions: FormatOptions = {}, private fs: FileSystem = getFileSystem()) {
private formatOptions: FormatOptions = {}, private fs: PathManipulation = getFileSystem()) {
validateOptions('Xliff1TranslationSerializer', [['xml:space', ['preserve']]], formatOptions);
}

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AbsoluteFsPath, FileSystem, getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {AbsoluteFsPath, getFileSystem, PathManipulation} from '@angular/compiler-cli/src/ngtsc/file_system';
import {ɵParsedMessage, ɵSourceLocation} from '@angular/localize';
import {extractIcuPlaceholders} from './icu_parsing';
@ -24,7 +24,7 @@ import {XmlFile} from './xml_file';
export class XmbTranslationSerializer implements TranslationSerializer {
constructor(
private basePath: AbsoluteFsPath, private useLegacyIds: boolean,
private fs: FileSystem = getFileSystem()) {}
private fs: PathManipulation = getFileSystem()) {}
serialize(messages: ɵParsedMessage[]): string {
const messageGroups = consolidateMessages(messages, message => this.getMessageId(message));

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AbsoluteFsPath, FileSystem, getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {AbsoluteFsPath, getFileSystem, PathManipulation} from '@angular/compiler-cli/src/ngtsc/file_system';
import {ɵisMissingTranslationError, ɵmakeTemplateObject, ɵParsedTranslation, ɵSourceLocation, ɵtranslate} from '@angular/localize';
import {NodePath} from '@babel/traverse';
import * as t from '@babel/types';
@ -74,7 +74,7 @@ export function buildLocalizeReplacement(
*/
export function unwrapMessagePartsFromLocalizeCall(
call: NodePath<t.CallExpression>,
fs: FileSystem = getFileSystem(),
fs: PathManipulation = getFileSystem(),
): [TemplateStringsArray, (ɵSourceLocation | undefined)[]] {
let cooked = call.get('arguments')[0];
@ -160,7 +160,7 @@ export function unwrapMessagePartsFromLocalizeCall(
*/
export function unwrapSubstitutionsFromLocalizeCall(
call: NodePath<t.CallExpression>,
fs: FileSystem = getFileSystem()): [t.Expression[], (ɵSourceLocation | undefined)[]] {
fs: PathManipulation = getFileSystem()): [t.Expression[], (ɵSourceLocation | undefined)[]] {
const expressions = call.get('arguments').splice(1);
if (!isArrayOfExpressions(expressions)) {
const badExpression = expressions.find(expression => !expression.isExpression())!;
@ -182,8 +182,8 @@ export function unwrapSubstitutionsFromLocalizeCall(
* @publicApi used by CLI
*/
export function unwrapMessagePartsFromTemplateLiteral(
elements: NodePath<t.TemplateElement>[],
fs: FileSystem = getFileSystem()): [TemplateStringsArray, (ɵSourceLocation | undefined)[]] {
elements: NodePath<t.TemplateElement>[], fs: PathManipulation = getFileSystem()):
[TemplateStringsArray, (ɵSourceLocation | undefined)[]] {
const cooked = elements.map(q => {
if (q.node.value.cooked === undefined) {
throw new BabelParseError(
@ -207,7 +207,7 @@ export function unwrapMessagePartsFromTemplateLiteral(
*/
export function unwrapExpressionsFromTemplateLiteral(
quasi: NodePath<t.TemplateLiteral>,
fs: FileSystem = getFileSystem()): [t.Expression[], (ɵSourceLocation | undefined)[]] {
fs: PathManipulation = getFileSystem()): [t.Expression[], (ɵSourceLocation | undefined)[]] {
return [quasi.node.expressions, quasi.get('expressions').map(e => getLocation(fs, e))];
}
@ -235,7 +235,7 @@ export function wrapInParensIfNecessary(expression: t.Expression): t.Expression
*/
export function unwrapStringLiteralArray(
array: NodePath<t.Expression>,
fs: FileSystem = getFileSystem()): [string[], (ɵSourceLocation | undefined)[]] {
fs: PathManipulation = getFileSystem()): [string[], (ɵSourceLocation | undefined)[]] {
if (!isStringLiteralArray(array.node)) {
throw new BabelParseError(
array.node, 'Unexpected messageParts for `$localize` (expected an array of strings).');
@ -400,7 +400,7 @@ export function buildCodeFrameError(path: NodePath, e: BabelParseError): string
}
export function getLocation(
fs: FileSystem, startPath: NodePath, endPath?: NodePath): ɵSourceLocation|undefined {
fs: PathManipulation, startPath: NodePath, endPath?: NodePath): ɵSourceLocation|undefined {
const startLocation = startPath.node.loc;
const file = getFileFromPath(fs, startPath);
if (!startLocation || !file) {
@ -425,7 +425,7 @@ export function serializeLocationPosition(location: ɵSourceLocation): string {
return `${location.start.line + 1}${endLineString}`;
}
function getFileFromPath(fs: FileSystem, path: NodePath|undefined): AbsoluteFsPath|null {
function getFileFromPath(fs: PathManipulation, path: NodePath|undefined): AbsoluteFsPath|null {
const opts = path?.hub.file.opts;
return opts?.filename ?
fs.resolve(opts.generatorOpts.sourceRoot ?? opts.cwd, fs.relative(opts.cwd, opts.filename)) :

View File

@ -5,13 +5,12 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {FileSystem, getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {getFileSystem, PathManipulation} from '@angular/compiler-cli/src/ngtsc/file_system';
import {ɵParsedTranslation} from '@angular/localize';
import {NodePath, PluginObj} from '@babel/core';
import {TaggedTemplateExpression} from '@babel/types';
import {Diagnostics} from '../../diagnostics';
import {buildCodeFrameError, buildLocalizeReplacement, isBabelParseError, isLocalize, translate, TranslatePluginOptions, unwrapMessagePartsFromTemplateLiteral} from '../../source_file_utils';
/**
@ -23,7 +22,7 @@ import {buildCodeFrameError, buildLocalizeReplacement, isBabelParseError, isLoca
export function makeEs2015TranslatePlugin(
diagnostics: Diagnostics, translations: Record<string, ɵParsedTranslation>,
{missingTranslation = 'error', localizeName = '$localize'}: TranslatePluginOptions = {},
fs: FileSystem = getFileSystem()): PluginObj {
fs: PathManipulation = getFileSystem()): PluginObj {
return {
visitor: {
TaggedTemplateExpression(path: NodePath<TaggedTemplateExpression>) {

View File

@ -5,13 +5,12 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {FileSystem, getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {getFileSystem, PathManipulation} from '@angular/compiler-cli/src/ngtsc/file_system';
import {ɵParsedTranslation} from '@angular/localize';
import {NodePath, PluginObj} from '@babel/core';
import {CallExpression} from '@babel/types';
import {Diagnostics} from '../../diagnostics';
import {buildCodeFrameError, buildLocalizeReplacement, isBabelParseError, isLocalize, translate, TranslatePluginOptions, unwrapMessagePartsFromLocalizeCall, unwrapSubstitutionsFromLocalizeCall} from '../../source_file_utils';
/**
@ -23,7 +22,7 @@ import {buildCodeFrameError, buildLocalizeReplacement, isBabelParseError, isLoca
export function makeEs5TranslatePlugin(
diagnostics: Diagnostics, translations: Record<string, ɵParsedTranslation>,
{missingTranslation = 'error', localizeName = '$localize'}: TranslatePluginOptions = {},
fs: FileSystem = getFileSystem()): PluginObj {
fs: PathManipulation = getFileSystem()): PluginObj {
return {
visitor: {
CallExpression(callPath: NodePath<CallExpression>) {

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AbsoluteFsPath, FileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {AbsoluteFsPath, ReadonlyFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {DiagnosticHandlingStrategy, Diagnostics} from '../../diagnostics';
import {TranslationBundle} from '../translator';
@ -16,7 +16,7 @@ import {ParseAnalysis, TranslationParser} from './translation_parsers/translatio
*/
export class TranslationLoader {
constructor(
private fs: FileSystem, private translationParsers: TranslationParser<any>[],
private fs: ReadonlyFileSystem, private translationParsers: TranslationParser<any>[],
private duplicateTranslation: DiagnosticHandlingStrategy,
/** @deprecated */ private diagnostics?: Diagnostics) {}

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AbsoluteFsPath, FileSystem, PathSegment} from '@angular/compiler-cli/src/ngtsc/file_system';
import {AbsoluteFsPath, PathSegment, ReadonlyFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {ɵMessageId, ɵParsedTranslation} from '@angular/localize';
import {Diagnostics} from '../diagnostics';
@ -64,7 +64,7 @@ export interface TranslationHandler {
*/
export class Translator {
constructor(
private fs: FileSystem, private resourceHandlers: TranslationHandler[],
private fs: ReadonlyFileSystem, private resourceHandlers: TranslationHandler[],
private diagnostics: Diagnostics) {}
translateFiles(

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {absoluteFrom, FileSystem, getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {absoluteFrom, getFileSystem, PathManipulation} from '@angular/compiler-cli/src/ngtsc/file_system';
import {runInEachFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
import {ɵParsedMessage} from '@angular/localize';
@ -14,7 +14,7 @@ import {ArbTranslationSerializer} from '../../../src/extract/translation_files/a
import {location, mockMessage} from './mock_message';
runInEachFileSystem(() => {
let fs: FileSystem;
let fs: PathManipulation;
describe('ArbTranslationSerializer', () => {
beforeEach(() => {
fs = getFileSystem();

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {absoluteFrom, FileSystem, getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {absoluteFrom, getFileSystem, PathManipulation} from '@angular/compiler-cli/src/ngtsc/file_system';
import {runInEachFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
import {ɵParsedMessage, ɵSourceLocation} from '@angular/localize';
@ -14,7 +14,7 @@ import {XmbTranslationSerializer} from '../../../src/extract/translation_files/x
import {location, mockMessage} from './mock_message';
runInEachFileSystem(() => {
let fs: FileSystem;
let fs: PathManipulation;
beforeEach(() => fs = getFileSystem());
describe('XmbTranslationSerializer', () => {
[false, true].forEach(useLegacyIds => {

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {absoluteFrom, FileSystem, getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {absoluteFrom, getFileSystem, PathManipulation} from '@angular/compiler-cli/src/ngtsc/file_system';
import {runInEachFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
import {ɵmakeTemplateObject} from '@angular/localize';
import {NodePath, TransformOptions, transformSync} from '@babel/core';
@ -16,7 +16,7 @@ import {Expression, Identifier, TaggedTemplateExpression, ExpressionStatement, C
import {isGlobalIdentifier, isNamedIdentifier, isStringLiteralArray, isArrayOfExpressions, unwrapStringLiteralArray, unwrapMessagePartsFromLocalizeCall, wrapInParensIfNecessary, buildLocalizeReplacement, unwrapSubstitutionsFromLocalizeCall, unwrapMessagePartsFromTemplateLiteral, getLocation} from '../src/source_file_utils';
runInEachFileSystem(() => {
let fs: FileSystem;
let fs: PathManipulation;
beforeEach(() => fs = getFileSystem());
describe('utils', () => {
describe('isNamedIdentifier()', () => {

View File

@ -5,7 +5,6 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {FileSystem, getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {runInEachFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
import {ɵcomputeMsgId, ɵparseTranslation} from '@angular/localize';
import {ɵParsedTranslation} from '@angular/localize/private';
@ -16,8 +15,6 @@ import {TranslatePluginOptions} from '../../../src/source_file_utils';
import {makeEs2015TranslatePlugin} from '../../../src/translate/source_files/es2015_translate_plugin';
runInEachFileSystem(() => {
let fs: FileSystem;
beforeEach(() => fs = getFileSystem());
describe('makeEs2015Plugin', () => {
describe('(no translations)', () => {
it('should transform `$localize` tags with binary expression', () => {

View File

@ -5,7 +5,6 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {FileSystem, getFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system';
import {runInEachFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
import {ɵcomputeMsgId, ɵparseTranslation} from '@angular/localize';
import {ɵParsedTranslation} from '@angular/localize/private';
@ -16,8 +15,6 @@ import {TranslatePluginOptions} from '../../../src/source_file_utils';
import {makeEs5TranslatePlugin} from '../../../src/translate/source_files/es5_translate_plugin';
runInEachFileSystem(() => {
let fs: FileSystem;
beforeEach(() => fs = getFileSystem());
describe('makeEs5Plugin', () => {
describe('(no translations)', () => {
it('should transform `$localize` calls with binary expression', () => {