All files / src nodes.jsx

14.39% Statements 20/139
15.63% Branches 5/32
7.69% Functions 6/78
14.71% Lines 20/136

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 5671x 1x 1x 1x 1x 1x 1x                           112x               112x                                                                                   14x 14x 14x                                     12x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             45x 45x 45x                         100x                                                                                                                                                               3x 3x 3x                                                      
import * as P from 'pretty-fast-pretty-printer';
import React from 'react';
import {ASTNode, enumerateList, pluralize} from './ast';
import Node from './components/Node';
import Args from './components/Args';
import { DropTarget } from './components/DropTarget';
import * as Spec from './nodeSpec';
 
 
 
// Displays a comment according to specific rules.
//
// - `doc` is what's being commented.
// - `comment` is the comment itself. If it is falsy, there is no comment.
// - `container` is the ast node that owns the comment. This argument is used to
//   determine if the comment is a line comment (appears after `container` on
//   the same line). Line comments will stay as line comments _as long as they
//   fit on the line_. If they don't, they'll be converted into a comment on the
//   previous line.
function withComment(doc, comment, container) {
  Iif (comment) {
    // This comment was on the same line as the node. Keep it that way, as long as it fits on a line.
    if (container && container.to.line == comment.from.line) {
      return P.ifFlat(P.horz(doc, " ", comment), P.vert(comment, doc));
    } else {
      return P.vert(comment, doc);
    }
  } else {
    return doc;
  }
}
 
export class Unknown extends ASTNode {
  constructor(from, to, elts, options={}) {
    super(from, to, 'unknown', options);
    this.elts = elts;
  }
 
  static spec = Spec.nodeSpec([
    Spec.list('elts')
  ])
 
  longDescription(level) {
    return `an unknown expression with ${pluralize("children", this.elts)} `+ 
      this.elts.map((e, i, elts)  => (elts.length>1? (i+1) + ": " : "")+ e.describe(level)).join(", ");
  }
 
  pretty() {
    return withComment(
      P.standardSexpr(this.elts[0], this.elts.slice(1)),
      this.options.comment,
      this);
  }
 
  render(props) {
    const firstElt = this.elts[0].reactElement();
    const restElts = this.elts.slice(1);
    return (
      <Node node={this} {...props}>
        <span className="blocks-operator">{firstElt}</span>
        <span className="blocks-args">
        <Args field="elts">{restElts}</Args>
        </span>
      </Node>
    );
  }
}
 
export class FunctionApp extends ASTNode {
  constructor(from, to, func, args, options={}) {
    super(from, to, 'functionApp', options);
    this.func = func;
    this.args = args;
  }
 
  static spec = Spec.nodeSpec([
    Spec.required('func'),
    Spec.list('args')
  ])
 
  longDescription(level) {
    // if it's the top level, enumerate the args
    if((this.level  - level) == 0) {
      return `applying the function ${this.func.describe(level)} to ${pluralize("argument", this.args)} `+
      this.args.map((a, i, args)  => (args.length>1? (i+1) + ": " : "")+ a.describe(level)).join(", ");
    }
    // if we're lower than that (but not so low that `.shortDescription()` is used), use "f of A, B, C" format
    else return `${this.func.describe(level)} of `+ this.args.map(a  => a.describe(level)).join(", ");
  }
 
  pretty() {
    return withComment(
      P.standardSexpr(this.func, this.args),
      this.options.comment,
      this);
  }
 
  render(props) {
    const func = this.func.reactElement();
    return (
      <Node node={this} {...props}>
        <span className="blocks-operator">
          {func}
        </span>
        <span className="blocks-args">
          <Args field="args">{this.args}</Args>
        </span>
    </Node>
    );
  }
}
 
export class IdentifierList extends ASTNode {
  constructor(from, to, kind, ids, options={}) {
    super(from, to, 'identifierList', options);
    this.kind = kind;
    this.ids = ids;
  }
 
  static spec = Spec.nodeSpec([
    Spec.value('kind'),
    Spec.list('ids')
  ])
 
  longDescription(level) {
    return enumerateList(this.ids, level);
  }
 
  pretty() {
    return withComment(
      P.sepBy(this.ids, " "),
      this.options.comment,
      this);
  }
 
  render(props) {
    return (
      <Node node={this} {...props}>
        <span className="blocks-args">
          <Args field="ids">{this.ids}</Args>
        </span>
      </Node>
    );
  }
}
 
export class StructDefinition extends ASTNode {
  constructor(from, to, name, fields, options={}) {
    super(from, to, 'structDefinition', options);
    this.name = name;
    this.fields = fields;
  }
 
  static spec = Spec.nodeSpec([
    Spec.value('name'),
    Spec.required('fields')
  ])
 
  longDescription(level) {
    return `define ${this.name.describe(level)} to be a structure with ${this.fields.describe(level)}`;
  }
 
  pretty() {
    return withComment(
      P.lambdaLikeSexpr("define-struct", this.name, P.horz("(", this.fields, ")")),
      this.options.comment,
      this);
  }
 
  render(props) {
    const name = this.name.reactElement();
    const fields = this.fields.reactElement();
    return (
      <Node node={this} {...props}>
        <span className="blocks-operator">
          define-struct
          {name}
        </span>
        {fields}
      </Node>
    );
  }
}
 
export class VariableDefinition extends ASTNode {
  constructor(from, to, name, body, options={}) {
    super(from, to, 'variableDefinition', options);
    this.name = name;
    this.body = body;
  }
 
  static spec = Spec.nodeSpec([
    Spec.required('name'),
    Spec.required('body')
  ])
 
  longDescription(level) {
    let insert = ["literal", "blank"].includes(this.body.type)? "" : "the result of:";
    return `define ${this.name} to be ${insert} ${this.body.describe(level)}`;
  }
 
  pretty() {
    return withComment(
      P.lambdaLikeSexpr("define", this.name, this.body),
      this.options.comment,
      this);
  }
 
  render(props) {
    const body = this.body.reactElement();
    const name = this.name.reactElement();
    return (
      <Node node={this} {...props}>
        <span className="blocks-operator">
          define
          {name}
        </span>
        <span className="blocks-args">
          {body}
        </span>
      </Node>
    );
  }
}
 
export class LambdaExpression extends ASTNode {
  constructor(from, to, args, body, options={}) {
    super(from, to, 'lambdaExpression', options);
    this.args = args;
    this.body = body;
  }
 
  static spec = Spec.nodeSpec([
    Spec.required('args'),
    Spec.required('body')
  ])
 
  longDescription(level) {
    return `an anonymous function of ${pluralize("argument", this.args.ids)}: 
            ${this.args.describe(level)}, with body:
            ${this.body.describe(level)}`;
  }
 
  pretty() {
    return P.lambdaLikeSexpr("lambda(", P.horz("(", this.args, ")"), this.body);
  }
 
  render(props) {
    const args = this.args.reactElement();
    const body = this.body.reactElement();
    return (
      <Node node={this} {...props}>
        <span className="blocks-operator">
          &lambda; ({args})
        </span>
        <span className="blocks-args">
          {body}
        </span>
      </Node>
    );
  }
}
 
export class FunctionDefinition extends ASTNode {
  constructor(from, to, name, params, body, options={}) {
    super(from, to, 'functionDefinition', options);
    this.name = name;
    this.params = params;
    this.body = body;
  }
 
  static spec = Spec.nodeSpec([
    Spec.required('name'),
    Spec.required('params'),
    Spec.required('body')
  ])
 
  longDescription(level) {
    return `define ${this.name} to be a function of 
            ${this.params.describe(level)}, with body:
            ${this.body.describe(level)}`;
  }
 
  pretty() {
    return withComment(
      P.lambdaLikeSexpr(
        "define",
        P.standardSexpr(this.name, this.params),
        this.body),
      this.options.comment,
      this);
  }
 
  render(props) {
    let params = this.params.reactElement();
    let body = this.body.reactElement();
    let name = this.name.reactElement();
    return (
      <Node node={this} {...props}>
        <span className="blocks-operator">
          define ({name} {params})
        </span>
        <span className="blocks-args">
          {body}
        </span>
      </Node>
    );
  }
}
 
export class CondClause extends ASTNode {
  constructor(from, to, testExpr, thenExprs, options={}) {
    super(from, to, 'condClause', options);
    this.testExpr = testExpr;
    this.thenExprs = thenExprs;
  }
 
  static spec = Spec.nodeSpec([
    Spec.required('testExpr'),
    Spec.list('thenExprs')
  ])
 
  longDescription(level) {
    return `condition: if ${this.testExpr.describe(level)}, then, ${this.thenExprs.map(te => te.describe(level))}`;
  }
 
  pretty() {
    return P.horz("[", P.sepBy([this.testExpr].concat(this.thenExprs), " "), "]");
  }
 
  render(props) {
    const testExpr = this.testExpr.reactElement();
    return (
      <Node node={this} {...props}>
        <div className="blocks-cond-row">
          <div className="blocks-cond-predicate">
            {testExpr}
          </div>
          <div className="blocks-cond-result">
            {this.thenExprs.map((thenExpr, index) => (
              <span key={index}>
                <DropTarget field="thenExprs"/>
                {thenExpr.reactElement()}
              </span>))}
            <DropTarget field="thenExprs"/>
          </div>
        </div>
      </Node>
    );
  }
}
 
export class CondExpression extends ASTNode {
  constructor(from, to, clauses, options={}) {
    super(from, to, 'condExpression', options);
    this.clauses = clauses;
  }
 
  static spec = Spec.nodeSpec([
    Spec.list('clauses')
  ])
 
  longDescription(level) {
    return `a conditional expression with ${pluralize("condition", this.clauses)}: 
            ${this.clauses.map(c => c.describe(level))}`;
  }
 
  pretty() {
    return P.beginLikeSexpr("cond", this.clauses);
  }
 
  render(props) {
    const clauses = this.clauses.map((clause, index) => clause.reactElement({key: index}));
    return (
      <Node node={this} {...props}>
        <span className="blocks-operator">cond</span>
        <div className="blocks-cond-table">
          {clauses}
        </div>
      </Node>
    );
  }
}
 
export class IfExpression extends ASTNode {
  constructor(from, to, testExpr, thenExpr, elseExpr, options={}) {
    super(from, to, 'ifExpression', options);
    this.testExpr = testExpr;
    this.thenExpr = thenExpr;
    this.elseExpr = elseExpr;
  }
 
  static spec = Spec.nodeSpec([
    Spec.required('testExpr'),
    Spec.required('thenExpr'),
    Spec.required('elseExpr')
  ])
 
  longDescription(level) {
    return `an if expression: if ${this.testExpr.describe(level)}, then ${this.thenExpr.describe(level)} `+
            `else ${this.elseExpr.describe(level)}`;
  }
 
  pretty() {
    return withComment(
      P.standardSexpr("if", [this.testExpr, this.thenExpr, this.elseExpr]),
      this.options.comment,
      this);
  }
 
  render(props) {
    const testExpr = this.testExpr.reactElement();
    const thenExpr = this.thenExpr.reactElement();
    const elseExpr = this.elseExpr.reactElement();
    return (
      <Node node={this} {...props}>
        <span className="blocks-operator">if</span>
        <div className="blocks-cond-table">
          <div className="blocks-cond-row">
            <div className="blocks-cond-predicate">
              {testExpr}
            </div>
            <div className="blocks-cond-result">
              {thenExpr}
            </div>
          </div>
          <div className="blocks-cond-row">
            <div className="blocks-cond-predicate blocks-cond-else">
              else
            </div>
            <div className="blocks-cond-result">
              {elseExpr}
            </div>
          </div>
        </div>
      </Node>
    );
  }
}
 
export class Literal extends ASTNode {
  constructor(from, to, value, dataType='unknown', options={}) {
    super(from, to, 'literal', options);
    this.value = value;
    this.dataType = dataType;
  }
 
  static spec = Spec.nodeSpec([
    Spec.value('value'),
    Spec.value('dataType')
  ])
 
  describe(_level) {
    return this.options["aria-label"];
  }
 
  pretty() {
    return withComment(P.txt(this.value), this.options.comment, this);
  }
 
  render(props) {
    return (
      <Node node={this}
            normallyEditable={true}
            expandable={false}
            {...props}>
        <span className={`blocks-literal-${this.dataType}`}>
          {this.value.toString()}
        </span>
      </Node>
    );
  }
}
 
export class Comment extends ASTNode {
  constructor(from, to, comment, options={}) {
    super(from, to, 'comment', options);
    this.comment = comment;
    this.isLockedP = true;
  }
 
  static spec = Spec.nodeSpec([
    Spec.value('comment')
  ])
 
  describe(_level) {
    return this.options["aria-label"];
  }
 
  pretty() {
    let words = this.comment.trim().split(/\s+/);
    let wrapped = P.wrap(words);
    // Normalize all comments to block comments
    return P.concat("#| ", wrapped, " |#");
  }
 
  render(props) { // eslint-disable-line no-unused-vars
    return (<span className="blocks-comment" id={this.id} aria-hidden="true">
      <span className="screenreader-only">Has comment,</span> <span>{this.comment.toString()}</span>
    </span>);
  }
}
 
export class Blank extends ASTNode {
  constructor(from, to, value, dataType='blank', options={}) {
    super(from, to, 'blank', options);
    this.value = value || "...";
    this.dataType = dataType;
  }
 
  static spec = Spec.nodeSpec([
    Spec.value('value'),
    Spec.value('dataType')
  ])
 
  describe(_level) {
    return this.options["aria-label"];
  }
 
  pretty() {
    return P.txt(this.value);
  }
 
  render(props) {
    return (
      <Node node={this}
            normallyEditable={true}
            expandable={false}
            {...props}>
        <span className="blocks-literal-symbol" />
      </Node>
    );
  }
}
 
export class Sequence extends ASTNode {
  constructor(from, to, exprs, name, options={}) {
    super(from, to, 'sequence', options);
    this.exprs = exprs;
    this.name = name;
  }
 
  static spec = Spec.nodeSpec([
    Spec.list('exprs'),
    Spec.value('name')
  ])
 
  longDescription(level) {
    return `a sequence containing ${enumerateList(this.exprs, level)}`;
  }
 
  pretty() {
    return P.standardSexpr(this.name, this.exprs);
  }
 
  render(props) {
    return (
      <Node node={this} {...props}>
        <span className="blocks-operator">{this.name}</span>
        <div className="blocks-sequence-exprs">
          <Args field="exprs">{this.exprs}</Args>
        </div>
      </Node>
    );
  }
}