Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore masked references to FileAttachment. #129

Merged
merged 1 commit into from Aug 31, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}