|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = function preProcessXGettextJSMatch( match ) { |
|
|
const finalProps = { line: match.line }; |
|
|
|
|
|
if ( ! match.arguments.length ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const args = match.arguments; |
|
|
let options; |
|
|
|
|
|
[ 'single', 'plural', 'options' ].slice( 0, args.length ).forEach( function ( field, i ) { |
|
|
if ( 'StringLiteral' === args[ i ].type ) { |
|
|
finalProps[ field ] = makeDoubleQuoted( args[ i ].extra.raw ); |
|
|
} else if ( 'BinaryExpression' === args[ i ].type ) { |
|
|
finalProps[ field ] = encapsulateString( concatenateBinaryExpression( args[ i ] ) ); |
|
|
} else if ( 'ObjectExpression' === args[ i ].type && 'undefined' === typeof options ) { |
|
|
options = args[ i ]; |
|
|
} else if ( 'TemplateLiteral' === args[ i ].type ) { |
|
|
finalProps[ field ] = makeDoubleQuoted( '`' + args[ i ].quasis[ 0 ].value.raw + '`' ); |
|
|
} |
|
|
} ); |
|
|
|
|
|
if ( 'undefined' !== typeof options ) { |
|
|
|
|
|
options.properties.forEach( function ( property ) { |
|
|
|
|
|
const key = property.key.name || property.key.value; |
|
|
if ( 'StringLiteral' === property.value.type ) { |
|
|
const keyName = key === 'original' ? 'single' : key; |
|
|
finalProps[ keyName ] = |
|
|
'comment' === key ? property.value.value : makeDoubleQuoted( property.value.extra.raw ); |
|
|
} else if ( 'ObjectExpression' === property.value.type && 'original' === key ) { |
|
|
|
|
|
|
|
|
property.value.properties.forEach( function ( innerProp ) { |
|
|
if ( 'StringLiteral' === innerProp.value.type ) { |
|
|
finalProps[ innerProp.key.name || innerProp.key.value ] = makeDoubleQuoted( |
|
|
innerProp.value.extra.raw |
|
|
); |
|
|
} |
|
|
} ); |
|
|
} |
|
|
} ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( typeof finalProps.plural !== 'undefined' ) { |
|
|
finalProps.count = 1; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( ! finalProps.single ) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
return finalProps; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function concatenateBinaryExpression( ASTNode ) { |
|
|
if ( ASTNode.operator !== '+' ) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
let result = |
|
|
'StringLiteral' === ASTNode.left.type |
|
|
? ASTNode.left.value |
|
|
: concatenateBinaryExpression( ASTNode.left ); |
|
|
result += |
|
|
'StringLiteral' === ASTNode.right.type |
|
|
? ASTNode.right.value |
|
|
: concatenateBinaryExpression( ASTNode.right ); |
|
|
|
|
|
return result; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function makeDoubleQuoted( literal ) { |
|
|
if ( ! literal || literal.length < 2 ) { |
|
|
return undefined; |
|
|
} |
|
|
|
|
|
|
|
|
if ( literal.charAt( 0 ) === '"' ) { |
|
|
return literal.replace( /(\\)/g, '\\$1' ); |
|
|
} |
|
|
|
|
|
|
|
|
if ( literal.charAt( 0 ) === "'" ) { |
|
|
return ( |
|
|
'"' + |
|
|
literal |
|
|
.substring( 1, literal.length - 1 ) |
|
|
.replace( /\\'/g, "'" ) |
|
|
.replace( /(\\|")/g, '\\$1' ) + |
|
|
'"' |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
if ( literal.charAt( 0 ) === '`' ) { |
|
|
return ( |
|
|
'"' + |
|
|
literal |
|
|
.substring( 1, literal.length - 1 ) |
|
|
.replace( /`/g, '`' ) |
|
|
.replace( /(\\|")/g, '\\$1' ) + |
|
|
'"' |
|
|
); |
|
|
} |
|
|
|
|
|
return ''; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function encapsulateString( input ) { |
|
|
if ( 'string' !== typeof input ) { |
|
|
return input; |
|
|
} |
|
|
return '"' + input.replace( /(\\|")/g, '\\$1' ) + '"'; |
|
|
} |
|
|
|