Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cjs bare exports expression detect #4375

Merged
merged 4 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ impl Visit for CommonJsExportDependencyScanner<'_> {
{
return;
}
// var a = exports/module.exports/this;
// Object.setPrototypeOf(exports/module.exports/this, a);
// ...
if self.is_exports_or_module_exports_or_this_expr(expr) {
self.bailout();
}
expr.visit_children_with(self);
}

Expand All @@ -114,12 +120,16 @@ impl Visit for CommonJsExportDependencyScanner<'_> {
// const flagIt = () => { exports.__esModule = true }; => stmt_level = 2, last_stmt_is_expr_stmt = true
// (exports.__esModule = true); => stmt_level = 1, last_stmt_is_expr_stmt = true
self.stmt_level == 1 && self.last_stmt_is_expr_stmt,
&assign_expr.right,
Some(&assign_expr.right),
);
assign_expr.right.visit_children_with(self);
return;
}
// exports.xxx = 1;
if self.is_exports_member_expr_start(expr) {
self.enable();
assign_expr.right.visit_children_with(self);
return;
}
if self.is_exports_or_module_exports_or_this_expr(expr) {
self.enable();
Expand All @@ -135,14 +145,10 @@ impl Visit for CommonJsExportDependencyScanner<'_> {
// this = {};
self.bailout();
}
assign_expr.right.visit_children_with(self);
return;
}
}
// var a = exports;
// var a = module.exports;
// var a = this;
if self.is_exports_or_module_exports_or_this_expr(&assign_expr.right) {
self.bailout();
}
assign_expr.visit_children_with(self);
}

Expand All @@ -151,15 +157,31 @@ impl Visit for CommonJsExportDependencyScanner<'_> {
// Object.defineProperty(exports, "__esModule", { value: true });
// Object.defineProperty(module.exports, "__esModule", { value: true });
// Object.defineProperty(this, "__esModule", { value: true });
if expr_matcher::is_object_define_property(expr) && let Some(ExprOrSpread { expr, .. }) = call_expr.args.get(0) && let Some(ExprOrSpread { expr: box Expr::Lit(Lit::Str(str)), .. }) = call_expr.args.get(1) && &str.value == "__esModule" && let Some(value) = get_value_of_property_description(&call_expr.args.get(2)) && self.is_exports_or_module_exports_or_this_expr(expr) {
if expr_matcher::is_object_define_property(expr)
&& let Some(ExprOrSpread { expr, .. }) = call_expr.args.get(0)
&& self.is_exports_or_module_exports_or_this_expr(expr)
&& let Some(arg2) = call_expr.args.get(2) {
self.enable();
self.check_namespace(self.stmt_level == 1, value);

if let Some(ExprOrSpread { expr: box Expr::Lit(Lit::Str(str)), .. }) = call_expr.args.get(1)
&& str.value == "__esModule" {
self.check_namespace(self.stmt_level == 1, get_value_of_property_description(arg2));
}

self.enter_call += 1;
arg2.visit_children_with(self);
self.enter_call -= 1;
return;
}
// exports()
// module.exports()
// this()
if self.is_exports_or_module_exports_or_this_expr(expr) {
self.bailout();
self.enter_call += 1;
call_expr.args.visit_children_with(self);
self.enter_call -= 1;
return;
}
}
self.enter_call += 1;
Expand Down Expand Up @@ -189,11 +211,11 @@ impl<'a> CommonJsExportDependencyScanner<'a> {
matches!(expr, Expr::Ident(ident) if &ident.sym == "exports" && ident.span.ctxt == *self.unresolved_ctxt)
}

fn check_namespace(&mut self, top_level: bool, value_expr: &Expr) {
fn check_namespace(&mut self, top_level: bool, value_expr: Option<&Expr>) {
if matches!(self.parser_exports_state, Some(false)) || self.parser_exports_state.is_none() {
return;
}
if is_truthy_literal(value_expr) && top_level {
if let Some(value_expr) = value_expr && is_truthy_literal(value_expr) && top_level {
self.set_flagged();
} else {
self.set_dynamic();
Expand Down Expand Up @@ -241,13 +263,11 @@ impl<'a> CommonJsExportDependencyScanner<'a> {
}
}

fn get_value_of_property_description<'a>(
expr_or_spread: &Option<&'a ExprOrSpread>,
) -> Option<&'a Expr> {
if let Some(ExprOrSpread {
fn get_value_of_property_description(expr_or_spread: &ExprOrSpread) -> Option<&Expr> {
if let ExprOrSpread {
expr: box Expr::Object(ObjectLit { props, .. }),
..
}) = expr_or_spread
} = expr_or_spread
{
for prop in props {
if let PropOrSpread::Prop(prop) = prop && let Prop::KeyValue(key_value_prop) = &**prop && let PropName::Ident(ident) = &key_value_prop.key && &ident.sym == "value" {
Expand Down
4 changes: 0 additions & 4 deletions webpack-test/cases/json/data/test.filter.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { FilteredStatus } = require("../../../lib/util/filterUtil")

module.exports = () => {return false}
module.exports = () => {return [FilteredStatus.PARTIAL_PASS, "https://github.com/web-infra-dev/rspack/issues/3823"]}


4 changes: 0 additions & 4 deletions webpack-test/cases/json/default-default/test.filter.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

module.exports = () => {return false}
module.exports = () => {return "https://github.com/web-infra-dev/rspack/issues/4358"}


Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { FilteredStatus } = require("../../../lib/util/filterUtil")

module.exports = () => {return false}
module.exports = () => {return [FilteredStatus.PARTIAL_PASS, "https://github.com/web-infra-dev/rspack/issues/3823, https://github.com/web-infra-dev/rspack/issues/4323"]}


2 changes: 1 addition & 1 deletion webpack-test/cases/json/import-by-name/test.filter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

module.exports = () => {return false}
module.exports = () => {return "https://github.com/web-infra-dev/rspack/issues/4323"}


4 changes: 0 additions & 4 deletions webpack-test/cases/json/import-lazy/test.filter.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { FilteredStatus } = require("../../../lib/util/filterUtil")

module.exports = () => {return false}
module.exports = () => {return [FilteredStatus.PARTIAL_PASS, "https://github.com/web-infra-dev/rspack/issues/3823"]}


4 changes: 0 additions & 4 deletions webpack-test/cases/json/import-with-default/test.filter.js

This file was deleted.

4 changes: 0 additions & 4 deletions webpack-test/cases/json/prototype-methods/test.filter.js

This file was deleted.

3 changes: 2 additions & 1 deletion webpack-test/cases/json/reexport/test.filter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { FilteredStatus } = require("../../../lib/util/filterUtil")

module.exports = () => {return false}
module.exports = () => {return [FilteredStatus.PARTIAL_PASS, "https://github.com/web-infra-dev/rspack/issues/4323"]}


4 changes: 0 additions & 4 deletions webpack-test/cases/json/weird-properties/test.filter.js

This file was deleted.

2 changes: 1 addition & 1 deletion webpack-test/cases/large/big-assets/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const createHash = require("../../../../lib/util/hash/xxhash64");
const createHash = require("@rspack/core/dist/util/hash/xxhash64");
const fs = require("fs");

const h = url => {
Expand Down
2 changes: 1 addition & 1 deletion webpack-test/cases/large/big-assets/test.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ module.exports = function (config) {
};

*/
module.exports = () => {return false}
module.exports = () => {return "https://github.com/web-infra-dev/rspack/issues/3180"}


2 changes: 1 addition & 1 deletion webpack-test/cases/large/many-replacements/test.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ module.exports = function (config) {
};

*/
module.exports = () => {return false}
module.exports = () => {return "https://github.com/web-infra-dev/rspack/issues/4396"}


4 changes: 0 additions & 4 deletions webpack-test/cases/loaders/async/test.filter.js

This file was deleted.

2 changes: 1 addition & 1 deletion webpack-test/cases/loaders/coffee-loader/test.filter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

module.exports = () => {return false}
module.exports = () => {return "https://github.com/web-infra-dev/rspack/issues/4396"}


2 changes: 1 addition & 1 deletion webpack-test/cases/loaders/context/test.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ module.exports = config => {
};

*/
module.exports = () => {return false}
module.exports = () => {return "https://github.com/web-infra-dev/rspack/issues/4397"}


4 changes: 0 additions & 4 deletions webpack-test/cases/loaders/css-loader/test.filter.js

This file was deleted.

2 changes: 1 addition & 1 deletion webpack-test/cases/loaders/import-module/test.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ module.exports = config => {
};

*/
module.exports = () => {return false}
module.exports = () => {return "https://github.com/web-infra-dev/rspack/issues/4398"}


4 changes: 0 additions & 4 deletions webpack-test/cases/loaders/json-loader/test.filter.js

This file was deleted.

4 changes: 0 additions & 4 deletions webpack-test/cases/loaders/less-loader/test.filter.js

This file was deleted.

This file was deleted.

7 changes: 0 additions & 7 deletions webpack-test/cases/loaders/pug-loader/test.filter.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@

/*
const supportsRequireInModule = require("../../../helpers/supportsRequireInModule");

module.exports = config => {
return !config.module || supportsRequireInModule();
};

*/
module.exports = () => {return true}


6 changes: 2 additions & 4 deletions webpack-test/cases/loaders/query/test.filter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const { FilteredStatus } = require("../../../lib/util/filterUtil")

// Didn't turn on every cases
module.exports = () => {return true}


module.exports = () => {return [FilteredStatus.PARTIAL_PASS, "https://github.com/web-infra-dev/rspack/issues/3180, https://github.com/web-infra-dev/rspack/issues/4397"]}
4 changes: 0 additions & 4 deletions webpack-test/cases/loaders/raw-loader/test.filter.js

This file was deleted.

2 changes: 1 addition & 1 deletion webpack-test/cases/loaders/resolve/test.filter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

module.exports = () => {return "Rspack does not support resolving a virtual resource with only loader available, see: query test"}
module.exports = () => {return "https://github.com/web-infra-dev/rspack/issues/3180"}


2 changes: 1 addition & 1 deletion webpack-test/cases/loaders/utils/test.filter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

module.exports = () => {return "blocked by context support for loader"}
module.exports = () => {return "https://github.com/web-infra-dev/rspack/issues/4397"}


Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions webpack-test/cases/mjs/no-module-main-field/test.filter.js

This file was deleted.

1 change: 0 additions & 1 deletion webpack-test/cases/mjs/type-module/test.fitler.js

This file was deleted.

2 changes: 1 addition & 1 deletion webpack-test/cases/nonce/set-nonce/test.filter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

module.exports = () => {return false}
module.exports = () => {return "https://github.com/web-infra-dev/rspack/issues/4401"}


3 changes: 2 additions & 1 deletion webpack-test/scripts/test-metric-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ function renderAllTestsToMarkdown(jsonObj) {
const testResults = jsonObj["testResults"];
return testResults
.flatMap(testSuite => testSuite.assertionResults)
.map((test, index) => `${index + 1}. ${renderTestToMarkdown(test.fullName)}`)
// use 1\. to break GitHub markdown list auto ordering
.map((test, index) => `${index + 1}\. ${renderTestToMarkdown(test.fullName)}`)
.join('\n')
}

Expand Down
Loading