mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
feat: deprecate import assertions (#25281)
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
parent
d183533404
commit
2afbc1aa39
8 changed files with 61 additions and 88 deletions
64
cli/emit.rs
64
cli/emit.rs
|
@ -112,8 +112,7 @@ impl Emitter {
|
||||||
let parsed_source_cache = self.parsed_source_cache.clone();
|
let parsed_source_cache = self.parsed_source_cache.clone();
|
||||||
let transpile_and_emit_options =
|
let transpile_and_emit_options =
|
||||||
self.transpile_and_emit_options.clone();
|
self.transpile_and_emit_options.clone();
|
||||||
let (should_cache, transpile_result) =
|
let transpile_result = deno_core::unsync::spawn_blocking({
|
||||||
deno_core::unsync::spawn_blocking({
|
|
||||||
let specifier = specifier.clone();
|
let specifier = specifier.clone();
|
||||||
let source = source.clone();
|
let source = source.clone();
|
||||||
move || -> Result<_, AnyError> {
|
move || -> Result<_, AnyError> {
|
||||||
|
@ -133,7 +132,6 @@ impl Emitter {
|
||||||
specifier,
|
specifier,
|
||||||
transpile_result,
|
transpile_result,
|
||||||
source_hash,
|
source_hash,
|
||||||
should_cache,
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -150,8 +148,7 @@ impl Emitter {
|
||||||
match helper.pre_emit_parsed_source(specifier, source) {
|
match helper.pre_emit_parsed_source(specifier, source) {
|
||||||
PreEmitResult::Cached(emitted_text) => Ok(emitted_text),
|
PreEmitResult::Cached(emitted_text) => Ok(emitted_text),
|
||||||
PreEmitResult::NotCached { source_hash } => {
|
PreEmitResult::NotCached { source_hash } => {
|
||||||
let (should_cache, transpile_result) =
|
let transpile_result = EmitParsedSourceHelper::transpile(
|
||||||
EmitParsedSourceHelper::transpile(
|
|
||||||
&self.parsed_source_cache,
|
&self.parsed_source_cache,
|
||||||
specifier,
|
specifier,
|
||||||
source.clone(),
|
source.clone(),
|
||||||
|
@ -163,7 +160,6 @@ impl Emitter {
|
||||||
specifier,
|
specifier,
|
||||||
transpile_result,
|
transpile_result,
|
||||||
source_hash,
|
source_hash,
|
||||||
should_cache,
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -261,16 +257,13 @@ impl<'a> EmitParsedSourceHelper<'a> {
|
||||||
media_type: MediaType,
|
media_type: MediaType,
|
||||||
transpile_options: &deno_ast::TranspileOptions,
|
transpile_options: &deno_ast::TranspileOptions,
|
||||||
emit_options: &deno_ast::EmitOptions,
|
emit_options: &deno_ast::EmitOptions,
|
||||||
) -> Result<(bool, TranspileResult), AnyError> {
|
) -> Result<TranspileResult, AnyError> {
|
||||||
// nothing else needs the parsed source at this point, so remove from
|
// nothing else needs the parsed source at this point, so remove from
|
||||||
// the cache in order to not transpile owned
|
// the cache in order to not transpile owned
|
||||||
let parsed_source = parsed_source_cache
|
let parsed_source = parsed_source_cache
|
||||||
.remove_or_parse_module(specifier, source, media_type)?;
|
.remove_or_parse_module(specifier, source, media_type)?;
|
||||||
let should_cache = !has_import_assertion(&parsed_source);
|
ensure_no_import_assertion(&parsed_source)?;
|
||||||
Ok((
|
Ok(parsed_source.transpile(transpile_options, emit_options)?)
|
||||||
should_cache,
|
|
||||||
parsed_source.transpile(transpile_options, emit_options)?,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn post_emit_parsed_source(
|
pub fn post_emit_parsed_source(
|
||||||
|
@ -278,8 +271,6 @@ impl<'a> EmitParsedSourceHelper<'a> {
|
||||||
specifier: &ModuleSpecifier,
|
specifier: &ModuleSpecifier,
|
||||||
transpile_result: TranspileResult,
|
transpile_result: TranspileResult,
|
||||||
source_hash: u64,
|
source_hash: u64,
|
||||||
// todo(dsherret): remove after Deno 2.0
|
|
||||||
should_cache: bool,
|
|
||||||
) -> ModuleCodeBytes {
|
) -> ModuleCodeBytes {
|
||||||
let transpiled_source = match transpile_result {
|
let transpiled_source = match transpile_result {
|
||||||
TranspileResult::Owned(source) => source,
|
TranspileResult::Owned(source) => source,
|
||||||
|
@ -289,44 +280,47 @@ impl<'a> EmitParsedSourceHelper<'a> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
debug_assert!(transpiled_source.source_map.is_none());
|
debug_assert!(transpiled_source.source_map.is_none());
|
||||||
if should_cache {
|
|
||||||
self.0.emit_cache.set_emit_code(
|
self.0.emit_cache.set_emit_code(
|
||||||
specifier,
|
specifier,
|
||||||
source_hash,
|
source_hash,
|
||||||
&transpiled_source.source,
|
&transpiled_source.source,
|
||||||
);
|
);
|
||||||
}
|
|
||||||
transpiled_source.source.into_boxed_slice().into()
|
transpiled_source.source.into_boxed_slice().into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_import_assertion(parsed_source: &deno_ast::ParsedSource) -> bool {
|
// todo(dsherret): this is a temporary measure until we have swc erroring for this
|
||||||
|
fn ensure_no_import_assertion(
|
||||||
|
parsed_source: &deno_ast::ParsedSource,
|
||||||
|
) -> Result<(), AnyError> {
|
||||||
fn has_import_assertion(text: &str) -> bool {
|
fn has_import_assertion(text: &str) -> bool {
|
||||||
// good enough
|
// good enough
|
||||||
text.contains(" assert ") && !text.contains(" with ")
|
text.contains(" assert ") && !text.contains(" with ")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn warn_import_attribute(
|
fn create_err(
|
||||||
parsed_source: &deno_ast::ParsedSource,
|
parsed_source: &deno_ast::ParsedSource,
|
||||||
range: SourceRange,
|
range: SourceRange,
|
||||||
) {
|
) -> AnyError {
|
||||||
let text_info = parsed_source.text_info_lazy();
|
let text_info = parsed_source.text_info_lazy();
|
||||||
let loc = text_info.line_and_column_display(range.start);
|
let loc = text_info.line_and_column_display(range.start);
|
||||||
deno_runtime::import_assertion_callback(
|
let mut msg = "Import assertions are deprecated. Use `with` keyword, instead of 'assert' keyword.".to_string();
|
||||||
deno_core::ImportAssertionsSupportCustomCallbackArgs {
|
msg.push_str("\n\n");
|
||||||
maybe_specifier: Some(parsed_source.specifier().to_string()),
|
msg.push_str(range.text_fast(text_info));
|
||||||
maybe_line_number: Some(loc.line_number),
|
msg.push_str("\n\n");
|
||||||
column_number: loc.column_number,
|
msg.push_str(&format!(
|
||||||
maybe_source_line: Some(range.text_fast(text_info).to_string()),
|
" at {}:{}:{}\n",
|
||||||
},
|
parsed_source.specifier(),
|
||||||
)
|
loc.line_number,
|
||||||
|
loc.column_number,
|
||||||
|
));
|
||||||
|
deno_core::anyhow::anyhow!("{}", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
let Some(module) = parsed_source.program_ref().as_module() else {
|
let Some(module) = parsed_source.program_ref().as_module() else {
|
||||||
return false;
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut had_import_assertion = false;
|
|
||||||
for item in &module.body {
|
for item in &module.body {
|
||||||
match item {
|
match item {
|
||||||
deno_ast::swc::ast::ModuleItem::ModuleDecl(decl) => match decl {
|
deno_ast::swc::ast::ModuleItem::ModuleDecl(decl) => match decl {
|
||||||
|
@ -334,24 +328,21 @@ fn has_import_assertion(parsed_source: &deno_ast::ParsedSource) -> bool {
|
||||||
if n.with.is_some()
|
if n.with.is_some()
|
||||||
&& has_import_assertion(n.text_fast(parsed_source.text_info_lazy()))
|
&& has_import_assertion(n.text_fast(parsed_source.text_info_lazy()))
|
||||||
{
|
{
|
||||||
had_import_assertion = true;
|
return Err(create_err(parsed_source, n.range()));
|
||||||
warn_import_attribute(parsed_source, n.range());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
deno_ast::swc::ast::ModuleDecl::ExportAll(n) => {
|
deno_ast::swc::ast::ModuleDecl::ExportAll(n) => {
|
||||||
if n.with.is_some()
|
if n.with.is_some()
|
||||||
&& has_import_assertion(n.text_fast(parsed_source.text_info_lazy()))
|
&& has_import_assertion(n.text_fast(parsed_source.text_info_lazy()))
|
||||||
{
|
{
|
||||||
had_import_assertion = true;
|
return Err(create_err(parsed_source, n.range()));
|
||||||
warn_import_attribute(parsed_source, n.range());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
deno_ast::swc::ast::ModuleDecl::ExportNamed(n) => {
|
deno_ast::swc::ast::ModuleDecl::ExportNamed(n) => {
|
||||||
if n.with.is_some()
|
if n.with.is_some()
|
||||||
&& has_import_assertion(n.text_fast(parsed_source.text_info_lazy()))
|
&& has_import_assertion(n.text_fast(parsed_source.text_info_lazy()))
|
||||||
{
|
{
|
||||||
had_import_assertion = true;
|
return Err(create_err(parsed_source, n.range()));
|
||||||
warn_import_attribute(parsed_source, n.range());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
deno_ast::swc::ast::ModuleDecl::ExportDecl(_)
|
deno_ast::swc::ast::ModuleDecl::ExportDecl(_)
|
||||||
|
@ -364,5 +355,6 @@ fn has_import_assertion(parsed_source: &deno_ast::ParsedSource) -> bool {
|
||||||
deno_ast::swc::ast::ModuleItem::Stmt(_) => {}
|
deno_ast::swc::ast::ModuleItem::Stmt(_) => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
had_import_assertion
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
2
tests/specs/cache/json_import/main.ts
vendored
2
tests/specs/cache/json_import/main.ts
vendored
|
@ -1,2 +1,2 @@
|
||||||
import asdf from "./test.json" assert { type: "json" };
|
import asdf from "./test.json" with { type: "json" };
|
||||||
console.log(asdf);
|
console.log(asdf);
|
||||||
|
|
|
@ -3,10 +3,12 @@
|
||||||
"assertion": {
|
"assertion": {
|
||||||
"steps": [{
|
"steps": [{
|
||||||
"args": "run assertion.ts",
|
"args": "run assertion.ts",
|
||||||
|
"exitCode": 1,
|
||||||
"output": "assertion.out"
|
"output": "assertion.out"
|
||||||
}, {
|
}, {
|
||||||
// should output the same because the emit won't be cached
|
// should output the same because the emit won't be cached
|
||||||
"args": "run assertion.ts",
|
"args": "run assertion.ts",
|
||||||
|
"exitCode": 1,
|
||||||
"output": "assertion.out"
|
"output": "assertion.out"
|
||||||
}]
|
}]
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,14 +1,6 @@
|
||||||
⚠️ Import assertions are deprecated. Use `with` keyword, instead of 'assert' keyword.
|
error: Import assertions are deprecated. Use `with` keyword, instead of 'assert' keyword.
|
||||||
|
|
||||||
import test from "./data.json" assert { type: "json" };
|
import test from "./data.json" assert { type: "json" };
|
||||||
|
|
||||||
at file:///[WILDLINE]/assertion.ts:1:1
|
at file:///[WILDLINE]/assertion.ts:1:1
|
||||||
|
|
||||||
{ prop: "data" }
|
|
||||||
⚠️ Import assertions are deprecated. Use `with` keyword, instead of 'assert' keyword.
|
|
||||||
|
|
||||||
console.log((await import("./data.json", {
|
|
||||||
|
|
||||||
at file:///[WILDLINE]/assertion.ts:5:13
|
|
||||||
|
|
||||||
{ prop: "data" }
|
|
||||||
|
|
|
@ -1,7 +1 @@
|
||||||
⚠️ Import assertions are deprecated. Use `with` keyword, instead of 'assert' keyword.
|
|
||||||
|
|
||||||
export { default } from "./data.json" assert { type: "json" };
|
|
||||||
|
|
||||||
at file:///[WILDLINE]
|
|
||||||
|
|
||||||
{ a: "b", c: { d: 10 } }
|
{ a: "b", c: { d: 10 } }
|
||||||
|
|
|
@ -1,8 +1,2 @@
|
||||||
⚠️ Import assertions are deprecated. Use `with` keyword, instead of 'assert' keyword.
|
|
||||||
|
|
||||||
import data2 from "./data.json" assert { type: "json" };
|
|
||||||
|
|
||||||
at file:///[WILDLINE]
|
|
||||||
|
|
||||||
{ a: "b", c: { d: 10 } }
|
{ a: "b", c: { d: 10 } }
|
||||||
{ a: "b", c: { d: 10 } }
|
{ a: "b", c: { d: 10 } }
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import data1 from "./data.json" with { type: "json" };
|
import data1 from "./data.json" with { type: "json" };
|
||||||
// deno-lint-ignore no-import-assertions
|
import data2 from "./data.json" with { type: "json" };
|
||||||
import data2 from "./data.json" assert { type: "json" };
|
|
||||||
|
|
||||||
console.log(data1);
|
console.log(data1);
|
||||||
console.log(data2);
|
console.log(data2);
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
export { default } from "./data.json" assert { type: "json" };
|
export { default } from "./data.json" with { type: "json" };
|
||||||
|
|
Loading…
Reference in a new issue