Skip to content

Commit

Permalink
Merge pull request #1511 from stealjs/comment-es
Browse files Browse the repository at this point in the history
ES module detection, remove strings before removing comments
  • Loading branch information
matthewp authored Oct 21, 2019
2 parents de95e05 + d47b975 commit 0d8a037
Show file tree
Hide file tree
Showing 26 changed files with 49 additions and 26 deletions.
4 changes: 3 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3454,6 +3454,7 @@ function es6(loader) {

// good enough ES6 detection regex - format detections not designed to be accurate, but to handle the 99% use case
var es6RegEx = /(^\s*|[}\);\n]\s*)(import\s+(['"]|(\*\s+as\s+)?[^"'\(\)\n;]+\s+from\s+['"]|\{)|export\s+\*\s+from\s+["']|export\s+(\{|default|function|class|var|const|let|async\s+function))/;
var strictStringRegEx = /["'].*["']/g;
var strictCommentRegEx = /\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm;

var traceurRuntimeRegEx = /\$traceurRuntime\s*\./;
Expand Down Expand Up @@ -3493,7 +3494,8 @@ function es6(loader) {
};

function looksLikeES6(source) {
var sourceWithComments = source.replace(strictCommentRegEx, '$1');
var sourceWithComments = source.replace(strictStringRegEx, '""')
.replace(strictCommentRegEx, '$1');
return sourceWithComments.match(es6RegEx);
}

Expand Down
4 changes: 3 additions & 1 deletion src/base/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,7 @@ function es6(loader) {

// good enough ES6 detection regex - format detections not designed to be accurate, but to handle the 99% use case
var es6RegEx = /(^\s*|[}\);\n]\s*)(import\s+(['"]|(\*\s+as\s+)?[^"'\(\)\n;]+\s+from\s+['"]|\{)|export\s+\*\s+from\s+["']|export\s+(\{|default|function|class|var|const|let|async\s+function))/;
var strictStringRegEx = /["'].*["']/g;
var strictCommentRegEx = /\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm;

var traceurRuntimeRegEx = /\$traceurRuntime\s*\./;
Expand Down Expand Up @@ -1048,7 +1049,8 @@ function es6(loader) {
};

function looksLikeES6(source) {
var sourceWithComments = source.replace(strictCommentRegEx, '$1');
var sourceWithComments = source.replace(strictStringRegEx, '""')
.replace(strictCommentRegEx, '$1');
return sourceWithComments.match(es6RegEx);
}

Expand Down
4 changes: 3 additions & 1 deletion src/base/lib/extension-es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function es6(loader) {

// good enough ES6 detection regex - format detections not designed to be accurate, but to handle the 99% use case
var es6RegEx = /(^\s*|[}\);\n]\s*)(import\s+(['"]|(\*\s+as\s+)?[^"'\(\)\n;]+\s+from\s+['"]|\{)|export\s+\*\s+from\s+["']|export\s+(\{|default|function|class|var|const|let|async\s+function))/;
var strictStringRegEx = /["'].*["']/g;
var strictCommentRegEx = /\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm;

var traceurRuntimeRegEx = /\$traceurRuntime\s*\./;
Expand Down Expand Up @@ -45,7 +46,8 @@ function es6(loader) {
};

function looksLikeES6(source) {
var sourceWithComments = source.replace(strictCommentRegEx, '$1');
var sourceWithComments = source.replace(strictStringRegEx, '""')
.replace(strictCommentRegEx, '$1');
return sourceWithComments.match(es6RegEx);
}

Expand Down
4 changes: 3 additions & 1 deletion steal-with-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -4602,6 +4602,7 @@ function es6(loader) {

// good enough ES6 detection regex - format detections not designed to be accurate, but to handle the 99% use case
var es6RegEx = /(^\s*|[}\);\n]\s*)(import\s+(['"]|(\*\s+as\s+)?[^"'\(\)\n;]+\s+from\s+['"]|\{)|export\s+\*\s+from\s+["']|export\s+(\{|default|function|class|var|const|let|async\s+function))/;
var strictStringRegEx = /["'].*["']/g;
var strictCommentRegEx = /\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm;

var traceurRuntimeRegEx = /\$traceurRuntime\s*\./;
Expand Down Expand Up @@ -4641,7 +4642,8 @@ function es6(loader) {
};

function looksLikeES6(source) {
var sourceWithComments = source.replace(strictCommentRegEx, '$1');
var sourceWithComments = source.replace(strictStringRegEx, '""')
.replace(strictCommentRegEx, '$1');
return sourceWithComments.match(es6RegEx);
}

Expand Down
2 changes: 1 addition & 1 deletion steal-with-promises.production.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion steal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3332,6 +3332,7 @@ function es6(loader) {

// good enough ES6 detection regex - format detections not designed to be accurate, but to handle the 99% use case
var es6RegEx = /(^\s*|[}\);\n]\s*)(import\s+(['"]|(\*\s+as\s+)?[^"'\(\)\n;]+\s+from\s+['"]|\{)|export\s+\*\s+from\s+["']|export\s+(\{|default|function|class|var|const|let|async\s+function))/;
var strictStringRegEx = /["'].*["']/g;
var strictCommentRegEx = /\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm;

var traceurRuntimeRegEx = /\$traceurRuntime\s*\./;
Expand Down Expand Up @@ -3371,7 +3372,8 @@ function es6(loader) {
};

function looksLikeES6(source) {
var sourceWithComments = source.replace(strictCommentRegEx, '$1');
var sourceWithComments = source.replace(strictStringRegEx, '""')
.replace(strictCommentRegEx, '$1');
return sourceWithComments.match(es6RegEx);
}

Expand Down
2 changes: 1 addition & 1 deletion steal.production.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/bad_json/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
window.WARN(msg);
};
</script>
<script src="../../../steal-with-promises.js"
<script src="../../steal-with-promises.js"
data-base-url="."
data-config="package.json!npm"
data-main="@empty"></script>
Expand Down
14 changes: 12 additions & 2 deletions test/cjs_export_default/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,28 @@
<script>
steal.import("~/index")
.then(function(index){

if(window.assert) {
window.assert.equal(index.foo, "bar", "loaded correctly");
window.done();
} else {
console.log("index module:", index);
}

return steal.import("~/es");
})
.then(function(es) {
if(window.assert) {
window.assert.equal(es.default.default, "//", "comment in a string");
window.done();
} else {
console.log("es module", es);
}
})
.catch(function(err) {
if(window.assert) {
window.assert.ok(false, err);
window.done();
} else {
console.error("Oops", err);
}
});
</script>
Expand Down
1 change: 1 addition & 0 deletions test/cjs_export_default/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var french={default:"//"};export default french;
2 changes: 1 addition & 1 deletion test/dev_bundle_err/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
window.assert = window.parent.assert;
window.done = window.parent.done;
</script>
<script src="../../../steal-with-promises.js"
<script src="../../steal-with-promises.js"
data-base-url="."
data-config="package.json!npm"
data-dev-bundle></script>
Expand Down
2 changes: 1 addition & 1 deletion test/http_spec/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
return _send.apply(this, arguments);
};
</script>
<script src="../../../steal-with-promises.js"
<script src="../../steal-with-promises.js"
data-base-url="."
data-config="package.json!npm"></script>
<script>
Expand Down
2 changes: 1 addition & 1 deletion test/init_error/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</script>
<script
main="~/main"
src="../../../steal-with-promises.js"
src="../../steal-with-promises.js"
data-base-url="."
data-config="package.json!npm"></script>
<script>
Expand Down
2 changes: 1 addition & 1 deletion test/json_syntax_err/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</script>
<script
main="~/main"
src="../../../steal-with-promises.js"
src="../../steal-with-promises.js"
data-base-url="."
data-config="package.json!npm"></script>
<script>
Expand Down
2 changes: 1 addition & 1 deletion test/load_after_fail/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
window.assert = window.parent.assert;
window.done = window.parent.done;
</script>
<script src="../../../steal-with-promises.js" data-base-url="."
<script src="../../steal-with-promises.js" data-base-url="."
data-config="package.json!npm" data-main="~/oops"></script>
<script>
steal.done().then(null, function(err){
Expand Down
2 changes: 1 addition & 1 deletion test/load_module_twice/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
main="~/main"
base-url="."
config="package.json!npm"
src="../../../steal-with-promises.js"
src="../../steal-with-promises.js"
>
</script>
</body>
Expand Down
2 changes: 1 addition & 1 deletion test/load_module_twice_clone/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
main="~/main"
base-url="."
config="package.json!npm"
src="../../../steal-with-promises.js"
src="../../steal-with-promises.js"
>
</script>
<script>
Expand Down
2 changes: 1 addition & 1 deletion test/load_module_twice_false_positive/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
main="main"
base-url="."
config="package.json!npm"
src="../../../steal-with-promises.js"
src="../../steal-with-promises.js"
></script>
<script>
steal.done().then(function(){
Expand Down
2 changes: 1 addition & 1 deletion test/load_module_twice_false_positive/on-error.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
window.done();
};
</script>
<script src="../../../steal-with-promises.js" data-base-url="."
<script src="../../steal-with-promises.js" data-base-url="."
data-config="package.json!npm" data-main="~/oops"></script>
<script>
function importAModuleThatImportsInvalidPackage() {
Expand Down
2 changes: 1 addition & 1 deletion test/meta_deps/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
window.assert = window.parent.assert;
window.done = window.parent.done;
</script>
<script main src="../../../steal-with-promises.js" base-url="." config="package.json!npm">
<script main src="../../steal-with-promises.js" base-url="." config="package.json!npm">
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion test/mjs/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
window.done = window.parent.done;
</script>
<script
src="../../../steal-with-promises.js"
src="../../steal-with-promises.js"
data-main
data-base-url="."
data-config="package.json!npm"></script>
Expand Down
2 changes: 1 addition & 1 deletion test/parse_errors/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
window.assert = window.parent.assert;
window.done = window.parent.done;
</script>
<script src="../../../steal-with-promises.js"
<script src="../../steal-with-promises.js"
data-base-url="."
data-config="package.json!npm"
data-main="@empty"></script>
Expand Down
4 changes: 3 additions & 1 deletion test/steal-with-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -4602,6 +4602,7 @@ function es6(loader) {

// good enough ES6 detection regex - format detections not designed to be accurate, but to handle the 99% use case
var es6RegEx = /(^\s*|[}\);\n]\s*)(import\s+(['"]|(\*\s+as\s+)?[^"'\(\)\n;]+\s+from\s+['"]|\{)|export\s+\*\s+from\s+["']|export\s+(\{|default|function|class|var|const|let|async\s+function))/;
var strictStringRegEx = /["'].*["']/g;
var strictCommentRegEx = /\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm;

var traceurRuntimeRegEx = /\$traceurRuntime\s*\./;
Expand Down Expand Up @@ -4641,7 +4642,8 @@ function es6(loader) {
};

function looksLikeES6(source) {
var sourceWithComments = source.replace(strictCommentRegEx, '$1');
var sourceWithComments = source.replace(strictStringRegEx, '""')
.replace(strictCommentRegEx, '$1');
return sourceWithComments.match(es6RegEx);
}

Expand Down
2 changes: 1 addition & 1 deletion test/steal-with-promises.production.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/syntax_errs/build.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
window.assert = window.parent.assert;
window.done = window.parent.done;
</script>
<script src="../../../steal-with-promises.js"
<script src="../../steal-with-promises.js"
data-base-url="."
data-config="package.json!npm"
data-main="@empty"></script>
Expand Down
2 changes: 1 addition & 1 deletion test/syntax_errs/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
window.assert = window.parent.assert;
window.done = window.parent.done;
</script>
<script src="../../../steal-with-promises.js"
<script src="../../steal-with-promises.js"
data-base-url="."
data-config="package.json!npm"
data-main="~/importer"></script>
Expand Down

0 comments on commit 0d8a037

Please sign in to comment.