goaljs 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/compiler.js +40 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "goaljs",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "goal.js: var is state, let/const are computed, server-rendered and resumable",
5
5
  "homepage": "https://goallabs.github.io/goal/",
6
6
  "repository": {
package/src/compiler.js CHANGED
@@ -171,6 +171,7 @@ const routeEntry = (method, path, handler, types = {}) =>
171
171
 
172
172
  function transform(ast, mode, o, routes) {
173
173
  const program = programPath(t.cloneNode(ast, true));
174
+ if (mode === 'server') readOnly(program);
174
175
  o.tplN = 0; // row templates are numbered the same way in both modes
175
176
  const byName = new Map(routes.map((r) => [r.name, r]));
176
177
  const routeFns = program.get('body').filter((s) => s.isFunctionDeclaration() && byName.has(s.node.id.name));
@@ -185,6 +186,23 @@ function transform(ast, mode, o, routes) {
185
186
  return { program, components };
186
187
  }
187
188
 
189
+ // Assignments JavaScript refuses when the code runs, reported when it compiles instead: to a const
190
+ // or an import, anywhere in the file.
191
+ function readOnly(program) {
192
+ const scopes = new Set([program.scope]);
193
+ program.traverse({
194
+ Scopable(p) {
195
+ scopes.add(p.scope);
196
+ },
197
+ });
198
+ for (const s of scopes)
199
+ for (const [name, b] of Object.entries(s.bindings)) {
200
+ const v = b.constantViolations[0];
201
+ if (v && b.kind === 'const') fail(`\`${name}\` is a const, so it can't be changed`, v.node);
202
+ if (v && b.kind === 'module') fail(`\`${name}\` is an import, so it can't be changed`, v.node);
203
+ }
204
+ }
205
+
188
206
  function programPath(file) {
189
207
  let found;
190
208
  traverse(file, {
@@ -271,6 +289,14 @@ function route(fn, meta, program) {
271
289
  for (const [name, b] of Object.entries(fn.scope.bindings))
272
290
  if (b.kind === 'var') fail(`\`${name}\`: state (var) belongs in a component, because a route runs once per request. Use let or const`, b.path.node);
273
291
  for (const p of meta.params) if (fn.scope.hasOwnBinding(p)) fail(`\`${p}\` is already the route's path parameter {${p}}`, fn.scope.getBinding(p).identifier);
292
+ // A path parameter is a const the compiler declares (below), so it isn't bound yet: `id = 2` assigns a global
293
+ fn.get('body').traverse({
294
+ 'AssignmentExpression|UpdateExpression'(p) {
295
+ for (const [n, id] of Object.entries(p.getBindingIdentifiers()))
296
+ if (meta.params.includes(n) && !p.scope.hasBinding(n))
297
+ fail(`\`${n}\` is the route's path parameter {${n}}, so it can't be changed. Give the new value a name of its own: const n = Number(${n})`, id);
298
+ },
299
+ });
274
300
 
275
301
  const cId = t.identifier('_$c');
276
302
  fn.get('body').traverse(jsxVisitor({ kind: 'route', hs: null, scope: fn.scope, program: program.scope, reactive: new Set(), cId }));
@@ -402,6 +428,10 @@ function analyze(fn) {
402
428
  ReturnStatement(p) {
403
429
  if (p.node !== last.node && p.getFunctionParent() === fn) fail('a component has one return, at the end', p.node);
404
430
  },
431
+ // var is state, so a var is only at the top of the component: inside a function it's let or const
432
+ VariableDeclaration(p) {
433
+ if (p.node.kind === 'var' && p.getFunctionParent() !== fn) fail('inside a function, use let or const: var declares state, at the top of the component', p.node);
434
+ },
405
435
  });
406
436
 
407
437
  const state = [];
@@ -419,10 +449,19 @@ function analyze(fn) {
419
449
  state.push(name);
420
450
  } else if (b.kind === 'let' || b.kind === 'const' || b.kind === 'hoisted') {
421
451
  for (const v of b.constantViolations)
422
- if (v.getFunctionParent() !== fn) fail(`\`${name}\` is computed from state, so a handler can't change it. Declare it with \`var\` to make it state`, v.node);
452
+ if (v.getFunctionParent() !== fn)
453
+ fail(b.kind === 'hoisted' ? `\`${name}\` is a function of the component's, so a handler can't change it` : `\`${name}\` is computed from state, so a handler can't change it. Declare it with \`var\` to make it state`, v.node);
423
454
  deps.set(name, [b.path, ...b.constantViolations.map((v) => topStatement(v, node.body))]);
455
+ } else {
456
+ // the rest are the component's parameter: its props
457
+ for (const v of b.constantViolations)
458
+ if (v.getFunctionParent() !== fn) fail(`\`${name}\` comes from the props, so a handler can't change it. Copy it into state (var) and change that`, v.node);
424
459
  }
425
460
  }
461
+ // The file's own variables: no page updates when one changes, and on the server every request shares it
462
+ for (const [name, b] of Object.entries(fn.scope.getProgramParent().bindings))
463
+ for (const v of b.constantViolations)
464
+ if (v.isDescendant(fn)) fail(`\`${name}\` is declared outside the component, so the component can't change it: on the server, every request shares it. Make it state with var, in the component`, v.node);
426
465
 
427
466
  // Names whose markup has event handlers: a hole that uses one is rendered in the browser too.
428
467
  const withHandlers = new Set();