Skip to content

Commit

Permalink
feat(node/swc): Support sourceFileName (#1976)
Browse files Browse the repository at this point in the history
swc:
 - Support `sourceFileName`.
  • Loading branch information
kdy1 authored Aug 2, 2021
1 parent 6285f20 commit e916b35
Show file tree
Hide file tree
Showing 21 changed files with 4,607 additions and 21 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ jobs:
run: |
# This does not have any effect on windows.
npm config set prefix $HOME/npm
npm i browserslist regenerator-runtime sourcemap-validator progress
npm i
yarn
npm i -g jest
echo $HOME
echo $HOME/npm/bin >> $GITHUB_PATH
Expand Down
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ package-lock.json

pkg/

yarn.lock
.idea/


# Generated while testing
*.tmp.js
*.tmp.js

# Used to see output of babel.
/lab
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc"
repository = "https://github.com/swc-project/swc.git"
version = "0.33.0"
version = "0.34.0"

[lib]
name = "swc"
Expand Down
1 change: 1 addition & 0 deletions benches/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ fn bench_codegen(b: &mut Bencher, _target: JscTarget) {
c.print(
&module,
None,
None,
JscTarget::Es2020,
SourceMapsConfig::Bool(false),
None,
Expand Down
2 changes: 2 additions & 0 deletions ecmascript/parser/tests/typescript/next/0001/input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import React from "react";
const comp = () => <amp-something className="something" />;
164 changes: 164 additions & 0 deletions ecmascript/parser/tests/typescript/next/0001/input.tsx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 86,
"ctxt": 0
},
"body": [
{
"type": "ImportDeclaration",
"span": {
"start": 0,
"end": 26,
"ctxt": 0
},
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"span": {
"start": 7,
"end": 12,
"ctxt": 0
},
"local": {
"type": "Identifier",
"span": {
"start": 7,
"end": 12,
"ctxt": 0
},
"value": "React",
"optional": false
}
}
],
"source": {
"type": "StringLiteral",
"span": {
"start": 18,
"end": 25,
"ctxt": 0
},
"value": "react",
"hasEscape": false,
"kind": {
"type": "normal",
"containsQuote": true
}
},
"typeOnly": false,
"asserts": null
},
{
"type": "VariableDeclaration",
"span": {
"start": 27,
"end": 86,
"ctxt": 0
},
"kind": "const",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 33,
"end": 85,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 33,
"end": 37,
"ctxt": 0
},
"value": "comp",
"optional": false,
"typeAnnotation": null
},
"init": {
"type": "ArrowFunctionExpression",
"span": {
"start": 40,
"end": 85,
"ctxt": 0
},
"params": [],
"body": {
"type": "JSXElement",
"span": {
"start": 46,
"end": 85,
"ctxt": 0
},
"opening": {
"type": "JSXOpeningElement",
"name": {
"type": "Identifier",
"span": {
"start": 47,
"end": 60,
"ctxt": 0
},
"value": "amp-something",
"optional": false
},
"span": {
"start": 46,
"end": 85,
"ctxt": 0
},
"attributes": [
{
"type": "JSXAttribute",
"span": {
"start": 61,
"end": 82,
"ctxt": 0
},
"name": {
"type": "Identifier",
"span": {
"start": 61,
"end": 70,
"ctxt": 0
},
"value": "className",
"optional": false
},
"value": {
"type": "StringLiteral",
"span": {
"start": 71,
"end": 82,
"ctxt": 0
},
"value": "something",
"hasEscape": false,
"kind": {
"type": "normal",
"containsQuote": true
}
}
}
],
"selfClosing": true,
"typeArguments": null
},
"children": [],
"closing": null
},
"async": false,
"generator": false,
"typeParameters": null,
"returnType": null
},
"definite": false
}
]
}
],
"interpreter": null
}
6 changes: 6 additions & 0 deletions ecmascript/transforms/base/src/fixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ use swc_common::{comments::Comments, Span, Spanned};
use swc_ecma_ast::*;
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};

/// Fixes ast nodes before printing so semantics are preserved.
///
/// You don't have to bother to create appropriate parenthesis.
/// The pass will insert parenthesis as needed. In other words, it's
/// okay to store `a * (b + c)` as `Bin { a * Bin { b + c } }`.
pub fn fixer<'a>(comments: Option<&'a dyn Comments>) -> impl 'a + Fold + VisitMut {
as_folder(Fixer {
comments,
Expand Down
6 changes: 5 additions & 1 deletion ecmascript/transforms/base/src/hygiene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,15 @@ impl<'a> Hygiene<'a> {
}
}

/// Creates a `hygiene` pass with default value of [Config].
/// See [hygiene_with_config] for doc. Creates a `hygiene` pass with default
/// value of [Config].
pub fn hygiene() -> impl Fold + 'static {
hygiene_with_config(Default::default())
}

/// The pass actually modifies the identifiers in the way that different
/// identifier (with respect to span hygiene) becomes different identifier.
/// (e.g. `a1` for `a#6`, `a2` for `a#23`)
pub fn hygiene_with_config(config: Config) -> impl 'static + Fold + VisitMut {
chain!(
as_folder(Hygiene {
Expand Down
40 changes: 40 additions & 0 deletions ecmascript/transforms/testing/tests/visited.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use swc_ecma_ast::*;
use swc_ecma_parser::{EsConfig, Syntax};
use swc_ecma_transforms_testing::test_transform;
use swc_ecma_visit::Fold;

struct Panicking;

impl Fold for Panicking {
fn fold_jsx_opening_element(&mut self, node: JSXOpeningElement) -> JSXOpeningElement {
let JSXOpeningElement { name, .. } = &node;
println!("HMM");

if let JSXElementName::Ident(Ident { sym, .. }) = name {
panic!("visited: {}", sym)
}

JSXOpeningElement { ..node }
}
}

#[test]
#[should_panic = "visited"]
fn ensure_visited() {
test_transform(
Syntax::Es(EsConfig {
jsx: true,
..Default::default()
}),
|_| Panicking,
"
import React from 'react';
const comp = () => <amp-something className='something' />;
",
"
import React from 'react';
const comp = () => <amp-something className='something' />;
",
false,
);
}
1 change: 1 addition & 0 deletions node/binding/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ impl Task for BundleTask {
let output = self.swc.print(
&m,
None,
None,
codegen_target,
SourceMapsConfig::Bool(true),
None,
Expand Down
2 changes: 2 additions & 0 deletions node/binding/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl Task for PrintTask {
self.c
.print(
&self.program,
None,
self.options.output_path.clone(),
self.options.config.jsc.target.unwrap_or(JscTarget::Es2020),
self.options
Expand Down Expand Up @@ -82,6 +83,7 @@ pub fn print_sync(cx: CallContext) -> napi::Result<JsObject> {
let result = {
c.print(
&program,
None,
options.output_path,
codegen_target,
options
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/core",
"version": "1.2.70",
"version": "1.2.71",
"description": "Super-fast alternative for babel",
"homepage": "https://swc.rs",
"main": "./index.js",
Expand Down Expand Up @@ -69,7 +69,6 @@
"@babel/types": "^7.14.0",
"@napi-rs/cli": "^1.0.4",
"@swc/helpers": "^0.2.10",
"@types/browserslist": "^4.15.0",
"@types/jest": "^26.0.23",
"@types/node": "^14.14.41",
"axios": "^0.21.1",
Expand All @@ -80,9 +79,10 @@
"jest": "^27.0.1",
"lodash": "^4.17.21",
"progress": "^2.0.3",
"regenerator-runtime": "^0.13.9",
"source-map": "^0.7.3",
"source-map-support": "^0.5.19",
"sourcemap-validator": "^1.1.1",
"sourcemap-validator": "^2.1.0",
"typescript": "^4.2.0-beta"
},
"funding": {
Expand Down
1 change: 1 addition & 0 deletions spack/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ fn reference_tests(tests: &mut Vec<TestDescAndFn>, errors: bool) -> Result<(), i
.print(
&bundled.module.fold_with(&mut fixer(None)),
None,
None,
JscTarget::Es2020,
SourceMapsConfig::Bool(false),
None,
Expand Down
4 changes: 4 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ impl Options {
cm: &Arc<SourceMap>,
base: &FileName,
output_path: Option<&Path>,
source_file_name: Option<String>,
handler: &Handler,
is_module: bool,
config: Option<Config>,
Expand Down Expand Up @@ -308,6 +309,7 @@ impl Options {
.unwrap_or(SourceMapsConfig::Bool(false)),
input_source_map: self.config.input_source_map.clone(),
output_path: output_path.map(|v| v.to_path_buf()),
source_file_name,
}
}
}
Expand Down Expand Up @@ -635,6 +637,8 @@ pub struct BuiltConfig<P: swc_ecma_visit::Fold> {
pub input_source_map: InputSourceMap,
pub is_module: bool,
pub output_path: Option<PathBuf>,

pub source_file_name: Option<String>,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
Expand Down
Loading

0 comments on commit e916b35

Please sign in to comment.