Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ compiler/**/.next

# contains invalid graphql`...` which results in a promise rejection error from `yarn prettier-all`.
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-kitchensink.js
# contains explicit resource management syntax not yet parsed by prettier.
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-await-using-declaration.js
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.todo-using-declaration.js

compiler/crates
compiler/target
Expand Down
2 changes: 1 addition & 1 deletion compiler/apps/playground/lib/compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function parseInput(
});
} else {
return babelParse(input, {
plugins: ['typescript', 'jsx'],
plugins: ['typescript', 'jsx', 'explicitResourceManagement'],
sourceType: 'module',
}) as ParseResult<t.File>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function runBabelPluginReactCompiler(
): BabelCore.BabelFileResult {
const ast = BabelParser.parse(text, {
sourceFilename: file,
plugins: [language, 'jsx'],
plugins: [language, 'jsx', 'explicitResourceManagement'],
sourceType: 'module',
});
const result = transformFromAstSync(ast, text, {
Expand Down
30 changes: 28 additions & 2 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,11 @@ function lowerStatement(
case 'VariableDeclaration': {
const stmt = stmtPath as NodePath<t.VariableDeclaration>;
const nodeKind: t.VariableDeclaration['kind'] = stmt.node.kind;
if (nodeKind === 'var') {
if (
nodeKind === 'var' ||
nodeKind === 'using' ||
nodeKind === 'await using'
) {
builder.recordError(
new CompilerErrorDetail({
reason: `(BuildHIR::lowerStatement) Handle ${nodeKind} kinds in VariableDeclaration`,
Expand All @@ -912,8 +916,8 @@ function lowerStatement(
suggestions: null,
}),
);
// Treat `var` as `let` so references to the variable don't break
}
// Treat `var` as `let` so references to the variable don't break
const kind =
nodeKind === 'let' || nodeKind === 'var'
? InstructionKind.Let
Expand Down Expand Up @@ -1183,6 +1187,17 @@ function lowerStatement(
collection: {...value},
});
if (left.isVariableDeclaration()) {
const nodeKind = left.node.kind;
if (nodeKind === 'using' || nodeKind === 'await using') {
builder.recordError(
new CompilerErrorDetail({
reason: `(BuildHIR::lowerStatement) Handle ${nodeKind} kinds in ForOfStatement`,
category: ErrorCategory.Todo,
loc: left.node.loc ?? null,
suggestions: null,
}),
);
}
const declarations = left.get('declarations');
CompilerError.invariant(declarations.length === 1, {
reason: `Expected only one declaration in the init of a ForOfStatement, got ${declarations.length}`,
Expand Down Expand Up @@ -1274,6 +1289,17 @@ function lowerStatement(
value,
});
if (left.isVariableDeclaration()) {
const nodeKind = left.node.kind;
if (nodeKind === 'using' || nodeKind === 'await using') {
builder.recordError(
new CompilerErrorDetail({
reason: `(BuildHIR::lowerStatement) Handle ${nodeKind} kinds in ForInStatement`,
category: ErrorCategory.Todo,
loc: left.node.loc ?? null,
suggestions: null,
}),
);
}
const declarations = left.get('declarations');
CompilerError.invariant(declarations.length === 1, {
reason: `Expected only one declaration in the init of a ForInStatement, got ${declarations.length}`,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

## Input

```javascript
async function useAsyncResource() {
await using resource = createAsyncResource();
return resource.value;
}

export const FIXTURE_ENTRYPOINT = {
fn: useAsyncResource,
params: [],
isComponent: false,
};

```


## Error

```
Found 1 error:

Todo: (BuildHIR::lowerStatement) Handle await using kinds in VariableDeclaration

error.todo-await-using-declaration.ts:2:2
1 | async function useAsyncResource() {
> 2 | await using resource = createAsyncResource();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (BuildHIR::lowerStatement) Handle await using kinds in VariableDeclaration
3 | return resource.value;
4 | }
5 |
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
async function useAsyncResource() {
await using resource = createAsyncResource();
return resource.value;
}

export const FIXTURE_ENTRYPOINT = {
fn: useAsyncResource,
params: [],
isComponent: false,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

## Input

```javascript
function useResource() {
using resource = createResource();
return resource.value;
}

export const FIXTURE_ENTRYPOINT = {
fn: useResource,
params: [],
isComponent: false,
};

```


## Error

```
Found 1 error:

Todo: (BuildHIR::lowerStatement) Handle using kinds in VariableDeclaration

error.todo-using-declaration.ts:2:2
1 | function useResource() {
> 2 | using resource = createResource();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (BuildHIR::lowerStatement) Handle using kinds in VariableDeclaration
3 | return resource.value;
4 | }
5 |
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function useResource() {
using resource = createResource();
return resource.value;
}

export const FIXTURE_ENTRYPOINT = {
fn: useResource,
params: [],
isComponent: false,
};
2 changes: 1 addition & 1 deletion compiler/packages/snap/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function parseInput(
} else {
return BabelParser.parse(input, {
sourceFilename: filename,
plugins: ['typescript', 'jsx'],
plugins: ['typescript', 'jsx', 'explicitResourceManagement'],
sourceType,
});
}
Expand Down