Skip to content

feat(es/codegen): Introduce SpanRewriter #10349

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

Draft
wants to merge 104 commits into
base: main
Choose a base branch
from
Draft

feat(es/codegen): Introduce SpanRewriter #10349

wants to merge 104 commits into from

Conversation

kdy1
Copy link
Member

@kdy1 kdy1 commented Apr 10, 2025

Description:

This PR is a retry of #10261 with generics.

image

The idea is that, we can conditionally execute code using a macro.

#[node_impl]
impl MacroNode for Number {
    fn emit(&mut self, emitter: &mut Macro) -> Result {
        let lo = only_new!(emitter.wr.get_pos());

        emitter.emit_num_lit_internal(self, false)?;

        let hi = only_new!(emitter.wr.get_pos());

        Ok(only_new!(Number {
            span: Span::new(lo, hi),
            ..self.clone()
        }))
    }
}

expands to

impl crate::Node for Number {
    fn emit_with<W, S>(&self, emitter: &mut crate::Emitter<'_, W, S>) -> crate::Result
    where
        W: crate::text_writer::WriteJs,
        S: swc_common::SourceMapper + swc_ecma_ast::SourceMapperExt,
    {
        {
            let lo = ();
            emitter.emit_num_lit_internal(self, false)?;
            let hi = ();
            Ok(())
        }
    }

    fn with_new_span<W, S>(&self, e: &mut crate::NodeEmitter<'_, W, S>) -> crate::Result<Self>
    where
        W: crate::text_writer::SpannedWriteJs,
        S: swc_common::SourceMapper + swc_ecma_ast::SourceMapperExt,
    {
            let lo = emitter.wr.get_pos();
            emitter.emit_num_lit_internal(self, false)?;
            let hi = emitter.wr.get_pos();
            Ok(Number {
	            span: Span::new(lo, hi),
            	..self.clone()
        	})
    }
}

Related issue:

@kdy1 kdy1 added this to the Planned milestone Apr 10, 2025
@kdy1 kdy1 self-assigned this Apr 10, 2025
Copy link

changeset-bot bot commented Apr 10, 2025

⚠️ No Changeset found

Latest commit: fb1d187

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

codspeed-hq bot commented Apr 10, 2025

CodSpeed Performance Report

Merging #10349 will degrade performances by 2.4%

Comparing kdy1/codegen-generic (fb1d187) with main (a0e89f0)

Summary

⚡ 1 improvements
❌ 1 regressions
✅ 150 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
deserialization of serde 2.4 µs 2.4 µs -2.4%
serialization of serde 2.8 µs 2.7 µs +3.28%

@kdy1 kdy1 force-pushed the kdy1/codegen-generic branch from f1ba5e3 to 1d43f81 Compare April 14, 2025 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

rspack: ES codegen that modifies span to emulate codegen + reparse
1 participant