Skip to content

Commit

Permalink
fix(es/module): Fix relativeness check for jsc.paths (#8702)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8701
  • Loading branch information
kdy1 authored Mar 6, 2024
1 parent 90aaaba commit d37125f
Show file tree
Hide file tree
Showing 27 changed files with 456 additions and 31 deletions.
15 changes: 15 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8701/1/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "http://json.schemastore.org/swcrc",
"jsc": {
"baseUrl": ".",
"paths": {
"@app/*": [
"./src/*"
]
},
"parser": {
"syntax": "typescript",
"decorators": true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';

describe('AppController', () => {
let app: TestingModule;

beforeAll(async () => {
app = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
});

describe('getHello', () => {
it('should return "Hello World!"', () => {
const appController = app.get(AppController);
expect(appController.getHello()).toBe('Hello World!');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
getHello(): string {
return this.appService.getHello();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { AppController } from '@app/app.controller';
import { AppService } from '@app/app.service';

@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}
8 changes: 8 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8701/1/input/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import { Test } from "@nestjs/testing";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";
describe("AppController", function() {
var app;
beforeAll(/*#__PURE__*/ _async_to_generator(function() {
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
return [
4,
Test.createTestingModule({
controllers: [
AppController
],
providers: [
AppService
]
}).compile()
];
case 1:
app = _state.sent();
return [
2
];
}
});
}));
describe("getHello", function() {
it('should return "Hello World!"', function() {
var appController = app.get(AppController);
expect(appController.getHello()).toBe("Hello World!");
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _ts_decorate } from "@swc/helpers/_/_ts_decorate";
import { Controller, Get } from "@nestjs/common";
export var AppController = /*#__PURE__*/ function() {
"use strict";
function AppController(appService) {
_class_call_check(this, AppController);
_define_property(this, "appService", void 0);
this.appService = appService;
}
_create_class(AppController, [
{
key: "getHello",
value: function getHello() {
return this.appService.getHello();
}
}
]);
return AppController;
}();
_ts_decorate([
Get()
], AppController.prototype, "getHello", null);
AppController = _ts_decorate([
Controller()
], AppController);
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _ts_decorate } from "@swc/helpers/_/_ts_decorate";
import { Module } from "@nestjs/common";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";
export var AppModule = function AppModule() {
"use strict";
_class_call_check(this, AppModule);
};
AppModule = _ts_decorate([
Module({
imports: [],
controllers: [
AppController
],
providers: [
AppService
]
})
], AppModule);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
import { _ as _ts_decorate } from "@swc/helpers/_/_ts_decorate";
import { Injectable } from "@nestjs/common";
export var AppService = /*#__PURE__*/ function() {
"use strict";
function AppService() {
_class_call_check(this, AppService);
}
_create_class(AppService, [
{
key: "getHello",
value: function getHello() {
return "Hello World!";
}
}
]);
return AppService;
}();
AppService = _ts_decorate([
Injectable()
], AppService);
34 changes: 34 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8701/1/output/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
function bootstrap() {
return _bootstrap.apply(this, arguments);
}
function _bootstrap() {
_bootstrap = _async_to_generator(function() {
var app;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
return [
4,
NestFactory.create(AppModule)
];
case 1:
app = _state.sent();
return [
4,
app.listen(3000)
];
case 2:
_state.sent();
return [
2
];
}
});
});
return _bootstrap.apply(this, arguments);
}
bootstrap();
59 changes: 56 additions & 3 deletions crates/swc/tests/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use anyhow::Context;
use rayon::prelude::*;
use swc::{
config::{
Config, FileMatcher, JsMinifyOptions, JscConfig, ModuleConfig, Options, SourceMapsConfig,
TransformConfig,
Config, FileMatcher, JsMinifyOptions, JscConfig, ModuleConfig, Options, Paths,
SourceMapsConfig, TransformConfig,
},
try_with_handler, BoolOrDataConfig, Compiler, TransformOutput,
};
Expand Down Expand Up @@ -65,7 +65,16 @@ fn file_with_opt(filename: &str, options: Options) -> Result<NormalizedOutput, S
}

fn str_with_opt(content: &str, options: Options) -> Result<NormalizedOutput, StdErr> {
compile_str(FileName::Anon, content, options).map(|v| v.code.into())
compile_str(
if options.filename.is_empty() {
FileName::Anon
} else {
FileName::Real(PathBuf::from(&options.filename))
},
content,
options,
)
.map(|v| v.code.into())
}

fn compile_str(
Expand Down Expand Up @@ -1155,6 +1164,50 @@ fn issue_8674_1() {
assert_eq!(output.to_string(), "import { foo } from \"./src/foo\";\n");
}

#[test]
fn issue_8701_1() {
static INPUT: &str = "import { AppController } from '@app/app.controller';
import { AppService } from '@app/app.service';
console.log(AppController, AppService);";

let base_url = current_dir()
.unwrap()
.join("tests/projects/issue-8701")
.canonicalize()
.unwrap();

dbg!(&base_url);

let output = str_with_opt(
INPUT,
Options {
filename: "src/app.module.ts".into(),
config: Config {
jsc: JscConfig {
base_url,
paths: {
let mut paths = Paths::default();
paths.insert("@app/*".into(), vec!["./src/*".into()]);
paths
},
..Default::default()
},
..Default::default()
},
..Default::default()
},
)
.unwrap();
println!("{}", output);

assert_eq!(
output.to_string(),
"import { AppController } from \"./app.controller\";\nimport { AppService } from \
\"./app.service\";\nconsole.log(AppController, AppService);\n"
);
}

#[testing::fixture("tests/minify/**/input.js")]
fn minify(input_js: PathBuf) {
let input_dir = input_js.parent().unwrap();
Expand Down
21 changes: 21 additions & 0 deletions crates/swc/tests/projects/issue-8701/src/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';

describe('AppController', () => {
let app: TestingModule;

beforeAll(async () => {
app = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
});

describe('getHello', () => {
it('should return "Hello World!"', () => {
const appController = app.get(AppController);
expect(appController.getHello()).toBe('Hello World!');
});
});
});
12 changes: 12 additions & 0 deletions crates/swc/tests/projects/issue-8701/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
getHello(): string {
return this.appService.getHello();
}
}
9 changes: 9 additions & 0 deletions crates/swc/tests/projects/issue-8701/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { AppController } from '@app/app.controller';
import { AppService } from '@app/app.service';
@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
})
export class AppModule { }
8 changes: 8 additions & 0 deletions crates/swc/tests/projects/issue-8701/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}
8 changes: 8 additions & 0 deletions crates/swc/tests/projects/issue-8701/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
9 changes: 1 addition & 8 deletions crates/swc_ecma_transforms_module/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,7 @@ where
info!("Resolved as {target:?} with slug = {slug:?}");

let mut target = match target {
FileName::Real(v) => {
// @nestjs/common should be preserved as a whole
if v.starts_with(".") || v.starts_with("..") || v.is_absolute() {
v
} else {
return Ok(self.to_specifier(v, slug));
}
}
FileName::Real(v) => v,
FileName::Custom(s) => return Ok(self.to_specifier(s.into(), slug)),
_ => {
unreachable!(
Expand Down
Loading

0 comments on commit d37125f

Please sign in to comment.