Skip to content

Commit

Permalink
compiler: fix jsx text attributes with double quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
josephsavona committed May 15, 2024
1 parent c93c30f commit 9e82704
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2040,6 +2040,9 @@ function codegenJsxAttribute(
switch (innerValue.type) {
case "StringLiteral": {
value = innerValue;
if (value.value.indexOf('"') !== -1) {
value = createJsxExpressionContainer(value.loc, value);
}
break;
}
default: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

## Input

```javascript
export function Component() {
return <Child text='Some \"text\"' />;
}

function Child(props) {
return props.text;
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};

```

## Code

```javascript
import { c as _c } from "react/compiler-runtime";
export function Component() {
const $ = _c(1);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = <Child text={'Some \\"text\\"'} />;
$[0] = t0;
} else {
t0 = $[0];
}
return t0;
}

function Child(props) {
return props.text;
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};

```
### Eval output
(kind: ok) Some \"text\"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function Component() {
return <Child text='Some \"text\"' />;
}

function Child(props) {
return props.text;
}

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

## Input

```javascript
export function Component() {
return <Child text='Some "text"' />;
}

function Child(props) {
return props.text;
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};

```

## Code

```javascript
import { c as _c } from "react/compiler-runtime";
export function Component() {
const $ = _c(1);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = <Child text={'Some "text"'} />;
$[0] = t0;
} else {
t0 = $[0];
}
return t0;
}

function Child(props) {
return props.text;
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};

```
### Eval output
(kind: ok) Some "text"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function Component() {
return <Child text='Some "text"' />;
}

function Child(props) {
return props.text;
}

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

## Input

```javascript
export function Component() {
// Test what happens if a string with double-quotes is interpolated via constant propagation
const text = 'Some "text"';
return <Child text={text} />;
}

function Child(props) {
return props.text;
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};

```

## Code

```javascript
import { c as _c } from "react/compiler-runtime";
export function Component() {
const $ = _c(1);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = <Child text={'Some "text"'} />;
$[0] = t0;
} else {
t0 = $[0];
}
return t0;
}

function Child(props) {
return props.text;
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};

```
### Eval output
(kind: ok) Some "text"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function Component() {
// Test what happens if a string with double-quotes is interpolated via constant propagation
const text = 'Some "text"';
return <Child text={text} />;
}

function Child(props) {
return props.text;
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
5 changes: 5 additions & 0 deletions compiler/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3745,6 +3745,11 @@ babel-plugin-polyfill-regenerator@^0.5.0:
dependencies:
"@babel/helper-define-polyfill-provider" "^0.4.0"

babel-plugin-react-compiler@*:
version "0.0.0"
resolved "https://registry.yarnpkg.com/babel-plugin-react-compiler/-/babel-plugin-react-compiler-0.0.0.tgz#1a1f9867fad83f217f0b3fe6f1b94cca0b77b68b"
integrity sha512-Kigl0V36a/6hLVH7+CCe1CCtU3mFBqBd829V//VtuG7I/pyq+B2QZJqOefd63snQmdfCryNhO9XW1FbGPBvYDA==

babel-plugin-syntax-hermes-parser@^0.15.1:
version "0.15.1"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-hermes-parser/-/babel-plugin-syntax-hermes-parser-0.15.1.tgz#d115ee9761a808af590a9b2a0b568115e25ea743"
Expand Down

0 comments on commit 9e82704

Please sign in to comment.