Skip to content

Commit d44c1ec

Browse files
authored
allow disabling "use strict" emit in SWC when you reeeally want to (#1537)
* allow disabling "use strict" emit in SWC when you reeeally want to * lint-fix
1 parent 2385967 commit d44c1ec

File tree

2 files changed

+92
-56
lines changed

2 files changed

+92
-56
lines changed

package-lock.json

Lines changed: 70 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/transpilers/swc.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
5252
module,
5353
jsxFactory,
5454
jsxFragmentFactory,
55+
strict,
56+
alwaysStrict,
57+
noImplicitUseStrict,
5558
} = compilerOptions;
5659
const nonTsxOptions = createSwcOptions(false);
5760
const tsxOptions = createSwcOptions(true);
@@ -71,21 +74,39 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
7174
}
7275
swcTarget = swcTargets[swcTargetIndex];
7376
const keepClassNames = target! >= /* ts.ScriptTarget.ES2016 */ 3;
77+
// swc only supports these 4x module options
7478
const moduleType =
7579
module === ModuleKind.CommonJS
7680
? 'commonjs'
7781
: module === ModuleKind.AMD
7882
? 'amd'
7983
: module === ModuleKind.UMD
8084
? 'umd'
81-
: undefined;
85+
: 'es6';
86+
// In swc:
87+
// strictMode means `"use strict"` is *always* emitted for non-ES module, *never* for ES module where it is assumed it can be omitted.
88+
// (this assumption is invalid, but that's the way swc behaves)
89+
// tsc is a bit more complex:
90+
// alwaysStrict will force emitting it always unless `import`/`export` syntax is emitted which implies it per the JS spec.
91+
// if not alwaysStrict, will emit implicitly whenever module target is non-ES *and* transformed module syntax is emitted.
92+
// For node, best option is to assume that all scripts are modules (commonjs or esm) and thus should get tsc's implicit strict behavior.
93+
94+
// Always set strictMode, *unless* alwaysStrict is disabled and noImplicitUseStrict is enabled
95+
const strictMode =
96+
// if `alwaysStrict` is disabled, remembering that `strict` defaults `alwaysStrict` to true
97+
(alwaysStrict === false || (alwaysStrict !== true && strict !== true)) &&
98+
// if noImplicitUseStrict is enabled
99+
noImplicitUseStrict === true
100+
? false
101+
: true;
82102
return {
83103
sourceMaps: sourceMap,
84104
// isModule: true,
85105
module: moduleType
86106
? ({
87107
noInterop: !esModuleInterop,
88108
type: moduleType,
109+
strictMode,
89110
} as swcTypes.ModuleConfig)
90111
: undefined,
91112
swcrc: false,

0 commit comments

Comments
 (0)