summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjuju <gliheng@live.com>2022-01-12 20:05:06 +0800
committerGitHub <noreply@github.com>2022-01-12 13:05:06 +0100
commit50e8ab8a8630a272e9862f2fb7014107474405c6 (patch)
treef6c070945cc67d6b30506d422cdf23d5cb58babd
parent62291e9b0e99406ac7f5a95dc329400f9f966825 (diff)
feat(cli): add ignore directives to bundled code (#13309)
This commit adds lint and fmt ignore directives to bundled code as well as a comment stating that the code was bundled and shouldn't be edited manually.
-rw-r--r--cli/emit.rs19
-rw-r--r--cli/main.rs1
-rw-r--r--cli/ops/runtime_compiler.rs1
-rw-r--r--cli/tests/integration/bundle_tests.rs5
-rw-r--r--cli/tests/integration/compile_tests.rs2
-rw-r--r--cli/tests/testdata/bundle_ignore_directives.test.out6
-rw-r--r--cli/tests/testdata/compiler_api_test.ts13
7 files changed, 44 insertions, 3 deletions
diff --git a/cli/emit.rs b/cli/emit.rs
index 50d4eb821..35d6f1110 100644
--- a/cli/emit.rs
+++ b/cli/emit.rs
@@ -42,6 +42,13 @@ use std::result;
use std::sync::Arc;
use std::time::Instant;
+const IGNORE_DIRECTIVES: &[&str] = &[
+ "// deno-fmt-ignore-file",
+ "// deno-lint-ignore-file",
+ "// This code was bundled using `deno bundle` and it's not recommended to edit it manually",
+ ""
+];
+
/// Represents the "default" type library that should be used when type
/// checking the code in the module graph. Note that a user provided config
/// of `"lib"` would override this value.
@@ -503,6 +510,7 @@ impl From<BundleType> for swc::bundler::ModuleType {
pub(crate) struct BundleOptions {
pub bundle_type: BundleType,
pub ts_config: TsConfig,
+ pub emit_ignore_directives: bool,
}
/// A module loader for swc which does the appropriate retrieval and transpiling
@@ -630,12 +638,21 @@ pub(crate) fn bundle(
let mut srcmap = Vec::new();
{
let cfg = swc::codegen::Config { minify: false };
- let wr = Box::new(swc::codegen::text_writer::JsWriter::new(
+ let mut wr = Box::new(swc::codegen::text_writer::JsWriter::new(
cm.clone(),
"\n",
&mut buf,
Some(&mut srcmap),
));
+
+ if options.emit_ignore_directives {
+ // write leading comments in bundled file
+ use swc::codegen::text_writer::WriteJs;
+ use swc::common::source_map::DUMMY_SP;
+ let cmt = IGNORE_DIRECTIVES.join("\n") + "\n";
+ wr.write_comment(DUMMY_SP, &cmt)?;
+ }
+
let mut emitter = swc::codegen::Emitter {
cfg,
cm: cm.clone(),
diff --git a/cli/main.rs b/cli/main.rs
index d8e4c4a70..d1fda4727 100644
--- a/cli/main.rs
+++ b/cli/main.rs
@@ -732,6 +732,7 @@ fn bundle_module_graph(
emit::BundleOptions {
bundle_type: emit::BundleType::Module,
ts_config,
+ emit_ignore_directives: true,
},
)
}
diff --git a/cli/ops/runtime_compiler.rs b/cli/ops/runtime_compiler.rs
index faa427ae7..50268b7eb 100644
--- a/cli/ops/runtime_compiler.rs
+++ b/cli/ops/runtime_compiler.rs
@@ -285,6 +285,7 @@ async fn op_emit(
emit::BundleOptions {
bundle_type: bundle.into(),
ts_config,
+ emit_ignore_directives: true,
},
)?;
let mut files = HashMap::new();
diff --git a/cli/tests/integration/bundle_tests.rs b/cli/tests/integration/bundle_tests.rs
index 4ef48b7e3..36ea1f8dd 100644
--- a/cli/tests/integration/bundle_tests.rs
+++ b/cli/tests/integration/bundle_tests.rs
@@ -442,3 +442,8 @@ itest!(bundle_export_specifier_with_alias {
args: "bundle bundle/file_tests-fixture16.ts",
output: "bundle/fixture16.out",
});
+
+itest!(bundle_ignore_directives {
+ args: "bundle subdir/mod1.ts",
+ output: "bundle_ignore_directives.test.out",
+});
diff --git a/cli/tests/integration/compile_tests.rs b/cli/tests/integration/compile_tests.rs
index bff03013f..1a81784af 100644
--- a/cli/tests/integration/compile_tests.rs
+++ b/cli/tests/integration/compile_tests.rs
@@ -130,7 +130,7 @@ fn standalone_error() {
.unwrap();
assert!(!output.status.success());
assert_eq!(output.stdout, b"");
- let expected_stderr = "error: Error: boom!\n at boom (file://$deno$/bundle.js:2:11)\n at foo (file://$deno$/bundle.js:5:5)\n at file://$deno$/bundle.js:7:1\n";
+ let expected_stderr = "error: Error: boom!\n at boom (file://$deno$/bundle.js:6:11)\n at foo (file://$deno$/bundle.js:9:5)\n at file://$deno$/bundle.js:11:1\n";
let stderr = String::from_utf8(output.stderr).unwrap();
assert_eq!(stderr, expected_stderr);
}
diff --git a/cli/tests/testdata/bundle_ignore_directives.test.out b/cli/tests/testdata/bundle_ignore_directives.test.out
new file mode 100644
index 000000000..b69c2632c
--- /dev/null
+++ b/cli/tests/testdata/bundle_ignore_directives.test.out
@@ -0,0 +1,6 @@
+[WILDCARD]
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+// This code was bundled using `deno bundle` and it's not recommended to edit it manually
+
+[WILDCARD]
diff --git a/cli/tests/testdata/compiler_api_test.ts b/cli/tests/testdata/compiler_api_test.ts
index 30fc19d5f..41378f941 100644
--- a/cli/tests/testdata/compiler_api_test.ts
+++ b/cli/tests/testdata/compiler_api_test.ts
@@ -418,10 +418,21 @@ Deno.test({
"/b.ts": `export const b = "b";`,
},
});
+ const ignoreDirecives = [
+ "// deno-fmt-ignore-file",
+ "// deno-lint-ignore-file",
+ "// This code was bundled using `deno bundle` and it's not recommended to edit it manually",
+ "",
+ "",
+ ].join("\n");
assert(diagnostics);
assertEquals(diagnostics.length, 0);
assertEquals(Object.keys(files).length, 2);
- assert(files["deno:///bundle.js"].startsWith("(function() {\n"));
+ assert(
+ files["deno:///bundle.js"].startsWith(
+ ignoreDirecives + "(function() {\n",
+ ),
+ );
assert(files["deno:///bundle.js"].endsWith("})();\n"));
assert(files["deno:///bundle.js.map"]);
},