From 949a4d9716a0e86318bcf1987e6bbdcad10c62b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sun, 15 Aug 2021 03:34:14 +0900 Subject: [PATCH] fix(es): Fix simple bugs (#2077) swc_ecma_visit: - Ensure that #1967 is wrong. (#1967) swc: - Add a test for #1107. (#1107) node: - Make optional fields optional. (#1947) --- ecmascript/parser/tests/visitor.rs | 40 ++++++++++++++++++++++ node-swc/src/types.ts | 18 +++++----- tests/fixture/issue-1107/1/input/index.js | 11 ++++++ tests/fixture/issue-1107/1/output/index.js | 17 +++++++++ 4 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 ecmascript/parser/tests/visitor.rs create mode 100644 tests/fixture/issue-1107/1/input/index.js create mode 100644 tests/fixture/issue-1107/1/output/index.js diff --git a/ecmascript/parser/tests/visitor.rs b/ecmascript/parser/tests/visitor.rs new file mode 100644 index 00000000000..a05f8ca1e12 --- /dev/null +++ b/ecmascript/parser/tests/visitor.rs @@ -0,0 +1,40 @@ +use swc_common::{input::StringInput, FileName, DUMMY_SP}; +use swc_ecma_ast::*; +use swc_ecma_parser::{lexer::Lexer, Parser}; +use swc_ecma_visit::{All, Node, VisitAll, VisitWith}; + +struct Issue1967; + +impl VisitAll for Issue1967 { + fn visit_expr(&mut self, _: &Expr, _: &dyn Node) { + panic!("intended") + } +} + +#[test] +#[should_panic = "intended"] +fn issue_1967() { + testing::run_test2(false, |cm, _handler| { + let fm = cm.new_source_file(FileName::Anon, "function foo(){ return 'I'; }".to_string()); + let lexer = Lexer::new( + Default::default(), + EsVersion::latest(), + StringInput::from(&*fm), + None, + ); + + let mut parser = Parser::new_from(lexer); + + let m = parser.parse_module().unwrap(); + + m.visit_with( + &Invalid { span: DUMMY_SP }, + &mut All { + visitor: Issue1967 {}, + }, + ); + + Ok(()) + }) + .unwrap(); +} diff --git a/node-swc/src/types.ts b/node-swc/src/types.ts index f1796639a65..de61a3fb2d0 100644 --- a/node-swc/src/types.ts +++ b/node-swc/src/types.ts @@ -620,13 +620,13 @@ export interface ReactConfig { * * Defaults to `React.createElement`. */ - pragma: string; + pragma?: string; /** * Replace the component used when compiling JSX fragments. * * Defaults to `React.Fragment` */ - pragmaFrag: string; + pragmaFrag?: string; /** * Toggles whether or not to throw an error if a XML namespaced tag name is used. For example: * `` @@ -635,7 +635,7 @@ export interface ReactConfig { * JSX does not currently have support for it. * */ - throwIfNamespace: boolean; + throwIfNamespace?: boolean; /** * Toggles plugins that aid in development, such as @swc/plugin-transform-react-jsx-self * and @swc/plugin-transform-react-jsx-source. @@ -643,26 +643,26 @@ export interface ReactConfig { * Defaults to `false`, * */ - development: boolean; + development?: boolean; /** * Use `Object.assign()` instead of `_extends`. Defaults to false. */ - useBuiltins: boolean; + useBuiltins?: boolean; /** * Enable fast refresh feature for React app */ - refresh: boolean; + refresh?: boolean; /** * jsx runtime */ - runtime: 'automatic' | 'classic' + runtime?: 'automatic' | 'classic' /** * Declares the module specifier to be used for importing the `jsx` and `jsxs` factory functions when using `runtime` 'automatic' */ - importSource: string + importSource?: string } /** * - `import { DEBUG } from '@ember/env-flags';` @@ -786,7 +786,7 @@ export interface UmdConfig extends BaseModuleConfig { export interface AmdConfig extends BaseModuleConfig { type: "amd"; - moduleId: string; + moduleId?: string; } export interface Output { diff --git a/tests/fixture/issue-1107/1/input/index.js b/tests/fixture/issue-1107/1/input/index.js new file mode 100644 index 00000000000..2326a37281c --- /dev/null +++ b/tests/fixture/issue-1107/1/input/index.js @@ -0,0 +1,11 @@ +export class Class1 { + constructor({ name = '' }) { + console.log(name) + } +} + +export class Class2 { + constructor({ name = '' }) { + console.log(name) + } +} \ No newline at end of file diff --git a/tests/fixture/issue-1107/1/output/index.js b/tests/fixture/issue-1107/1/output/index.js new file mode 100644 index 00000000000..574d065c618 --- /dev/null +++ b/tests/fixture/issue-1107/1/output/index.js @@ -0,0 +1,17 @@ +function _classCallCheck(instance, Constructor) { + if (!(instance instanceof Constructor)) { + throw new TypeError("Cannot call a class as a function"); + } +} +export var Class1 = function Class1(param) { + "use strict"; + var _name = param.name, name = _name === void 0 ? '' : _name; + _classCallCheck(this, Class1); + console.log(name); +}; +export var Class2 = function Class2(param) { + "use strict"; + var _name = param.name, name = _name === void 0 ? '' : _name; + _classCallCheck(this, Class2); + console.log(name); +};