A Module With No Export Is Refused

Status: refused by design

const a = 1; is not FunctionalScript. const a = 1; is valid JavaScript, and fjs compile refuses it by design: the demo below shows what it prints.

A FunctionalScript module is a function of its imports, and what it answers is its exports. A module that exports nothing answers nothing anyone can read, and having no effects it can do nothing else, so a module needs at least one export (module structure).

The row mod.no-export of refused constructs registers the refusal, and module structure gives its reason.

Examples

const a = 1;

The demo on this page compiles sample.mjs, the module above, with this build's fjs compile, and shows the line it prints and the token it refuses. Below that it shows what JavaScript answers for the same module: undefined.

The unused a binding in sample.mjs is intentional. A code-quality check correctly reports it as unused: the example must have no exports. Exporting a would remove the refusal, and deleting the binding would leave an empty module instead of the nonempty module this example demonstrates.

What to Write Instead

Export what the module computes, as alternative.f.js does:

const a = 1;
export default a;

It answers 1.

A Better Message

Today fjs compile prints unexpected end at the refused token. The demo proposes what it should print instead: the reason at the same token, what JavaScript does there, and the allowed spelling, the line alternative.f.js writes, as help. The page compiles the alternative too, so the suggestion is one the compiler accepts.

Reference

Demo

Not allowed in FunctionalScript

This is valid JavaScript, and fjs compile refuses it, by design: it is not a FunctionalScript module.

What fjs compile sample.mjs output.js prints:

sample.mjs:2:1 - error: unexpected end
2 | 
  | ^

What it should print instead, pointing to what FunctionalScript allows (proposed):

sample.mjs:2:1 - error: this module exports nothing: a FunctionalScript module answers its exports
2 | 
  | ^ JavaScript loads a module nothing can read here
  = help: export what the module computes:
          export default a;
  = note: refused by design, see spec/modules/no-export/README.md

JavaScript source, sample.mjs:

const a = 1;

What JavaScript answers for the same module, its default export:

undefined

What to write instead

The FunctionalScript spelling, alternative.f.js:

/**
 * The FunctionalScript spelling of [`sample.mjs`](./sample.mjs): the same module, exporting its value
 * ([`README.md`](./README.md)).
 *
 * @module
 */

const a = 1;
export default a;

What fjs compile alternative.f.js output.js writes:

export default 1;

What it answers, its default export:

1

Emergent Testing

Idle. Press Run to start the suite.

  • ./proof.f.mjs