Skip to content

Commit 4bdfeda

Browse files
committed
simplify, now that expr is always defined
1 parent 7bdebaf commit 4bdfeda

3 files changed

Lines changed: 88 additions & 90 deletions

File tree

packages/migrate/migrations/routes/migrate_page_js/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function migrate_page(content) {
4141
const imports = new Set();
4242

4343
rewrite_returns(fn.body, (expr, node) => {
44-
const nodes = expr && ts.isObjectLiteralExpression(expr) && get_object_nodes(expr);
44+
const nodes = ts.isObjectLiteralExpression(expr) && get_object_nodes(expr);
4545

4646
if (nodes) {
4747
const keys = Object.keys(nodes).sort().join(' ');

packages/migrate/migrations/routes/migrate_page_server/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function migrate_page_server(content) {
3333
if (GET?.body) {
3434
// possible TODOs — handle errors and redirects
3535
rewrite_returns(GET.body, (expr, node) => {
36-
const nodes = expr && ts.isObjectLiteralExpression(expr) && get_object_nodes(expr);
36+
const nodes = ts.isObjectLiteralExpression(expr) && get_object_nodes(expr);
3737

3838
if (!nodes || nodes.headers || (nodes.status && nodes.status.getText() !== '200')) {
3939
manual_return_migration(node || GET, file.code, TASKS.PAGE_ENDPOINT);

packages/migrate/migrations/routes/migrate_server/index.js

Lines changed: 86 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -33,101 +33,99 @@ export function migrate_server(content) {
3333
const fn = get_function_node(statement, /** @type{string} */ (file.exports.map.get(method)));
3434
if (fn?.body) {
3535
rewrite_returns(fn.body, (expr, node) => {
36-
if (expr) {
37-
// leave `() => new Response(...)` alone
38-
if (is_new(expr, 'Response')) return;
39-
40-
const nodes = ts.isObjectLiteralExpression(expr) && get_object_nodes(expr);
41-
42-
if (nodes) {
43-
const body_is_object_literal = nodes.body && ts.isObjectLiteralExpression(nodes.body);
44-
45-
let safe_headers = !nodes.headers;
46-
if (nodes.headers) {
47-
if (ts.isObjectLiteralExpression(nodes.headers)) {
48-
// if `headers` is an object literal, and it either doesn't contain
49-
// `set-cookie` or `set-cookie` is a string, then the headers
50-
// are safe to use in a `Response`
51-
const set_cookie_value = nodes.headers.properties.find((prop) => {
52-
return (
53-
ts.isPropertyAssignment(prop) &&
54-
ts.isStringLiteral(prop.name) &&
55-
/set-cookie/i.test(prop.name.text)
56-
);
57-
});
58-
59-
if (!set_cookie_value || is_string_like(set_cookie_value)) {
60-
safe_headers = true;
61-
}
62-
} else {
63-
// `headers: new Headers(...)` is also safe, as long as we
64-
// don't need to augment it with `content-type`
65-
safe_headers = is_new(nodes.headers, 'Headers') && !body_is_object_literal;
66-
}
67-
}
36+
// leave `() => new Response(...)` alone
37+
if (is_new(expr, 'Response')) return;
38+
39+
const nodes = ts.isObjectLiteralExpression(expr) && get_object_nodes(expr);
40+
41+
if (nodes) {
42+
const body_is_object_literal = nodes.body && ts.isObjectLiteralExpression(nodes.body);
43+
44+
let safe_headers = !nodes.headers;
45+
if (nodes.headers) {
46+
if (ts.isObjectLiteralExpression(nodes.headers)) {
47+
// if `headers` is an object literal, and it either doesn't contain
48+
// `set-cookie` or `set-cookie` is a string, then the headers
49+
// are safe to use in a `Response`
50+
const set_cookie_value = nodes.headers.properties.find((prop) => {
51+
return (
52+
ts.isPropertyAssignment(prop) &&
53+
ts.isStringLiteral(prop.name) &&
54+
/set-cookie/i.test(prop.name.text)
55+
);
56+
});
6857

69-
const safe_body =
70-
!nodes.body ||
71-
is_string_like(nodes.body) ||
72-
body_is_object_literal ||
73-
(ts.isCallExpression(nodes.body) &&
74-
nodes.body.expression.getText() === 'JSON.stringify');
75-
76-
if (safe_headers) {
77-
let status = nodes.status ? nodes.status.getText() : '200';
78-
let headers = nodes.headers?.getText();
79-
let body = dedent(nodes.body?.getText() || 'undefined');
80-
81-
const multiline = /\n/.test(headers);
82-
83-
if (body_is_object_literal || (nodes.body && ts.isIdentifier(nodes.body))) {
84-
// `return { body: {...} }` is safe to convert to a JSON response,
85-
// but we probably need to add a `content-type` header
86-
body = `JSON.stringify(${body})`;
87-
const header = `'content-type': 'application/json; charset=utf-8'`;
88-
if (
89-
nodes.headers &&
90-
ts.isObjectLiteralExpression(nodes.headers) &&
91-
nodes.headers.properties.length > 0
92-
) {
93-
const join = multiline
94-
? `,\n${indent_at_line(content, nodes.headers.properties[0].getStart())}`
95-
: `, `;
96-
headers = headers.replace(/[{\s]/, header + join);
97-
} else {
98-
headers = `{ ${header} }`;
99-
}
58+
if (!set_cookie_value || is_string_like(set_cookie_value)) {
59+
safe_headers = true;
10060
}
61+
} else {
62+
// `headers: new Headers(...)` is also safe, as long as we
63+
// don't need to augment it with `content-type`
64+
safe_headers = is_new(nodes.headers, 'Headers') && !body_is_object_literal;
65+
}
66+
}
10167

102-
const init = [
103-
status !== '200' && `status: ${status}`,
104-
headers && `headers: ${headers}`
105-
].filter(Boolean);
106-
107-
const indent = indent_at_line(content, expr.getStart());
108-
const end_whitespace = multiline ? `\n${indent}` : ' ';
109-
const join_whitespace = multiline ? end_whitespace + guess_indent(content) : ' ';
110-
111-
const response =
112-
init.length > 0
113-
? `new Response(${body}, {${join_whitespace}${init.join(
114-
`,${join_whitespace}`
115-
)}${end_whitespace}})`
116-
: `new Response(${body})`;
117-
118-
if (safe_body) {
119-
automigration(expr, file.code, response);
68+
const safe_body =
69+
!nodes.body ||
70+
is_string_like(nodes.body) ||
71+
body_is_object_literal ||
72+
(ts.isCallExpression(nodes.body) &&
73+
nodes.body.expression.getText() === 'JSON.stringify');
74+
75+
if (safe_headers) {
76+
let status = nodes.status ? nodes.status.getText() : '200';
77+
let headers = nodes.headers?.getText();
78+
let body = dedent(nodes.body?.getText() || 'undefined');
79+
80+
const multiline = /\n/.test(headers);
81+
82+
if (body_is_object_literal || (nodes.body && ts.isIdentifier(nodes.body))) {
83+
// `return { body: {...} }` is safe to convert to a JSON response,
84+
// but we probably need to add a `content-type` header
85+
body = `JSON.stringify(${body})`;
86+
const header = `'content-type': 'application/json; charset=utf-8'`;
87+
if (
88+
nodes.headers &&
89+
ts.isObjectLiteralExpression(nodes.headers) &&
90+
nodes.headers.properties.length > 0
91+
) {
92+
const join = multiline
93+
? `,\n${indent_at_line(content, nodes.headers.properties[0].getStart())}`
94+
: `, `;
95+
headers = headers.replace(/[{\s]/, header + join);
12096
} else {
121-
manual_return_migration(
122-
node || fn,
123-
file.code,
124-
TASKS.STANDALONE_ENDPOINT,
125-
`return ${response};`
126-
);
97+
headers = `{ ${header} }`;
12798
}
99+
}
128100

129-
return;
101+
const init = [
102+
status !== '200' && `status: ${status}`,
103+
headers && `headers: ${headers}`
104+
].filter(Boolean);
105+
106+
const indent = indent_at_line(content, expr.getStart());
107+
const end_whitespace = multiline ? `\n${indent}` : ' ';
108+
const join_whitespace = multiline ? end_whitespace + guess_indent(content) : ' ';
109+
110+
const response =
111+
init.length > 0
112+
? `new Response(${body}, {${join_whitespace}${init.join(
113+
`,${join_whitespace}`
114+
)}${end_whitespace}})`
115+
: `new Response(${body})`;
116+
117+
if (safe_body) {
118+
automigration(expr, file.code, response);
119+
} else {
120+
manual_return_migration(
121+
node || fn,
122+
file.code,
123+
TASKS.STANDALONE_ENDPOINT,
124+
`return ${response};`
125+
);
130126
}
127+
128+
return;
131129
}
132130
}
133131

0 commit comments

Comments
 (0)