pintariching / rustle Goto Github PK
View Code? Open in Web Editor NEWSvelte compiler rewritten in Rust
License: MIT License
Svelte compiler rewritten in Rust
License: MIT License
I was going through the output of the svelte compiler vs rustle and I realized it wasn't one to one is this by design or is it something we are gonna have to work on?
Ok so we might have a problem the AST swc_ecma_parser
produces is very different from the EStree compatible AST that acorn
uses. The swc_estree_compat
library provides a way to transform SWC AST to an EStree compatible AST but it makes overall parsing much slower than just using the SWC AST and it also requires the nightly compiler which is unstable. Should we use the nightly compiler and risk some features being removed, build our own AST transformer or just use the SWC AST?
hi there! it is great to see your great work!
BTW, I try rustle around and find out it cannot parse the HTML DOM attribute
<button on:click={increment}>Increment</button>
<span>
<!-- this ok -->
<div bind:class={'q'}>{counter}</div>
<!-- this fail -->
<div class="q">{counter}</div>
</span>
<button on:click={decrement}>Decrement</button>
hope this help you, thank
A few helper functions need to be translated in compiler::parse::state::tag
, mainly
todo!()
.I think we can have a pre release when we get the parsing working. Also perhaps implementing a basic CLI with a few commands that you can point to a .svelte file and it prints out the AST?
What is this line here doing?
const identifier = this.template.slice(this.index, this.index = i);
I can't figure out what the this.index = 1
means. Does it assign i
to this.index
?
The type Node
is used by rustle interfaces for the compile step and also in the code-red
package in handlers.js
The type definition is pulled from @types/estree however from an old version 0.0.50
which defined Node
as
export type Node =
Identifier | Literal | Program | Function | SwitchCase | CatchClause |
VariableDeclarator | Statement | Expression | PrivateIdentifier | Property | PropertyDefinition |
AssignmentProperty | Super | TemplateElement | SpreadElement | Pattern |
ClassBody | Class | MethodDefinition | ModuleDeclaration | ModuleSpecifier;
The new version defines Node
as this
The problem is that in swc_estree_ast
the Node
enum has been commented out and I'm not sure where to pull the right type definitions.
I checked this file https://github.com/sveltejs/svelte/blob/master/src/compiler/utils/mapped_code.ts.
As I see in this file, it imports three libraries.
import { DecodedSourceMap, RawSourceMap, SourceMapLoader } from '@ampproject/remapping/dist/types/types';
import remapping from '@ampproject/remapping';
import { SourceMap } from 'magic-string';
We can use https://github.com/h-a-n-a/magic-string-rs for magic-string
. With @ampproject/remapping we can use https://github.com/getsentry/rust-sourcemap or https://www.npmjs.com/package/@swc-node/sourcemap-support.
I haven't checked it carefully. Feel free to help.
More tests would probably be a good idea. Maybe making a folder structure and having different files like input.svelte
, expected.js
and output.js
in a folder for every test, then use walkdir
to traverse all the folders and compile the files and check if they're the same as the expected.js
. Also adding test for checking if the AST has been parse correctly for a variety of different situations would probably be nice.
I think a simple CLI is the last piece missing before doing a prerelease. I think it should just accept a file or a directory and call the rustle::compile_file_to_js
function. For now I think if anything goes wrong it should just panic.
Does anybody want to work on it or has any other suggestions?
I checked the trim document https://doc.rust-lang.org/std/primitive.str.html#method.trim. It supports well trim_start
and trim_end
, so I don't think we need to implement the trim util anymore.
Awesome project, thanks for taking this on 🎉
I assumed I could create an app.rustle
file with the following contents:
<script>
let test = "test";
</script>
<h1>{test}</h1>
Then run rustle_cli app.rustle
to create a compiled app.js
file, but that throws:
thread 'main' panicked at 'Parse error: expecting ={', /home/jimafisk/.cargo/registry/src/github.com-1ecc6299db9ec823/rustle_lib-0.0.1-alpha/src/compiler/parse/parser.rs:120:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
The latest library release adds support for nested component and passing properties to them. A lot of functionality still has to be tested by expanding the tests.
Feel free to try out the compiler with everything you can throw at it so we can see what still has to be fixed!
Something that combines rust and svelte in some form?
For some ideas, rust could be substituted with something to do with oxidation or perhaps the various types of rust: hematite, magnetite, wüstite
Svelte in English is used to describe a slender - thin appearance
Is there anybody working on it? If not I will do it.
There is a package called code-red that svelte uses for parsing some expressions with the functions in src/compiler/parse/acorn.ts.
I think the code-red
package has to be rewritten in rust to be used in the parsing.
I'm implementing this repo https://github.com/Rich-Harris/locate-character/blob/master/src/index.ts that will support the error utils.
One place to start would be rewriting all the utilities from here: https://github.com/sveltejs/svelte/tree/master/src/compiler/utils
A lot of them are used further up in the parsing and compiling.
In src/compiler/parse/utils/entities.rs
. from_code
method matches duplicate two codes to both Entity::nbsp
and Entity::NonBreakingSpace
. It will cause the second statement to be never reached.
160 => Entity::NonBreakingSpace,
160 => Entity::nbsp,
This will be useful in the future for bug hunters to be able to create simple reproducible examples of unexpected behavior in rustle.
I implemented the error util https://github.com/sveltejs/svelte/blob/master/src/compiler/utils/error.ts
I haven't tested it yet because of the compile error.
I would need some help understanding the decode_character_references
function at src/compiler/parse/utils/html.ts
What do the (match, entity)
return?
For starters, I think getting all the matches in a vector would be a good idea? Something like this in src/compiler/parse/utils/html.rs
pub const boolean_attributes: HashSet<&str> = HashSet::from([
"allowfullscreen",
"allowpaymentrequest",
"async",
"autofocus",
"autoplay",
"checked",
"controls",
"default",
"defer",
"disabled",
"formnovalidate",
"hidden",
"ismap",
"loop",
"multiple",
"muted",
"nomodule",
"novalidate",
"open",
"playsinline",
"readonly",
"required",
"reversed",
"selected",
]);
causes the error
cannot call non-const fn `<HashSet<&str> as From<[&str; 24]>>::from` in constants
calls in constants are limited to constant functions, tuple structs and tuple variants
My suggestion for a fix is to make boolean_attributes a static
If anybody wants to check out the compiler::parse::index::Parser
struct and impl and possibly write some tests if everything works as it should.
Hi, I think we should use git conventional for our commit message and branch for easy maintenance. https://www.conventionalcommits.org/en/v1.0.0/
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.