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' ;
2019-12-30 13:08:44 +00:00
import { TSESTree } from '@typescript-eslint/experimental-utils' ;
2019-12-30 10:09:38 +00:00
import { readFileSync } from 'fs' ;
2019-12-30 12:01:33 +00:00
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' ;
2019-12-31 10:32:41 +00:00
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 {
2019-12-30 12:01:33 +00:00
return createImportRuleListener ( ( node , path ) = > this . _checkImport ( context , node , path ) ) ;
2019-12-30 10:09:38 +00:00
}
2019-12-30 13:08:44 +00:00
private _checkImport ( context : eslint.Rule.RuleContext , node : TSESTree.Node , path : string ) {
2019-12-30 10:09:38 +00:00
2019-12-30 12:01:33 +00:00
if ( path !== TranslationRemind . NLS_MODULE ) {
return ;
}
2019-12-30 10:09:38 +00:00
2019-12-30 12:01:33 +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
2019-12-30 12:01:33 +00:00
const resource = matchService ? matchService [ 0 ] : matchPart ! [ 0 ] ;
let resourceDefined = false ;
2019-12-30 10:09:38 +00:00
2019-12-30 12:01:33 +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 12:01:33 +00:00
} ) ;
2019-12-30 10:09:38 +00:00
2019-12-30 12:01:33 +00:00
if ( ! resourceDefined ) {
context . report ( {
2019-12-30 13:08:44 +00:00
loc : node.loc ,
2019-12-30 12:01:33 +00:00
messageId : 'missing' ,
data : { resource }
2019-12-30 10:09:38 +00:00
} ) ;
}
}
} ;