Skip to content
This repository has been archived by the owner on May 20, 2023. It is now read-only.

Commit

Permalink
Option requiring all messages begin with a given prefix (Fixes #61)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdforrester authored and Krinkle committed Jan 8, 2019
1 parent 17378e4 commit 12a92b1
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ module.exports = function ( grunt ) {
options: {
requireLowerCase: false
}
},
testRequiringSingleKeyPrefix: {
src: 'test/requireKeyPrefix/single',
options: {
requireKeyPrefix: [ 'alice' ]
}
},
testRequiringSingleKeyPrefixShucked: {
src: 'test/requireKeyPrefix/single',
options: {
requireKeyPrefix: 'alice'
}
},
testRequiringMultipleKeyPrefices: {
src: 'test/requireKeyPrefix/multiple',
options: {
requireKeyPrefix: [ 'alice', 'bob', 'timmy' ]
}
}
},
watch: {
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ Default value: `true`
Whether to fail if any message key is not lower case. If set to `"initial"`, fail only if the first
character is not lower case.

#### requireKeyPrefix
Type: `string` or `string[]`
Default value: `[]`

Whether to fail if any message key is not prefixed by the given prefix, or if multiple, one of the
given prefices.

#### disallowUnusedDocumentation
Type: `boolean`
Default value: `true`
Expand Down
39 changes: 39 additions & 0 deletions tasks/banana.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = function ( grunt ) {
disallowUnusedTranslations: false,

requireLowerCase: true,
requireKeyPrefix: [],

requireCompleteTranslationLanguages: [],
requireCompleteTranslationMessages: [],
Expand Down Expand Up @@ -50,6 +51,7 @@ module.exports = function ( grunt ) {
documentationMessageBlanks = [],
sourceMessageMissing = [],
sourceMessageWrongCase = [],
sourceMessageWrongPrefix = [],
count = 0;

function messages( filename, type ) {
Expand Down Expand Up @@ -156,6 +158,20 @@ module.exports = function ( grunt ) {
} );
}

if ( options.requireKeyPrefix.length ) {
if ( typeof options.requireKeyPrefix === 'string' ) {
options.requireKeyPrefix = [ options.requireKeyPrefix ];
}
sourceMessageWrongPrefix = sourceMessageKeys.filter( function ( key ) {
return options.requireKeyPrefix
.map( function ( prefix ) {
return !key.startsWith( prefix );
} ).reduce( function ( failed, accumulator ) {
return accumulator && failed;
} );
} );
}

while ( sourceMessageKeys.length > 0 ) {
message = sourceMessageKeys[ 0 ];

Expand Down Expand Up @@ -231,6 +247,29 @@ module.exports = function ( grunt ) {
}
}

count = sourceMessageWrongPrefix.length;
if ( count > 0 ) {
ok = false;

if ( options.requireKeyPrefix.length === 1 ) {
grunt.log.error(
count + ' message' + ( count > 1 ? 's do' : ' does' ) + ' not start with the required prefix "' + options.requireKeyPrefix[ 0 ] + '".'
);

sourceMessageWrongPrefix.forEach( function ( message ) {
grunt.log.error( 'Message "' + message + '" should start with the required prefix "' + options.requireKeyPrefix[ 0 ] + '".' );
} );
} else {
grunt.log.error(
count + ' message' + ( count > 1 ? 's do' : ' does' ) + ' not start with any of the required prefices.'
);

sourceMessageWrongPrefix.forEach( function ( message ) {
grunt.log.error( 'Message "' + message + '" should start with one of the required prefices.' );
} );
}
}

if ( options.disallowUnusedDocumentation ) {
count = documentationMessageKeys.length;
if ( count > 0 ) {
Expand Down
9 changes: 9 additions & 0 deletions test/requireKeyPrefix/multiple/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"@metadata": {
"metadata-key": "metadata value"
},
"alice-first-message-key": "first message value",
"alice-second-message-key": "second message value",
"bob-third-message-key": "third message value",
"timmyfourth-message-key": "fourth message value"
}
10 changes: 10 additions & 0 deletions test/requireKeyPrefix/multiple/qqq.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"@metadata": {
"first-metadata-key": "metadata value",
"second-metadata-key": "metadata value"
},
"alice-first-message-key": "first message definition",
"alice-second-message-key": "second message definition",
"bob-third-message-key": "third message definition",
"timmyfourth-message-key": "fourth message definition"
}
9 changes: 9 additions & 0 deletions test/requireKeyPrefix/single/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"@metadata": {
"metadata-key": "metadata value"
},
"alice-first-message-key": "first message value",
"alice-second-message-key": "second message value",
"alice-third-message-key": "third message value",
"alice-fourth-message-key": "fourth message value"
}
10 changes: 10 additions & 0 deletions test/requireKeyPrefix/single/qqq.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"@metadata": {
"first-metadata-key": "metadata value",
"second-metadata-key": "metadata value"
},
"alice-first-message-key": "first message definition",
"alice-second-message-key": "second message definition",
"alice-third-message-key": "third message definition",
"alice-fourth-message-key": "fourth message definition"
}

0 comments on commit 12a92b1

Please sign in to comment.