Skip to content

Commit

Permalink
Ignore masked references to FileAttachment.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Aug 2, 2020
1 parent 9efc431 commit 07e7787
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 18 deletions.
27 changes: 14 additions & 13 deletions src/file-attachments.js → src/fileAttachments.js
Expand Up @@ -3,7 +3,8 @@ import walk from "./walk.js";

export default function findFileAttachments(cell) {
const ast = {type: "Program", body: [cell.body]};
const references = new Map();
const fileAttachments = new Map();
const {references} = cell;

simple(
ast,
Expand All @@ -12,7 +13,11 @@ export default function findFileAttachments(cell) {
const {callee, arguments: args} = node;

// Ignore function calls that are not references to FileAttachment
if (!(callee.type === "Identifier" && callee.name === "FileAttachment")) return;
if (
callee.type !== "Identifier" ||
callee.name !== "FileAttachment" ||
references.indexOf(callee) < 0
) return;

// Forbid all sorts of dynamic uses of FileAttachment
if (
Expand All @@ -25,25 +30,21 @@ export default function findFileAttachments(cell) {
) {
throw Object.assign(
new SyntaxError(
`FileAttachment() requires a single literal string as its argument.`
`FileAttachment() requires a single literal string argument.`
),
{node}
);
}

const fileReference =
args[0].type === "Literal" ? args[0].value : args[0].quasis[0].value.cooked;
const fileLocation = {start: args[0].start, end: args[0].end};

if (references.has(fileReference)) {
references.get(fileReference).push(fileLocation);
} else {
references.set(fileReference, [fileLocation]);
}
const [arg] = args;
const name = arg.type === "Literal" ? arg.value : arg.quasis[0].value.cooked;
const location = {start: arg.start, end: arg.end};
if (fileAttachments.has(name)) fileAttachments.get(name).push(location);
else fileAttachments.set(name, [location]);
}
},
walk
);

return references;
return fileAttachments;
}
7 changes: 5 additions & 2 deletions src/parse.js
@@ -1,7 +1,7 @@
import {getLineInfo, tokTypes as tt, Parser} from "acorn";
import defaultGlobals from "./globals.js";
import findReferences from "./references.js";
import findFileAttachments from "./file-attachments.js";
import findFileAttachments from "./fileAttachments.js";

const SCOPE_FUNCTION = 2;
const SCOPE_ASYNC = 4;
Expand All @@ -13,7 +13,10 @@ const STATE_FUNCTION = Symbol("function");
const STATE_NAME = Symbol("name");

export function parseCell(input, {globals} = {}) {
return parseFileAttachments(parseReferences(CellParser.parse(input), input, globals));
const cell = CellParser.parse(input);
parseReferences(cell, input, globals);
parseFileAttachments(cell);
return cell;
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/references.js
@@ -1,4 +1,4 @@
// Base on https://github.com/ForbesLindesay/acorn-globals
// Based on https://github.com/ForbesLindesay/acorn-globals
// Copyright (c) 2014 Forbes Lindesay
// https://github.com/ForbesLindesay/acorn-globals/blob/master/LICENSE

Expand Down
116 changes: 114 additions & 2 deletions tap-snapshots/test-parse-test.js-TAP.test.js
Expand Up @@ -1202,7 +1202,7 @@ Object {
"column": 4,
"line": 1,
},
"message": "FileAttachment() requires a single literal string as its argument. (1:4)",
"message": "FileAttachment() requires a single literal string argument. (1:4)",
"pos": 4,
"type": "SyntaxError",
},
Expand All @@ -1216,13 +1216,125 @@ Object {
"column": 4,
"line": 1,
},
"message": "FileAttachment() requires a single literal string as its argument. (1:4)",
"message": "FileAttachment() requires a single literal string argument. (1:4)",
"pos": 4,
"type": "SyntaxError",
},
}
`

exports[`test/parse-test.js TAP parse file-attachment-masked.js > must match snapshot 1`] = `
Node {
"async": false,
"body": Node {
"body": Array [
Node {
"declarations": Array [
Node {
"end": 40,
"id": Node {
"end": 22,
"name": "FileAttachment",
"start": 8,
"type": "Identifier",
},
"init": Node {
"async": false,
"body": Node {
"end": 40,
"left": Node {
"end": 36,
"name": "a",
"start": 35,
"type": "Identifier",
},
"operator": "+",
"right": Node {
"end": 40,
"name": "b",
"start": 39,
"type": "Identifier",
},
"start": 35,
"type": "BinaryExpression",
},
"end": 40,
"expression": true,
"generator": false,
"id": null,
"params": Array [
Node {
"end": 27,
"name": "a",
"start": 26,
"type": "Identifier",
},
Node {
"end": 30,
"name": "b",
"start": 29,
"type": "Identifier",
},
],
"start": 25,
"type": "ArrowFunctionExpression",
},
"start": 8,
"type": "VariableDeclarator",
},
],
"end": 41,
"kind": "let",
"start": 4,
"type": "VariableDeclaration",
},
Node {
"argument": Node {
"arguments": Array [
Node {
"end": 67,
"raw": "1",
"start": 66,
"type": "Literal",
"value": 1,
},
Node {
"end": 70,
"raw": "2",
"start": 69,
"type": "Literal",
"value": 2,
},
],
"callee": Node {
"end": 65,
"name": "FileAttachment",
"start": 51,
"type": "Identifier",
},
"end": 71,
"start": 51,
"type": "CallExpression",
},
"end": 72,
"start": 44,
"type": "ReturnStatement",
},
],
"end": 74,
"start": 0,
"type": "BlockStatement",
},
"end": 75,
"fileAttachments": Map {},
"generator": false,
"id": null,
"references": Array [],
"start": 0,
"type": "Cell",
}
`

exports[`test/parse-test.js TAP parse file-attachment-nested.js > must match snapshot 1`] = `
Node {
"async": false,
Expand Down
4 changes: 4 additions & 0 deletions test/input/file-attachment-masked.js
@@ -0,0 +1,4 @@
{
let FileAttachment = (a, b) => a + b;
return FileAttachment(1, 2);
}

0 comments on commit 07e7787

Please sign in to comment.