-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathi18n-no-placeholders-only.js
58 lines (47 loc) · 1.9 KB
/
i18n-no-placeholders-only.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @file Disallow strings which include only placeholders
* @author Automattic
* @copyright 2016 Automattic. All rights reserved.
* See LICENSE.md file in root directory for full license.
*/
//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------
// Regular expression adapted from sprintf.js. See CREDITS.md for license information.
const RX_PLACEHOLDERS =
/(?:\x25\x25)|(\x25(?:(?:[1-9]\d*)\$|\((?:[^)]+)\))?(?:\+)?(?:0|'[^$])?(?:-)?(?:\d+)?(?:\.(?:\d+))?(?:[b-fiosuxX]))/g; // eslint-disable-line max-len
const RX_INTERPOLATED_COMPONENTS = /(\{\{\/?\s*\w+\s*\/?\}\})/g;
//------------------------------------------------------------------------------
// Helper Functions
//------------------------------------------------------------------------------
const getCallee = require( '../util/get-callee' );
const getTextContentFromNode = require( '../util/get-text-content-from-node' );
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const rule = ( module.exports = function ( context ) {
return {
CallExpression: function ( node ) {
if ( 'translate' !== getCallee( node ).name ) {
return;
}
node.arguments.forEach( function ( arg ) {
let value = getTextContentFromNode( arg );
if ( 'string' !== typeof value ) {
return;
}
value = value.replace( RX_PLACEHOLDERS, '' );
if ( 0 === value.length ) {
context.report( arg, rule.ERROR_MESSAGE );
return;
}
value = value.replace( RX_INTERPOLATED_COMPONENTS, '' );
if ( 0 === value.length ) {
context.report( arg, rule.ERROR_MESSAGE );
}
} );
},
};
} );
rule.ERROR_MESSAGE = "We shouldn't translate strings that are entirely placeholder";
rule.schema = [];