Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export function addImportsToProgram(
programContext: ProgramContext,
): void {
const existingImports = getExistingImports(path);
const stmts: Array<t.ImportDeclaration> = [];
const stmts: Array<t.ImportDeclaration | t.VariableDeclaration> = [];
const sortedModules = [...programContext.imports.entries()].sort(([a], [b]) =>
a.localeCompare(b),
);
Expand Down Expand Up @@ -303,9 +303,29 @@ export function addImportsToProgram(
if (maybeExistingImports != null) {
maybeExistingImports.pushContainer('specifiers', importSpecifiers);
} else {
stmts.push(
t.importDeclaration(importSpecifiers, t.stringLiteral(moduleName)),
);
if (path.node.sourceType === 'module') {
stmts.push(
t.importDeclaration(importSpecifiers, t.stringLiteral(moduleName)),
);
} else {
stmts.push(
t.variableDeclaration('const', [
t.variableDeclarator(
t.objectPattern(
sortedImport.map(specifier => {
return t.objectProperty(
t.identifier(specifier.imported),
t.identifier(specifier.name),
);
}),
),
t.callExpression(t.identifier('require'), [
t.stringLiteral(moduleName),
]),
),
]),
);
}
}
}
path.unshiftContainer('body', stmts);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

## Input

```javascript
// @script
const React = require('react');

function Component(props) {
return <div>{props.name}</div>;
}

// To work with snap evaluator
exports = {
FIXTURE_ENTRYPOINT: {
fn: Component,
params: [{name: 'React Compiler'}],
},
};

```

## Code

```javascript
const { c: _c } = require("react/compiler-runtime"); // @script
const React = require("react");

function Component(props) {
const $ = _c(2);
let t0;
if ($[0] !== props.name) {
t0 = <div>{props.name}</div>;
$[0] = props.name;
$[1] = t0;
} else {
t0 = $[1];
}
return t0;
}

// To work with snap evaluator
exports = {
FIXTURE_ENTRYPOINT: {
fn: Component,
params: [{ name: "React Compiler" }],
},
};

```

### Eval output
(kind: ok) <div>React Compiler</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @script
const React = require('react');

function Component(props) {
return <div>{props.name}</div>;
}

// To work with snap evaluator
exports = {
FIXTURE_ENTRYPOINT: {
fn: Component,
params: [{name: 'React Compiler'}],
},
};
13 changes: 10 additions & 3 deletions compiler/packages/snap/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ import prettier from 'prettier';
import SproutTodoFilter from './SproutTodoFilter';
import {isExpectError} from './fixture-utils';
import {makeSharedRuntimeTypeProvider} from './sprout/shared-runtime-type-provider';

export function parseLanguage(source: string): 'flow' | 'typescript' {
return source.indexOf('@flow') !== -1 ? 'flow' : 'typescript';
}

export function parseSourceType(source: string): 'script' | 'module' {
return source.indexOf('@script') !== -1 ? 'script' : 'module';
}

/**
* Parse react compiler plugin + environment options from test fixture. Note
* that although this primarily uses `Environment:parseConfigPragma`, it also
Expand Down Expand Up @@ -98,21 +103,22 @@ export function parseInput(
input: string,
filename: string,
language: 'flow' | 'typescript',
sourceType: 'module' | 'script',
): BabelCore.types.File {
// Extract the first line to quickly check for custom test directives
if (language === 'flow') {
return HermesParser.parse(input, {
babel: true,
flow: 'all',
sourceFilename: filename,
sourceType: 'module',
sourceType,
enableExperimentalComponentSyntax: true,
});
} else {
return BabelParser.parse(input, {
sourceFilename: filename,
plugins: ['typescript', 'jsx'],
sourceType: 'module',
sourceType,
});
}
}
Expand Down Expand Up @@ -221,11 +227,12 @@ export async function transformFixtureInput(
const firstLine = input.substring(0, input.indexOf('\n'));

const language = parseLanguage(firstLine);
const sourceType = parseSourceType(firstLine);
// Preserve file extension as it determines typescript's babel transform
// mode (e.g. stripping types, parsing rules for brackets)
const filename =
path.basename(fixturePath) + (language === 'typescript' ? '.ts' : '');
const inputAst = parseInput(input, filename, language);
const inputAst = parseInput(input, filename, language, sourceType);
// Give babel transforms an absolute path as relative paths get prefixed
// with `cwd`, which is different across machines
const virtualFilepath = '/' + filename;
Expand Down
5 changes: 4 additions & 1 deletion compiler/packages/snap/src/sprout/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@ export function doEval(source: string): EvaluatorResult {
return {
kind: 'UnexpectedError',
value:
'Unexpected error during eval, possible syntax error?\n' + e.message,
'Unexpected error during eval, possible syntax error?\n' +
e.message +
'\n\nsource:\n' +
source,
logs,
};
} finally {
Expand Down
Loading