Skip to content

Commit ee2b6bd

Browse files
Editor: Switch from Esprima to Espree for JavaScript validation.
Replaces the outdated `esprima` parser with `espree` to provide support for modern JavaScript (ES6+) validation in the code editor. A new `espree` script handle is registered, and the `fakejshint` wrapper is updated to use it while maintaining the `esprima` handle for backward compatibility. - Adds `espree` to `package.json`. - Creates `tools/vendors/espree-entry.js` and adds a new Webpack entry point to bundle `espree`. - Updates `fakejshint.js` to utilize `window.espree.parse` with ES6+ support. - Registers `espree` in `script-loader.php` and updates `jshint` handle dependencies. - Updates PHPUnit test data provider to include `espree`. Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 1535b0c commit ee2b6bd

7 files changed

Lines changed: 20 additions & 11 deletions

File tree

package-lock.json

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"core-js-url-browser": "3.6.4",
8080
"csslint": "1.0.5",
8181
"element-closest": "3.0.2",
82+
"espree": "9.6.1",
8283
"esprima": "4.0.1",
8384
"formdata-polyfill": "4.0.10",
8485
"hoverintent": "2.2.1",

src/js/_enqueues/vendor/codemirror/fakejshint.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// JSHINT has some GPL Compatability issues, so we are faking it out and using esprima for validation
1+
// JSHINT has some GPL Compatability issues, so we are faking it out and using espree for validation
22
// Based on https://github.com/jquery/esprima/blob/gh-pages/demo/validate.js which is MIT licensed
33

44
var fakeJSHINT = new function() {
@@ -15,9 +15,13 @@ var fakeJSHINT = new function() {
1515
};
1616
this.parse = function( code ){
1717
try {
18-
syntax = window.esprima.parse(code, { tolerant: true, loc: true });
18+
syntax = window.espree.parse(code, {
19+
ecmaVersion: 'latest',
20+
loc: true,
21+
sourceType: 'module'
22+
});
1923
errors = syntax.errors;
20-
if ( errors.length > 0 ) {
24+
if ( errors && errors.length > 0 ) {
2125
for ( var i = 0; i < errors.length; i++) {
2226
var error = errors[i];
2327
that.data.push( that.convertError( error ) );

src/wp-includes/script-loader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,8 +1197,9 @@ function wp_default_scripts( $scripts ) {
11971197

11981198
$scripts->add( 'wp-codemirror', '/wp-includes/js/codemirror/codemirror.min.js', array(), '5.65.20' );
11991199
$scripts->add( 'csslint', '/wp-includes/js/codemirror/csslint.js', array(), '1.0.5' );
1200-
$scripts->add( 'esprima', '/wp-includes/js/codemirror/esprima.js', array(), '4.0.1' );
1201-
$scripts->add( 'jshint', '/wp-includes/js/codemirror/fakejshint.js', array( 'esprima' ), '2.9.5' );
1200+
$scripts->add( 'esprima', '/wp-includes/js/codemirror/esprima.js', array(), '4.0.1' ); // Deprecated. Use 'espree' instead.
1201+
$scripts->add( 'espree', '/wp-includes/js/codemirror/espree.min.js', array(), '9.6.1' );
1202+
$scripts->add( 'jshint', '/wp-includes/js/codemirror/fakejshint.js', array( 'espree' ), '2.9.5' );
12021203
$scripts->add( 'jsonlint', '/wp-includes/js/codemirror/jsonlint.js', array(), '1.6.3' );
12031204
$scripts->add( 'htmlhint', '/wp-includes/js/codemirror/htmlhint.js', array(), '1.8.0' );
12041205
$scripts->add( 'htmlhint-kses', '/wp-includes/js/codemirror/htmlhint-kses.js', array( 'htmlhint' ) );

tests/phpunit/tests/dependencies/scripts.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3859,6 +3859,7 @@ public function data_vendor_script_versions_registered_manually() {
38593859
'csslint' => array( 'csslint' ),
38603860
'element-closest' => array( 'element-closest', 'wp-polyfill-element-closest' ),
38613861
'esprima' => array( 'esprima' ),
3862+
'espree' => array( 'espree' ),
38623863
'formdata-polyfill' => array( 'formdata-polyfill', 'wp-polyfill-formdata' ),
38633864
'imagesloaded' => array( 'imagesloaded' ),
38643865
'jquery-color' => array( 'jquery-color' ),

tools/vendors/espree-entry.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* jshint node:true */
2+
window.espree = require( 'espree' );

tools/webpack/codemirror.config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ module.exports = ( env = { buildTarget: 'src/' } ) => {
1111
return {
1212
target: 'browserslist',
1313
mode: 'production',
14-
entry: './tools/vendors/codemirror-entry.js',
14+
entry: {
15+
'codemirror.min': './tools/vendors/codemirror-entry.js',
16+
'espree.min': './tools/vendors/espree-entry.js',
17+
},
1518
output: {
1619
path: path.resolve( __dirname, '../../', buildTarget, 'wp-includes/js/codemirror' ),
17-
filename: 'codemirror.min.js',
20+
filename: '[name].js',
1821
},
1922
optimization: {
2023
minimize: true,

0 commit comments

Comments
 (0)