Skip to content

Commit

Permalink
feat: 🎸 support ts params extraction
Browse files Browse the repository at this point in the history
βœ… Closes: #153
  • Loading branch information
shaharkazaz committed Aug 2, 2024
1 parent 022e815 commit 0ccbe95
Show file tree
Hide file tree
Showing 32 changed files with 471 additions and 87 deletions.
23 changes: 3 additions & 20 deletions __tests__/buildTranslationFiles/build-translation-utils.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
import nodePath from 'node:path';
import {
BuildConfigOptions,
defaultValue,
buildConfig as _buildConfig,
removeI18nFolder as _removeI18nFolder,
assertPartialTranslation as _assertPartialTranslation,
assertTranslation as _assertTranslation,
AssertTranslationParams,
buildConfig as _buildConfig,
BuildConfigOptions,
removeI18nFolder as _removeI18nFolder,
} from '../spec-utils';

export const sourceRoot = '__tests__/buildTranslationFiles';

export function generateKeys({
start = 1,
end,
prefix,
}: {
start?: number;
end: number;
prefix?: string;
}): { [index: string]: string } {
const keys = {};
for (let i = start; i <= end; i++) {
keys[prefix ? `${prefix}.${i}` : i] = defaultValue;
}
return keys;
}

export type TranslationTestCase =
| 'template-extraction/pipe'
| 'template-extraction/directive'
Expand Down
7 changes: 5 additions & 2 deletions __tests__/buildTranslationFiles/comments/comments-spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {
assertTranslation,
buildConfig,
generateKeys,
removeI18nFolder,
sourceRoot,
TranslationTestCase,
} from '../build-translation-utils';
import { defaultValue, mockResolveProjectBasePath } from '../../spec-utils';
import {
defaultValue,
generateKeys,
mockResolveProjectBasePath,
} from '../../spec-utils';
import { Config } from '../../../src/types';

mockResolveProjectBasePath(sourceRoot);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {
assertTranslation,
buildConfig,
generateKeys,
removeI18nFolder,
sourceRoot,
TranslationTestCase,
} from '../../build-translation-utils';
import { defaultValue, mockResolveProjectBasePath } from '../../../spec-utils';
import {
defaultValue,
generateKeys,
mockResolveProjectBasePath,
} from '../../../spec-utils';
import { Config } from '../../../../src/types';
import nodePath from 'node:path';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {
assertTranslation,
buildConfig,
generateKeys,
removeI18nFolder,
sourceRoot,
TranslationTestCase,
} from '../../build-translation-utils';
import { mockResolveProjectBasePath } from '../../../spec-utils';
import { generateKeys, mockResolveProjectBasePath } from '../../../spec-utils';
import { Config } from '../../../../src/types';

mockResolveProjectBasePath(sourceRoot);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {
assertTranslation,
buildConfig,
generateKeys,
removeI18nFolder,
sourceRoot,
TranslationTestCase,
} from '../../build-translation-utils';
import { mockResolveProjectBasePath } from '../../../spec-utils';
import { generateKeys, mockResolveProjectBasePath } from '../../../spec-utils';
import { Config } from '../../../../src/types';

mockResolveProjectBasePath(sourceRoot);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {
assertTranslation,
buildConfig,
generateKeys,
removeI18nFolder,
sourceRoot,
TranslationTestCase,
} from '../../build-translation-utils';
import { defaultValue, mockResolveProjectBasePath } from '../../../spec-utils';
import {
defaultValue,
generateKeys,
mockResolveProjectBasePath,
} from '../../../spec-utils';
import { Config } from '../../../../src/types';

mockResolveProjectBasePath(sourceRoot);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {
assertTranslation,
buildConfig,
generateKeys,
removeI18nFolder,
sourceRoot,
TranslationTestCase,
} from '../../build-translation-utils';
import { defaultValue, mockResolveProjectBasePath } from '../../../spec-utils';
import {
defaultValue,
generateKeys,
mockResolveProjectBasePath,
} from '../../../spec-utils';
import { Config } from '../../../../src/types';

mockResolveProjectBasePath(sourceRoot);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {
assertTranslation,
buildConfig,
generateKeys,
removeI18nFolder,
sourceRoot,
TranslationTestCase,
} from '../../build-translation-utils';
import { defaultValue, mockResolveProjectBasePath } from '../../../spec-utils';
import {
defaultValue,
generateKeys,
mockResolveProjectBasePath,
} from '../../../spec-utils';
import { Config } from '../../../../src/types';

mockResolveProjectBasePath(sourceRoot);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {
assertTranslation,
buildConfig,
generateKeys,
removeI18nFolder,
sourceRoot,
TranslationTestCase,
} from '../../build-translation-utils';
import { defaultValue, mockResolveProjectBasePath } from '../../../spec-utils';
import {
defaultValue,
generateKeys,
mockResolveProjectBasePath,
} from '../../../spec-utils';
import { Config } from '../../../../src/types';

mockResolveProjectBasePath(sourceRoot);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {
assertTranslation,
buildConfig,
generateKeys,
removeI18nFolder,
sourceRoot,
TranslationTestCase,
} from '../../build-translation-utils';
import { mockResolveProjectBasePath } from '../../../spec-utils';
import { generateKeys, mockResolveProjectBasePath } from '../../../spec-utils';
import { Config } from '../../../../src/types';

mockResolveProjectBasePath(sourceRoot);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {
assertTranslation,
buildConfig,
generateKeys,
removeI18nFolder,
sourceRoot,
TranslationTestCase,
} from '../../build-translation-utils';
import { mockResolveProjectBasePath } from '../../../spec-utils';
import { generateKeys, mockResolveProjectBasePath } from '../../../spec-utils';
import { Config } from '../../../../src/types';

mockResolveProjectBasePath(sourceRoot);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {
assertTranslation,
buildConfig,
generateKeys,
removeI18nFolder,
sourceRoot,
TranslationTestCase,
} from '../../build-translation-utils';
import { defaultValue, mockResolveProjectBasePath } from '../../../spec-utils';
import {
defaultValue,
generateKeys,
mockResolveProjectBasePath,
} from '../../../spec-utils';
import { Config } from '../../../../src/types';

mockResolveProjectBasePath(sourceRoot);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import {
assertTranslation,
buildConfig,
generateKeys,
removeI18nFolder,
sourceRoot,
TranslationTestCase,
} from '../../build-translation-utils';
import { mockResolveProjectBasePath } from '../../../spec-utils';
import {
buildKeysFromParams,
mockResolveProjectBasePath,
setParamsInput,
generateKeys,
resolveValueWithParams,
} from '../../../spec-utils';
import { Config } from '../../../../src/types';

mockResolveProjectBasePath(sourceRoot);
Expand All @@ -26,10 +31,20 @@ export function testPureFunctionExtraction(fileFormat: Config['fileFormat']) {
beforeEach(() => removeI18nFolder(type));

it('should work with the pure `translate` function', () => {
const expected = generateKeys({ end: 3 });
const expected = generateKeys({ end: 4 });

buildTranslationFiles(config);
assertTranslation({ type, expected, fileFormat });
});

it('should extract params', () => {
const expected = {
...generateKeys({ end: 3, withParams: true }),
4: resolveValueWithParams(['foo', 'a', 'b.c']),
};

buildTranslationFiles(setParamsInput(config));
assertTranslation({ type, expected, fileFormat });
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export class AppComponent {
}

getString(): string {
return '5';
return '9';
}

extractionProblem(): void {
translate('2');
const foo = <string>this.getString();
translate('3');
translate(['3', '4']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Component } from '@angular/core';
import { translate } from '@jsverse/transloco';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrl: './app.component.scss',
})
export class AppComponent {
constructor() {
translate('1', { 1: '' });
translate(['2'], { 2: '' });
}

getString(): string {
return '9';
}

extractionProblem(): void {
const foo = <string>this.getString();
translate('4', { foo: '', a: '', b: { c: '' } });
}
}

translate('3', { 3: '' });
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import {
assertPartialTranslation,
assertTranslation,
buildConfig,
generateKeys,
removeI18nFolder,
sourceRoot,
TranslationTestCase,
} from '../../build-translation-utils';
import { defaultValue, mockResolveProjectBasePath } from '../../../spec-utils';
import {
defaultValue,
generateKeys,
mockResolveProjectBasePath,
buildKeysFromParams,
setParamsInput,
} from '../../../spec-utils';
import { Config } from '../../../../src/types';

mockResolveProjectBasePath(sourceRoot);
Expand Down Expand Up @@ -84,5 +89,20 @@ export function testServiceExtraction(fileFormat: Config['fileFormat']) {
buildTranslationFiles(config);
assertPartialTranslation({ type, expected, fileFormat });
});

it('should extract params', () => {
const expected = {
...generateKeys({ end: 11, withParams: true }),
...buildKeysFromParams([
'inject.test',
'private-class-field.test',
'variable',
'another.variable',
]),
};

buildTranslationFiles(setParamsInput(config));
assertTranslation({ type, expected, fileFormat });
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class NavPartitionsComponent implements OnInit, OnDestroy {
.pipe(untilDestroyed(this))
.subscribe((res: string) => {
this.lightConnectStore.setDataSegmentation(res);
this.transloco.translate('7', { hey: 'dsds' }, 'es');
this.transloco.translate('7', null, 'es');
});

const dataSegmentation =
Expand All @@ -155,14 +155,14 @@ export class NavPartitionsComponent implements OnInit, OnDestroy {
}

ngOnDestroy(): void {
const f = this.transloco.translate('8', { id: 1 });
const f = this.transloco.translate('8');
if (this.dispose) {
this.dispose.forEach((i) => i());
}
}

get iconDisabledColor() {
return this.transloco.translate('9', { id: 1 });
return this.transloco.translate('9');
}

get textDisabledColor() {
Expand All @@ -174,7 +174,7 @@ export class NavPartitionsComponent implements OnInit, OnDestroy {
get shouldDisableAccordion() {
const shouldUsePartitions =
this.form && this.form.get('shouldUsePartitions');
this.cc = this.transloco.translate('10', { id: 1, d: 'dsds' });
this.cc = this.transloco.translate('10');

return !shouldUsePartitions || !shouldUsePartitions.value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ export class Something implements OnInit, AfterViewInit, OnDestroy {
this._dispose = [
this._onStateChange(),
this.transloco.translate('1', {}, 'todos-page'),
transloco.selectTranslate(
'2.1',
{ myVAr: { nested: 'bla' } },
'todos-page',
),
transloco.selectTranslate('2.1', {}, 'todos-page'),
reaction(
() => this.leftNavStore.itemClicked,
(data) => {
Expand Down Expand Up @@ -113,7 +109,7 @@ export class Something implements OnInit, AfterViewInit, OnDestroy {
}

onScroll(event) {
this.transloco.selectTranslate(`6.1`, { some: 'asd' }, `nested/scope/es`);
this.transloco.selectTranslate(`6.1`, {}, `nested/scope/es`);
}

onSearch() {
Expand Down
Loading

0 comments on commit 0ccbe95

Please sign in to comment.