From daaa8b5c4a86cf39ab34bc46b239719fab8b9916 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Tue, 2 Aug 2022 14:16:30 +0800 Subject: [PATCH] fix(es/lints): Ignore TS module declare blocks (#5363) --- ...ortNamespaceWithDefault_es2015.1.normal.js | 20 ------------------ ...tNamespaceWithDefault_es2015.2.minified.js | 21 ------------------- ...ExportNamespaceWithDefault_es5.1.normal.js | 20 ------------------ ...portNamespaceWithDefault_es5.2.minified.js | 21 ------------------- .../tsxPreserveEmit1_es2015.1.normal.js | 16 -------------- .../tsxPreserveEmit1_es2015.2.minified.js | 17 --------------- .../tsxPreserveEmit1_es5.1.normal.js | 16 -------------- .../tsxPreserveEmit1_es5.2.minified.js | 17 --------------- .../src/rules/duplicate_exports.rs | 6 ++++++ 9 files changed, 6 insertions(+), 148 deletions(-) diff --git a/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es2015.1.normal.js b/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es2015.1.normal.js index 57b07db168e2..5d14cc6b0779 100644 --- a/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es2015.1.normal.js @@ -6,23 +6,3 @@ import a from "a"; a(); // @filename: external.d.ts -//! -//! x the name `default` is exported multiple times -//! ,-[6:9] -//! 6 | export { _a as default }; -//! : ^^^|^^^ -//! : `-- previous exported here -//! 7 | } -//! 8 | export default a; -//! : ^^^^^^^^|^^^^^^^^ -//! : `-- exported more than once -//! `---- -//! -//!Error: -//! > Exported identifiers must be unique -//! -//! x An export assignment cannot be used in a module with other exported elements. -//! ,---- -//! 13 | export = a; -//! : ^^^^^^^^^^^ -//! `---- diff --git a/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es2015.2.minified.js b/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es2015.2.minified.js index 654e2b48b88a..72798234eb26 100644 --- a/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es2015.2.minified.js @@ -1,23 +1,2 @@ import a from "a"; a(); -// @filename: external.d.ts -//! -//! x the name `default` is exported multiple times -//! ,-[6:9] -//! 6 | export { _a as default }; -//! : ^^^|^^^ -//! : `-- previous exported here -//! 7 | } -//! 8 | export default a; -//! : ^^^^^^^^|^^^^^^^^ -//! : `-- exported more than once -//! `---- -//! -//!Error: -//! > Exported identifiers must be unique -//! -//! x An export assignment cannot be used in a module with other exported elements. -//! ,---- -//! 13 | export = a; -//! : ^^^^^^^^^^^ -//! `---- diff --git a/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es5.1.normal.js b/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es5.1.normal.js index 57b07db168e2..5d14cc6b0779 100644 --- a/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es5.1.normal.js @@ -6,23 +6,3 @@ import a from "a"; a(); // @filename: external.d.ts -//! -//! x the name `default` is exported multiple times -//! ,-[6:9] -//! 6 | export { _a as default }; -//! : ^^^|^^^ -//! : `-- previous exported here -//! 7 | } -//! 8 | export default a; -//! : ^^^^^^^^|^^^^^^^^ -//! : `-- exported more than once -//! `---- -//! -//!Error: -//! > Exported identifiers must be unique -//! -//! x An export assignment cannot be used in a module with other exported elements. -//! ,---- -//! 13 | export = a; -//! : ^^^^^^^^^^^ -//! `---- diff --git a/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es5.2.minified.js b/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es5.2.minified.js index 654e2b48b88a..72798234eb26 100644 --- a/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/exportAssignmentOfExportNamespaceWithDefault_es5.2.minified.js @@ -1,23 +1,2 @@ import a from "a"; a(); -// @filename: external.d.ts -//! -//! x the name `default` is exported multiple times -//! ,-[6:9] -//! 6 | export { _a as default }; -//! : ^^^|^^^ -//! : `-- previous exported here -//! 7 | } -//! 8 | export default a; -//! : ^^^^^^^^|^^^^^^^^ -//! : `-- exported more than once -//! `---- -//! -//!Error: -//! > Exported identifiers must be unique -//! -//! x An export assignment cannot be used in a module with other exported elements. -//! ,---- -//! 13 | export = a; -//! : ^^^^^^^^^^^ -//! `---- diff --git a/crates/swc/tests/tsc-references/tsxPreserveEmit1_es2015.1.normal.js b/crates/swc/tests/tsc-references/tsxPreserveEmit1_es2015.1.normal.js index 4641eb3602f8..79d739a1adb5 100644 --- a/crates/swc/tests/tsc-references/tsxPreserveEmit1_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/tsxPreserveEmit1_es2015.1.normal.js @@ -2,22 +2,6 @@ //@jsx: preserve //@target: ES5 //@Filename: react.d.ts -//! -//! x multiple `export =` found -//! ,-[4:5] -//! 4 | export = x; -//! : ^^^^^|^^^^^ -//! : `-- previous `export =` declared here -//! 5 | } -//! 6 | -//! 7 | declare module ReactRouter { -//! 8 | var Route: any; -//! 9 | interface Thing { } -//! 10 | } -//! 11 | declare module 'react-router' { -//! 12 | export = ReactRouter; -//! : ^^^^^^^^^^^^^^^^^^^^^ -//! `---- //@Filename: test.tsx // Should emit 'react-router' in the AMD dependency list const React = require('react'); diff --git a/crates/swc/tests/tsc-references/tsxPreserveEmit1_es2015.2.minified.js b/crates/swc/tests/tsc-references/tsxPreserveEmit1_es2015.2.minified.js index 4d2d550fb8e8..b70b42ee25ed 100644 --- a/crates/swc/tests/tsc-references/tsxPreserveEmit1_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/tsxPreserveEmit1_es2015.2.minified.js @@ -1,20 +1,3 @@ -//@Filename: react.d.ts -//! -//! x multiple `export =` found -//! ,-[4:5] -//! 4 | export = x; -//! : ^^^^^|^^^^^ -//! : `-- previous `export =` declared here -//! 5 | } -//! 6 | -//! 7 | declare module ReactRouter { -//! 8 | var Route: any; -//! 9 | interface Thing { } -//! 10 | } -//! 11 | declare module 'react-router' { -//! 12 | export = ReactRouter; -//! : ^^^^^^^^^^^^^^^^^^^^^ -//! `---- var M; require('react'); let ReactRouter = require('react-router'); diff --git a/crates/swc/tests/tsc-references/tsxPreserveEmit1_es5.1.normal.js b/crates/swc/tests/tsc-references/tsxPreserveEmit1_es5.1.normal.js index e18a57e7e505..f9b5d970d36c 100644 --- a/crates/swc/tests/tsc-references/tsxPreserveEmit1_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/tsxPreserveEmit1_es5.1.normal.js @@ -2,22 +2,6 @@ //@jsx: preserve //@target: ES5 //@Filename: react.d.ts -//! -//! x multiple `export =` found -//! ,-[4:5] -//! 4 | export = x; -//! : ^^^^^|^^^^^ -//! : `-- previous `export =` declared here -//! 5 | } -//! 6 | -//! 7 | declare module ReactRouter { -//! 8 | var Route: any; -//! 9 | interface Thing { } -//! 10 | } -//! 11 | declare module 'react-router' { -//! 12 | export = ReactRouter; -//! : ^^^^^^^^^^^^^^^^^^^^^ -//! `---- //@Filename: test.tsx // Should emit 'react-router' in the AMD dependency list var React = require("react"); diff --git a/crates/swc/tests/tsc-references/tsxPreserveEmit1_es5.2.minified.js b/crates/swc/tests/tsc-references/tsxPreserveEmit1_es5.2.minified.js index c39fba23939a..760789b17afc 100644 --- a/crates/swc/tests/tsc-references/tsxPreserveEmit1_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/tsxPreserveEmit1_es5.2.minified.js @@ -1,20 +1,3 @@ -//@Filename: react.d.ts -//! -//! x multiple `export =` found -//! ,-[4:5] -//! 4 | export = x; -//! : ^^^^^|^^^^^ -//! : `-- previous `export =` declared here -//! 5 | } -//! 6 | -//! 7 | declare module ReactRouter { -//! 8 | var Route: any; -//! 9 | interface Thing { } -//! 10 | } -//! 11 | declare module 'react-router' { -//! 12 | export = ReactRouter; -//! : ^^^^^^^^^^^^^^^^^^^^^ -//! `---- var M; require("react"), require("react-router").Route, function(M) { var X1; diff --git a/crates/swc_ecma_lints/src/rules/duplicate_exports.rs b/crates/swc_ecma_lints/src/rules/duplicate_exports.rs index 8e0e70d2dc0b..05398e973489 100644 --- a/crates/swc_ecma_lints/src/rules/duplicate_exports.rs +++ b/crates/swc_ecma_lints/src/rules/duplicate_exports.rs @@ -80,6 +80,12 @@ impl DuplicateExports { impl Visit for DuplicateExports { noop_visit_type!(); + fn visit_ts_module_decl(&mut self, d: &TsModuleDecl) { + if !d.declare { + d.visit_children_with(self); + } + } + fn visit_export_default_decl(&mut self, d: &ExportDefaultDecl) { if matches!( d.decl,