-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
145 lines (115 loc) · 3.69 KB
/
index.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* External dependencies
*/
import { transformSync } from '@babel/core';
import traverse from '@babel/traverse';
/**
* Internal dependencies
*/
import babelPlugin from '..';
describe( 'babel-plugin', () => {
const {
getNodeAsString,
getExtractedComment,
isValidTranslationKey,
isSameTranslation,
} = babelPlugin;
describe( '.isValidTranslationKey()', () => {
it( 'should return false if not one of valid keys', () => {
expect( isValidTranslationKey( 'foo' ) ).toBe( false );
} );
it( 'should return true if one of valid keys', () => {
expect( isValidTranslationKey( 'msgid' ) ).toBe( true );
} );
} );
describe( '.isSameTranslation()', () => {
it( 'should return false if any translation keys differ', () => {
const a = { msgid: 'foo' };
const b = { msgid: 'bar' };
expect( isSameTranslation( a, b ) ).toBe( false );
} );
it( 'should return true if all translation keys the same', () => {
const a = { msgid: 'foo', comments: { reference: 'a' } };
const b = { msgid: 'foo', comments: { reference: 'b' } };
expect( isSameTranslation( a, b ) ).toBe( true );
} );
} );
describe( '.getExtractedComment()', () => {
function getCommentFromString( string ) {
let comment;
traverse(
transformSync( string, { ast: true, filename: 'test.js' } ).ast,
{
CallExpression( path ) {
comment = getExtractedComment( path );
},
}
);
return comment;
}
it( 'should not return translator comment on same line but after call expression', () => {
const comment = getCommentFromString(
"__( 'Hello world' ); // translators: Greeting"
);
expect( comment ).toBeUndefined();
} );
it( 'should return translator comment on leading comments', () => {
const comment = getCommentFromString(
"// translators: Greeting\n__( 'Hello world' );"
);
expect( comment ).toBe( 'Greeting' );
} );
it( 'should be case insensitive to translator prefix', () => {
const comment = getCommentFromString(
"// TrANslAtORs: Greeting\n__( 'Hello world' );"
);
expect( comment ).toBe( 'Greeting' );
} );
it( 'should traverse up parents until it encounters comment', () => {
const comment = getCommentFromString(
"// translators: Greeting\nconst string = __( 'Hello world' );"
);
expect( comment ).toBe( 'Greeting' );
} );
it( 'should not consider comment if it does not end on same or previous line', () => {
const comment = getCommentFromString(
"// translators: Greeting\n\n__( 'Hello world' );"
);
expect( comment ).toBeUndefined();
} );
it( 'should use multi-line comment starting many lines previous', () => {
const comment = getCommentFromString(
"/* translators: Long comment\nspanning multiple \nlines */\nconst string = __( 'Hello world' );"
);
expect( comment ).toBe( 'Long comment spanning multiple lines' );
} );
} );
describe( '.getNodeAsString()', () => {
function getNodeAsStringFromArgument( source ) {
let string;
traverse(
transformSync( source, { ast: true, filename: 'test.js' } ).ast,
{
CallExpression( path ) {
string = getNodeAsString( path.node.arguments[ 0 ] );
},
}
);
return string;
}
it( 'should returns an empty string by default', () => {
const string = getNodeAsStringFromArgument( '__( {} );' );
expect( string ).toBe( '' );
} );
it( 'should return a string value', () => {
const string = getNodeAsStringFromArgument( '__( "hello" );' );
expect( string ).toBe( 'hello' );
} );
it( 'should be a concatenated binary expression string value', () => {
const string = getNodeAsStringFromArgument(
'__( "hello" + " world" );'
);
expect( string ).toBe( 'hello world' );
} );
} );
} );