From a4b56e7b971fa81c56a59b465f90c8016f01320d Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Tue, 11 Oct 2022 09:54:56 +0200 Subject: Add support for proper positional info in swc tree * Fix some positional info in SWC error messages * Add positional info in `to_document` on duplicate layouts * Add support for `path` on `Program` (`to_swc`, `to_document`, `jsx_rewrite`), for the path of a file on disk * Add support for `development` to `jsx-rewrite`, which when defined will embed info on where tags were written into the runtime code when they are not passed * Refactor to move some utilities to `micromark_swc_utils.rs`, `swc_utils.rs` --- tests/test_utils/to_document.rs | 65 +++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 16 deletions(-) (limited to 'tests/test_utils/to_document.rs') diff --git a/tests/test_utils/to_document.rs b/tests/test_utils/to_document.rs index ded028a..938df1b 100644 --- a/tests/test_utils/to_document.rs +++ b/tests/test_utils/to_document.rs @@ -1,6 +1,12 @@ -extern crate swc_common; extern crate swc_ecma_ast; -use crate::test_utils::to_swc::Program; +use crate::test_utils::{ + micromark_swc_utils::{bytepos_to_point, prefix_error_with_point, span_to_position}, + to_swc::Program, +}; +use micromark::{ + unist::{Point, Position}, + Location, +}; /// JSX runtimes. #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] @@ -60,7 +66,11 @@ impl Default for Options { } #[allow(dead_code)] -pub fn to_document(mut program: Program, options: &Options) -> Result { +pub fn to_document( + mut program: Program, + options: &Options, + location: Option<&Location>, +) -> Result { // New body children. let mut replacements = vec![]; @@ -158,8 +168,8 @@ pub fn to_document(mut program: Program, options: &Options) -> Result Result { - // To do: use positional info. if layout { - return Err("Cannot specify multiple layouts".into()); + return Err(create_double_layout_message( + bytepos_to_point(&decl.span.lo, location).as_ref(), + layout_position.as_ref(), + )); } - // To do: set positional info. layout = true; + layout_position = span_to_position(&decl.span, location); match decl.decl { swc_ecma_ast::DefaultDecl::Class(cls) => { replacements.push(create_layout_decl(swc_ecma_ast::Expr::Class(cls))) @@ -190,22 +202,26 @@ pub fn to_document(mut program: Program, options: &Options) -> Result { return Err( - "Cannot use TypeScript interface declarations as default export in MDX files. The default export is reserved for a layout, which must be a component" - .into(), - ) + prefix_error_with_point( + "Cannot use TypeScript interface declarations as default export in MDX files. The default export is reserved for a layout, which must be a component".into(), + bytepos_to_point(&decl.span.lo, location).as_ref() + ) + ); } } } swc_ecma_ast::ModuleItem::ModuleDecl(swc_ecma_ast::ModuleDecl::ExportDefaultExpr( expr, )) => { - // To do: use positional info. if layout { - return Err("Cannot specify multiple layouts".into()); + return Err(create_double_layout_message( + bytepos_to_point(&expr.span.lo, location).as_ref(), + layout_position.as_ref(), + )); } - // To do: set positional info. layout = true; + layout_position = span_to_position(&expr.span, location); replacements.push(create_layout_decl(*expr.expr)); } // ```js @@ -239,12 +255,14 @@ pub fn to_document(mut program: Program, options: &Options) -> Result swc_ecma_ast::ModuleItem { }, )))) } + +/// Create an error message about multiple layouts. +fn create_double_layout_message(at: Option<&Point>, previous: Option<&Position>) -> String { + prefix_error_with_point( + format!( + "Cannot specify multiple layouts{}", + if let Some(previous) = previous { + format!(" (previous: {:?})", previous) + } else { + "".into() + } + ), + at, + ) +} -- cgit