Skip to content
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

Exclude children of specified keys #161

Merged
merged 5 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ This will print out a table of missing keys in your language files, as well as u
// Use if you want to override the separator used when parsing locale identifiers. Default is `.`.

--exclude
// Array,Comma seperated list
// Use if you want to exclude some keys from the unused keys report. Default is '[]'.
// String
// Use if you want to exclude keys from the unused keys report. If the value is a node, all it's children will be excluded. Can be used multiple times to exclude multiple keys and nodes.
// Example1 (specific, value is a key): user.auth.username.label
// Example2 (nested, value is a node): user.auth
// Example3 (multiple): --exclude user.auth.username.label --exclude user.auth.password.label
```

## Config File
Expand Down
3 changes: 3 additions & 0 deletions src/config-file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export function initCommand(): void {

export function resolveConfig (): Record<string, string> {
const argvOptions = cac().parse(process.argv, { run: false }).options;
const excluded = argvOptions.exclude;

argvOptions.exclude = !Array.isArray(excluded) ? [excluded] : excluded

try {
const pathToConfigFile = path.resolve(process.cwd(), './vue-i18n-extract.config.js');
Expand Down
3 changes: 2 additions & 1 deletion src/create-report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export async function createI18NReport (options: ReportOptions): Promise<I18NRep

const report = extractI18NReport(I18NItems, I18NLanguage);

report.unusedKeys = report.unusedKeys.filter(key => !exclude.includes(key.path));
report.unusedKeys = report.unusedKeys.filter(key =>
!exclude.filter(excluded => key.path.startsWith(excluded)).length)

if (report.missingKeys.length) console.info('\nMissing Keys'), console.table(report.missingKeys);
if (report.unusedKeys.length) console.info('\nUnused Keys'), console.table(report.unusedKeys);
Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/expected-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ export const expectedFromParsedLanguageFiles = {
{
path: 'unused_json',
file: './tests/fixtures/lang/en_EN.json',
},
{
path: 'unused_nested.auth',
file: './tests/fixtures/lang/en_EN.json',
},
{
path: 'unused_nested.forgot',
file: './tests/fixtures/lang/en_EN.json',
}
]
};
Expand Down Expand Up @@ -525,6 +533,16 @@ export const expectedI18NReport = {
path: "unused_json",
file: "./tests/fixtures/lang/en_EN.json",
language: "en_EN"
},
{
path: "unused_nested.auth",
file: "./tests/fixtures/lang/en_EN.json",
language: "en_EN"
},
{
path: "unused_nested.forgot",
file: "./tests/fixtures/lang/en_EN.json",
language: "en_EN"
}
],
maybeDynamicKeys: [
Expand Down
6 changes: 5 additions & 1 deletion tests/fixtures/lang/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@
"missing": {
"german": "Missing!"
},
"unused_json": "This key is unused."
"unused_json": "This key is unused.",
"unused_nested": {
"auth": "Auth Pages",
"forgot": "Forgot Password"
}
}
12 changes: 11 additions & 1 deletion tests/unit/create-report/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,17 @@ describe('file: create-report/index', () => {
});

it('Should exclude keys if the exclude option was used', async () => {
const report = await createI18NReport({...options, exclude: ['unused_js', 'unused_yaml', 'unused_json']});
const exclude = ['unused_js', 'unused_yaml', 'unused_json', 'unused_nested.auth', 'unused_nested.forgot'];
const report = await createI18NReport({...options, exclude});
const expectedI18NReportWithoutExcludedKeys = {...expectedI18NReport, unusedKeys: []};
expect(report).toEqual(
expectedI18NReportWithoutExcludedKeys
);
});

it('Should exclude nested keys if a parent key was excluded', async () => {
const exclude = ['unused_js', 'unused_yaml', 'unused_json', 'unused_nested'];
const report = await createI18NReport({...options, exclude});
const expectedI18NReportWithoutExcludedKeys = {...expectedI18NReport, unusedKeys: []};
expect(report).toEqual(
expectedI18NReportWithoutExcludedKeys
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/create-report/language-files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('file: create-report/language-files', () => {
jest.resetAllMocks();
const dotDeleteSpy = jest.spyOn(dot, 'delete');
removeUnusedFromLanguageFiles(readLanguageFiles(languageFiles), expectedI18NReport.unusedKeys);
expect(dotDeleteSpy).toHaveBeenCalledTimes(5);
expect(dotDeleteSpy).toHaveBeenCalledTimes(7);
expect(writeFileSyncSpy).toHaveBeenCalledTimes(3);
expect(writeFileSyncSpy.mock.calls[0][1]).not.toContain('unused');
});
Expand Down