🌐 AI搜索 & 代理 主页
Skip to content

Conversation

@mgol
Copy link
Member

@mgol mgol commented May 6, 2019

Summary

Fix the regex parsing AMD var-modules.

The previous regex caused the final jQuery binary to have syntax errors for
var-modules with names starting with "return". For example, the following module
wouldn't work when the file is named returnTrue.js:

define( function() {
	"use strict";
	return function returnTrue() {
		return true;
	};
} );

Checklist

@mgol mgol added this to the 4.0.0 milestone May 6, 2019
@mgol mgol requested a review from timmywil May 6, 2019 17:10
Copy link
Member

@timmywil timmywil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current regex works for me, even when the module is named returnTrue. https://jsbin.com/kemuvaj/edit?html,js,console,output

This change doesn't actually seem to match.

Note the ? is there to be non-greedy, meaning it stops on the first occurrence of "return", not going all the way to "returnTrue".

Edit: I realized the current regex would break if another var module included returnTrue as a dependency. However, the first argument to define is not a string, but an array, and it's an optional argument. I suggest the following instead:

/define\((?:\s*\[.*\]\s*)?[\w\W]*?return/

jsbin above updated.

@mgol
Copy link
Member Author

mgol commented May 6, 2019

@timmywil The value of the contents variable is not what's in the file, it gets the name & the dependencies array inserted during compilation (if missing in source). This applies to all modules. For the returnTrue example, the contents value before the replacement is:

define( 'var/returnTrue',[],function() {

"use strict";

return function returnTrue() {
	return true;
};

} );

After the replacement it is:

var returnTrue =True',[],function() {

"use strict";

return function returnTrue() {
	return true;
};

See the updated bin: https://jsbin.com/mufamok/1/edit?html,js,console,output

@mgol
Copy link
Member Author

mgol commented May 6, 2019

This PR is a result of my experiments around #4387. In commit mgol@9a9c3f0 I had to change the regexp because it was mangling my newly-separated returnTrue module. This code will not land in the end but I thought I'll at least submit the regex fix separately to avoid future issues like that.

@timmywil
Copy link
Member

timmywil commented May 6, 2019

Ah, I forgot it inserts the name. Ok.

contents = contents
.replace(
/define\([\w\W]*?return/,
/define\(\s*(["'])[\w\W]*?\1[\w\W]*?return/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simpler suggestion: https://jsbin.com/wozinogigo/1/edit?js,console,output

Suggested change
/define\(\s*(["'])[\w\W]*?\1[\w\W]*?return/,
/define\([\w\W]*?\breturn\b/,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, there could be a directory named return. Not the best idea, perhaps, but my regex should work for all of those weird cases.

The previous regex caused the final jQuery binary to have syntax errors for
var-modules with names starting with "return". For example, the following module
wouldn't work when the file is named `returnTrue.js`:

```js
define( function() {
	"use strict";
	return function returnTrue() {
		return true;
	};
} );
```
@mgol mgol force-pushed the fix-var-modules-regex branch from 800ae7d to 1dcad4f Compare May 13, 2019 19:46
@mgol mgol merged commit 9ec09c3 into jquery:master May 13, 2019
@mgol mgol deleted the fix-var-modules-regex branch May 13, 2019 19:55
mgol added a commit that referenced this pull request Sep 25, 2019
The previous regex caused the final jQuery binary to have syntax errors for
var-modules with names starting with "return". For example, the following module
wouldn't work when the file is named `returnTrue.js`:

```js
define( function() {
	"use strict";
	return function returnTrue() {
		return true;
	};
} );
```

Closes gh-4389

(cherry picked from commit 9ec09c3)
@mgol mgol modified the milestones: 4.0.0, 3.5.0 Sep 25, 2019
@mgol
Copy link
Member Author

mgol commented Sep 25, 2019

Cherry-picked to 3.x-stable in 36b59c9.

@lock lock bot locked as resolved and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Development

Successfully merging this pull request may close these issues.

3 participants