Skip to content

Commit

Permalink
Merge pull request #478 from observablehq/observable-update-3
Browse files Browse the repository at this point in the history
Observable update 3
  • Loading branch information
visnup authored Aug 22, 2022
2 parents 42b80d5 + 1a5d4b0 commit 8bc0f85
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prettier",
"version": "2.7.1",
"name": "@observablehq/prettier",
"version": "2.7.1-alpha.2",
"description": "Prettier is an opinionated code formatter",
"bin": "./bin/prettier.js",
"repository": "prettier/prettier",
Expand Down
58 changes: 58 additions & 0 deletions src/language-js/print/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function printImportDeclaration(path, options, print) {

parts.push(
printModuleSpecifiers(path, options, print),
printImportInjections(path, options, print),
printModuleSource(path, options, print),
printImportAssertions(path, options, print),
semi
Expand All @@ -48,6 +49,59 @@ function printImportDeclaration(path, options, print) {
return parts;
}

// Observable:
function printImportInjections(path, options, print) {
const node = path.getValue();
/** @type{Doc[]} */
const parts = [];

const groupedInjections = [];

if (node.injections) {
parts.push(" with ");
path.each(specifierPath => {
groupedInjections.push(print(specifierPath));
}, "injections");

if (groupedInjections.length === 0) {
parts.push("{}");
} else {
const canBreak =
groupedInjections.length > 1;

if (canBreak) {
parts.push(
group(
[
"{",
indent(
[
options.bracketSpacing ? line : softline,
join([",", line], groupedInjections),
]
),
ifBreak(shouldPrintComma(options) ? "," : ""),
options.bracketSpacing ? line : softline,
"}",
]
)
);
} else {
parts.push([
"{",
options.bracketSpacing ? " " : "",
...groupedInjections,
options.bracketSpacing ? " " : "",
"}",
]);
}
}
}

return parts;
}


function printExportDeclaration(path, options, print) {
const node = path.getValue();
/** @type{Doc[]} */
Expand Down Expand Up @@ -293,6 +347,10 @@ function printModuleSpecifier(path, options, print) {
parts.push(kind, " ");
}

// Observable:
if (node.view) {parts.push("viewof ");}
if (node.mutable) {parts.push("mutable ");}

const isImport = type.startsWith("Import");
const leftSideProperty = isImport ? "imported" : "local";
const rightSideProperty = isImport ? "local" : "exported";
Expand Down
48 changes: 46 additions & 2 deletions src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const {
isCallExpression,
isMemberExpression,
markerForIfWithoutBlockAndSameLineComment,
startsWithNoLookaheadToken,
} = require("./utils/index.js");
const { locStart, locEnd } = require("./loc.js");
const isBlockComment = require("./utils/is-block-comment.js");
Expand Down Expand Up @@ -207,8 +208,20 @@ function printPathNoParens(path, options, print, args) {

return parts;

case "Program":
return printBlockBody(path, options, print);
case "Program": {
// Observable Start
const printed = printBlockBody(path, options, print);
if (printed) {
const needsParens = node.body[0] && startsWithNoLookaheadToken(node.body[0],
/* forbidFunctionClassAndDoExpr */ false);
if (needsParens) {parts.push("(");}
parts.push(printed);
if (needsParens) {parts.push(")");}
return parts;
}
return "";
// Observable End
}
// Babel extension.
case "EmptyStatement":
return "";
Expand Down Expand Up @@ -804,6 +817,37 @@ function printPathNoParens(path, options, print, args) {
return parts;
}

// Observable types: Start
case "ViewExpression":
return ["viewof ", path.call(print, "id")];
case "MutableExpression":
return ["mutable ", path.call(print, "id")];
case "Cell": {
const shouldAddParens = node.body && startsWithNoLookaheadToken(node.body,
/* forbidFunctionClassAndDoExpr */ false);

const bodyId =
node.body &&
node.body.id !== null &&
(node.body.type === "FunctionExpression" || node.body.type === "ClassExpression") &&
node.body.id;
const isNamed = node.id && node.id !== bodyId;

const id = isNamed ? [
path.call(print, "id"),
" = "
] : [];

const body = [
shouldAddParens ? "(" : "",
path.call(print, "body"),
shouldAddParens ? ")" : ""
];

return ([...id, ...body]);
}
// Observable types: End

default:
/* istanbul ignore next */
throw new Error("unknown type: " + JSON.stringify(node.type));
Expand Down

0 comments on commit 8bc0f85

Please sign in to comment.