or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

async-generators.mdclass-system.mddecorators.mddestructuring.mdindex.mdmodule-interop.mdprivate-fields.mdtype-system.mdutilities.md

index.mddocs/

0

# SWC Helpers

1

2

SWC Helpers provides runtime helper functions for JavaScript and TypeScript transformations performed by the SWC compiler. It contains 100+ individual helper modules that implement various JavaScript language features and transformations, enabling SWC to compile modern JavaScript and TypeScript features down to older JavaScript versions while maintaining correct runtime behavior.

3

4

## Package Information

5

6

- **Package Name**: @swc/helpers

7

- **Package Type**: npm

8

- **Language**: JavaScript

9

- **Installation**: `npm install @swc/helpers`

10

11

## Core Imports

12

13

```javascript

14

// Import specific helpers

15

import { _class_call_check, _async_to_generator } from "@swc/helpers";

16

```

17

18

For individual helper imports:

19

20

```javascript

21

// Import individual helpers

22

import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";

23

import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";

24

```

25

26

CommonJS:

27

28

```javascript

29

const { _class_call_check, _async_to_generator } = require("@swc/helpers");

30

```

31

32

## Basic Usage

33

34

SWC Helpers are typically auto-injected by the SWC compiler during transformations, but can be used directly:

35

36

```javascript

37

import { _class_call_check, _create_class } from "@swc/helpers";

38

39

// Ensure class constructor called with new

40

function MyClass() {

41

_class_call_check(this, MyClass);

42

}

43

44

// Define class methods

45

_create_class(MyClass, [

46

{

47

key: "myMethod",

48

value: function myMethod() {

49

return "Hello World";

50

}

51

}

52

]);

53

```

54

55

## Architecture

56

57

SWC Helpers is organized as a collection of modular helper functions:

58

59

- **Individual Modules**: Each helper is a separate module file for optimal tree-shaking

60

- **Dual Export**: Supports both ESM and CommonJS through conditional exports

61

- **Runtime Support**: Provides the runtime behavior for compiled JavaScript features

62

- **Auto-Injection**: Automatically included by SWC when transformations require runtime support

63

64

## Capabilities

65

66

### Class System Helpers

67

68

Runtime support for modern class syntax including construction, inheritance, private fields, and decorators. Essential for compiling ES6+ classes to older JavaScript versions.

69

70

```javascript { .api }

71

function _class_call_check(instance, Constructor): void;

72

function _create_class(Constructor, protoProps, staticProps): Function;

73

function _inherits(subClass, superClass): void;

74

```

75

76

[Class System](./class-system.md)

77

78

### Async and Generator Helpers

79

80

Implementation of async/await syntax and generator functions for older JavaScript environments that don't support these features natively.

81

82

```javascript { .api }

83

function _async_to_generator(fn): Function;

84

function _async_generator(fn): Function;

85

function _await_value(value): any;

86

```

87

88

[Async and Generators](./async-generators.md)

89

90

### Destructuring Helpers

91

92

Support for destructuring assignments, rest/spread operators, and array/object manipulation patterns.

93

94

```javascript { .api }

95

function _sliced_to_array(arr, i): Array;

96

function _array_without_holes(arr): Array;

97

function _object_without_properties(source, excluded): Object;

98

```

99

100

[Destructuring](./destructuring.md)

101

102

### Private Field Helpers

103

104

Runtime implementation of private class fields and methods, providing encapsulation features for classes.

105

106

```javascript { .api }

107

function _class_private_field_get(receiver, privateMap): any;

108

function _class_private_field_set(receiver, privateMap, value): void;

109

function _class_private_method_get(receiver, privateSet, fn): Function;

110

```

111

112

[Private Fields](./private-fields.md)

113

114

### Decorator Helpers

115

116

Support for decorator syntax including method decorators, class decorators, and property decorators.

117

118

```javascript { .api }

119

function _decorate(decorators, target, key, desc): any;

120

function _apply_decorated_descriptor(target, property, decorators, descriptor, context): any;

121

```

122

123

[Decorators](./decorators.md)

124

125

### Module Interoperability

126

127

Helpers for handling different module systems and import/export patterns between CommonJS and ES modules.

128

129

```javascript { .api }

130

function _interop_require_default(obj): any;

131

function _interop_require_wildcard(obj): any;

132

function _export_star(from, to): void;

133

```

134

135

[Module Interoperability](./module-interop.md)

136

137

### Type System Helpers

138

139

Utilities for type checking, conversion, and property access operations commonly used in TypeScript and JavaScript.

140

141

```javascript { .api }

142

function _type_of(obj): string;

143

function _instanceof(left, right): boolean;

144

function _to_primitive(input, hint): any;

145

```

146

147

[Type System](./type-system.md)

148

149

### Utility Helpers

150

151

General-purpose utility functions for error handling, property operations, and other common runtime needs.

152

153

```javascript { .api }

154

function _define_property(obj, key, value): Object;

155

function _extends(...sources): Object;

156

function _throw(error): never;

157

```

158

159

[Utilities](./utilities.md)