Skip to content

Commit d9317e5

Browse files
committed
Security/Underscorejs: ignore Gruntfile.js files
These are configuration files and not part of the production code. This check does not verify whether this file is in the project root as we don't know what the project root is. It will plainly ignore any file called `Gruntfile.js` in a case-insensitive manner. Includes unit tests.
1 parent 1ab2794 commit d9317e5

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

WordPressVIPMinimum/Sniffs/Security/UnderscorejsSniff.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ public function register() {
6161
* @return void
6262
*/
6363
public function process_token( $stackPtr ) {
64+
/*
65+
* Ignore Gruntfile.js files as they are configuration, not code.
66+
*/
67+
$file_name = $this->strip_quotes( $this->phpcsFile->getFileName() );
68+
$file_name = strtolower( basename( $file_name ) );
69+
70+
if ( $file_name === 'gruntfile.js' ) {
71+
return;
72+
}
73+
6474
/*
6575
* Check for delimiter change in JS files.
6676
*/
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
module.exports = function(grunt) {
3+
4+
require('load-grunt-tasks')(grunt);
5+
6+
// Project configuration.
7+
grunt.initConfig({
8+
pkg: grunt.file.readJSON('package.json'),
9+
10+
checktextdomain: {
11+
options:{
12+
text_domain: '<%= pkg.name %>',
13+
correct_domain: true,
14+
keywords: [
15+
'__:1,2d',
16+
'_e:1,2d',
17+
'_x:1,2c,3d',
18+
'esc_html__:1,2d',
19+
'esc_html_e:1,2d',
20+
'esc_html_x:1,2c,3d',
21+
'esc_attr__:1,2d',
22+
'esc_attr_e:1,2d',
23+
'esc_attr_x:1,2c,3d',
24+
'_ex:1,2c,3d',
25+
'_n:1,2,4d',
26+
'_nx:1,2,4c,5d',
27+
'_n_noop:1,2,3d',
28+
'_nx_noop:1,2,3c,4d'
29+
]
30+
},
31+
files: {
32+
src: [
33+
'**/*.php',
34+
],
35+
expand: true
36+
}
37+
},
38+
39+
makepot: {
40+
target: {
41+
options: {
42+
domainPath: '/languages/', // Where to save the POT file.
43+
mainFile: 'style.css', // Main project file.
44+
potFilename: '<%= pkg.name %>.pot', // Name of the POT file.
45+
type: 'wp-theme', // Type of project (wp-plugin or wp-theme).
46+
processPot: function( pot, options ) {
47+
pot.headers['plural-forms'] = 'nplurals=2; plural=n != 1;';
48+
pot.headers['x-poedit-basepath'] = '.\n';
49+
pot.headers['x-poedit-language'] = 'English\n';
50+
pot.headers['x-poedit-country'] = 'UNITED STATES\n';
51+
pot.headers['x-poedit-sourcecharset'] = 'utf-8\n';
52+
pot.headers['X-Poedit-KeywordsList'] = '__;_e;__ngettext:1,2;_n:1,2;__ngettext_noop:1,2;_n_noop:1,2;_c,_nc:4c,1,2;_x:1,2c;_ex:1,2c;_nx:4c,1,2;_nx_noop:4c,1,2;\n';
53+
pot.headers['x-textdomain-support'] = 'yes\n';
54+
return pot;
55+
}
56+
}
57+
}
58+
},
59+
60+
// Clean up build directory
61+
clean: {
62+
main: ['build/<%= pkg.name %>']
63+
},
64+
65+
// Copy the theme into the build directory
66+
copy: {
67+
main: {
68+
src: [
69+
'**',
70+
'!build/**',
71+
'!.git/**',
72+
'!Gruntfile.js',
73+
'!package.json',
74+
'!.gitignore',
75+
'!.gitmodules',
76+
],
77+
dest: 'build/<%= pkg.name %>/'
78+
}
79+
},
80+
81+
//Compress build directory into <name>.zip and <name>-<version>.zip
82+
compress: {
83+
main: {
84+
options: {
85+
mode: 'zip',
86+
archive: './build/<%= pkg.name %>.zip'
87+
},
88+
expand: true,
89+
cwd: 'build/<%= pkg.name %>/',
90+
src: ['**/*'],
91+
dest: '<%= pkg.name %>/'
92+
}
93+
}
94+
95+
});
96+
97+
// Default task(s).
98+
grunt.registerTask( 'build', [ 'clean', 'copy', 'compress' ] );
99+
grunt.registerTask( 'i18n', [ 'checktextdomain', 'makepot' ] );
100+
};

WordPressVIPMinimum/Tests/Security/UnderscorejsUnitTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@
1818
*/
1919
class UnderscorejsUnitTest extends AbstractSniffUnitTest {
2020

21+
/**
22+
* Get a list of all test files to check.
23+
*
24+
* @param string $testFileBase The base path that the unit tests files will have.
25+
*
26+
* @return string[]
27+
*/
28+
protected function getTestFiles( $testFileBase ) {
29+
return [
30+
$testFileBase . 'inc',
31+
$testFileBase . 'js',
32+
__DIR__ . DIRECTORY_SEPARATOR . 'Gruntfile.js',
33+
];
34+
}
35+
2136
/**
2237
* Returns the lines where errors should occur.
2338
*

0 commit comments

Comments
 (0)