File size: 2,777 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @file Utility for extracting strings from node
 * @author Automattic
 * @copyright 2016 Automattic. All rights reserved.
 * See LICENSE.md file in root directory for full license.
 */

const parser = require( '@babel/eslint-parser' );
const getTextContentFromNode = require( '../../../lib/util/get-text-content-from-node.js' );

function parseCode( code ) {
	const programNode = parser.parse( code, {
		requireConfigFile: false,
		babelOptions: { configFile: false },
	} );
	// Espree thinks it's parsing a whole program, so we just need to peel away
	// the 'Program' packaging.
	return programNode.body[ 0 ];
}
function parseExpressionStatement( code ) {
	const node = parseCode( code ).expression;
	return node;
}

describe( '#getStringFromNode', function () {
	it( 'should return simple strings', function () {
		const result = getTextContentFromNode( parseExpressionStatement( "'a simple string'" ) );
		expect( result ).toEqual( 'a simple string' );
	} );

	it( 'should return concatentated strings', function () {
		const result = getTextContentFromNode(
			parseExpressionStatement( '"A string" + " in two parts"' )
		);
		expect( result ).toEqual( 'A string in two parts' );
	} );

	it( 'should return more concatentated strings', function () {
		const result = getTextContentFromNode(
			parseExpressionStatement( '"A string" + " in " + "three parts"' )
		);
		expect( result ).toEqual( 'A string in three parts' );
	} );

	it( 'should return strings from template literals', function () {
		const result = getTextContentFromNode(
			parseExpressionStatement( '`A template literal string`' )
		);
		expect( result ).toEqual( 'A template literal string' );
	} );

	it( 'should handle different literal types', function () {
		const result = getTextContentFromNode(
			parseExpressionStatement( '`A template` + " and a string"' )
		);
		expect( result ).toEqual( 'A template and a string' );
	} );

	it( 'should return false for functions', function () {
		const functionNode = parseExpressionStatement( 'foo()' );

		expect( getTextContentFromNode( functionNode ) ).toStrictEqual( false );
	} );

	it( 'should return false for variable assignments', function () {
		const variableDeclarationNode = parseCode( "var aVariable = 'a string to assign';" );
		const variableDeclarator = variableDeclarationNode.declarations[ 0 ];

		expect( getTextContentFromNode( variableDeclarationNode ) ).toStrictEqual( false );
		expect( getTextContentFromNode( variableDeclarator ) ).toStrictEqual( false );
	} );

	it( 'should return false for a binary structure including invalid node types', function () {
		const result = getTextContentFromNode(
			parseExpressionStatement( "'a string plus a function' + foo()" )
		);
		expect( result ).toStrictEqual( false );
	} );
} );