vscode/.eslintplugin/code-translation-remind.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

68 lines
2.1 KiB
TypeScript
Raw Normal View History

2019-12-30 10:09:38 +00:00
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as eslint from 'eslint';
import { TSESTree } from '@typescript-eslint/experimental-utils';
2019-12-30 10:09:38 +00:00
import { readFileSync } from 'fs';
import { createImportRuleListener } from './utils';
2019-12-30 10:09:38 +00:00
export = new class TranslationRemind implements eslint.Rule.RuleModule {
private static NLS_MODULE = 'vs/nls';
readonly meta: eslint.Rule.RuleMetaData = {
2019-12-30 10:09:38 +00:00
messages: {
missing: 'Please add \'{{resource}}\' to ./build/lib/i18n.resources.json file to use translations here.'
}
};
create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener {
return createImportRuleListener((node, path) => this._checkImport(context, node, path));
2019-12-30 10:09:38 +00:00
}
private _checkImport(context: eslint.Rule.RuleContext, node: TSESTree.Node, path: string) {
2019-12-30 10:09:38 +00:00
if (path !== TranslationRemind.NLS_MODULE) {
return;
}
2019-12-30 10:09:38 +00:00
const currentFile = context.getFilename();
const matchService = currentFile.match(/vs\/workbench\/services\/\w+/);
const matchPart = currentFile.match(/vs\/workbench\/contrib\/\w+/);
if (!matchService && !matchPart) {
return;
}
2019-12-30 10:09:38 +00:00
const resource = matchService ? matchService[0] : matchPart![0];
let resourceDefined = false;
2019-12-30 10:09:38 +00:00
let json;
try {
json = readFileSync('./build/lib/i18n.resources.json', 'utf8');
} catch (e) {
console.error('[translation-remind rule]: File with resources to pull from Transifex was not found. Aborting translation resource check for newly defined workbench part/service.');
return;
}
const workbenchResources = JSON.parse(json).workbench;
workbenchResources.forEach((existingResource: any) => {
if (existingResource.name === resource) {
resourceDefined = true;
2019-12-30 10:09:38 +00:00
return;
}
});
2019-12-30 10:09:38 +00:00
if (!resourceDefined) {
context.report({
loc: node.loc,
messageId: 'missing',
data: { resource }
2019-12-30 10:09:38 +00:00
});
}
}
};