nsp-server-pages 0.2.2 → 0.2.3

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.
package/cjs/src/app.js CHANGED
@@ -78,7 +78,7 @@ class App {
78
78
  return loader.load(file);
79
79
  }
80
80
  store(context, key) {
81
- if ("object" !== typeof context && context == null) {
81
+ if ("object" !== typeof context || context == null) {
82
82
  throw new Error("Context must be an object");
83
83
  }
84
84
  const { storeKey } = this.options;
@@ -58,10 +58,10 @@ class Attr {
58
58
  const nextLF = LF + SP;
59
59
  const keys = this.keys();
60
60
  const items = keys.map(key => {
61
+ const value = this.get(key);
61
62
  if (!isSafeKey(key)) {
62
63
  key = JSON.stringify(key);
63
64
  }
64
- const value = this.get(key);
65
65
  return `${key}: ${value}`;
66
66
  });
67
67
  // no arguments
@@ -46,6 +46,7 @@ const jspToJS = (app, src, option) => {
46
46
  const root = new tag_js_1.Tag(app);
47
47
  const tree = new store_js_1.Store(root);
48
48
  const array = src.split(tagRegExp);
49
+ const { vName } = app.options;
49
50
  for (let i = 0; i < array.length; i++) {
50
51
  const i3 = i % 3;
51
52
  let str = array[i];
@@ -55,7 +56,7 @@ const jspToJS = (app, src, option) => {
55
56
  // close-tag
56
57
  if (tag.isClose()) {
57
58
  const closed = tree.close();
58
- if (!closed) {
59
+ if (!closed?.tagName) {
59
60
  throw new Error(`invalid closing tag: </${tag.tagName}>`);
60
61
  }
61
62
  if (closed.tagName !== tag.tagName) {
@@ -72,7 +73,13 @@ const jspToJS = (app, src, option) => {
72
73
  else if (i3 === 2 && str) {
73
74
  // <% scriptlet %>
74
75
  const item = new scriptlet_js_1.Scriptlet(app, str);
75
- tree.get().append(item);
76
+ const toJS = (option) => {
77
+ const js = item.toJS(option);
78
+ if (/^\/\//.test(js))
79
+ return js; // comment
80
+ return `${vName} => ${js}`; // array function
81
+ };
82
+ tree.get().append({ toJS });
76
83
  }
77
84
  else if (i3 === 0) {
78
85
  // text node
@@ -40,7 +40,7 @@ class Scriptlet {
40
40
  }
41
41
  app.log(`${type} found: ${src?.substring(0, 1000)}`);
42
42
  src = /`|\$\{/.test(src) ? JSON.stringify(src) : "`" + src + "`";
43
- src = `${vName} => ${nspName}.process("${type}", ${src}, ${vName})`;
43
+ src = `${nspName}.process("${type}", ${src}, ${vName})`;
44
44
  return src;
45
45
  }
46
46
  }
@@ -104,7 +104,7 @@ class Tag {
104
104
  const nextLF = LF + SP;
105
105
  const nextOption = { SP, LF: (body ? nextLF : LF) };
106
106
  const type = `parse.tag.${tagName}`;
107
- const def = { app, name: tagName, attr, body, LF: LF, nextLF };
107
+ const def = { app, name: tagName, attr, body, LF, nextLF };
108
108
  const tagJS = app.process(type, def) ?? this.getTagJS(def, nextOption);
109
109
  return commentJS ? commentJS + tagJS : tagJS;
110
110
  }
@@ -52,7 +52,11 @@ const textToJS = (app, src, option) => {
52
52
  value = value.replace(/\s*}$/s, "");
53
53
  const item = new el_js_1.EL(app, value);
54
54
  if (isAsync) {
55
- items.push({ toJS: (option) => `await ${item.toJS(option)}` });
55
+ const toJS = (option) => {
56
+ const js = item.toJS(option);
57
+ return `await ${js}`;
58
+ };
59
+ items.push({ toJS });
56
60
  }
57
61
  else {
58
62
  items.push(item);
package/esm/src/app.js CHANGED
@@ -75,7 +75,7 @@ export class App {
75
75
  return loader.load(file);
76
76
  }
77
77
  store(context, key) {
78
- if ("object" !== typeof context && context == null) {
78
+ if ("object" !== typeof context || context == null) {
79
79
  throw new Error("Context must be an object");
80
80
  }
81
81
  const { storeKey } = this.options;
@@ -55,10 +55,10 @@ export class Attr {
55
55
  const nextLF = LF + SP;
56
56
  const keys = this.keys();
57
57
  const items = keys.map(key => {
58
+ const value = this.get(key);
58
59
  if (!isSafeKey(key)) {
59
60
  key = JSON.stringify(key);
60
61
  }
61
- const value = this.get(key);
62
62
  return `${key}: ${value}`;
63
63
  });
64
64
  // no arguments
@@ -42,6 +42,7 @@ export const jspToJS = (app, src, option) => {
42
42
  const root = new Tag(app);
43
43
  const tree = new Store(root);
44
44
  const array = src.split(tagRegExp);
45
+ const { vName } = app.options;
45
46
  for (let i = 0; i < array.length; i++) {
46
47
  const i3 = i % 3;
47
48
  let str = array[i];
@@ -51,7 +52,7 @@ export const jspToJS = (app, src, option) => {
51
52
  // close-tag
52
53
  if (tag.isClose()) {
53
54
  const closed = tree.close();
54
- if (!closed) {
55
+ if (!closed?.tagName) {
55
56
  throw new Error(`invalid closing tag: </${tag.tagName}>`);
56
57
  }
57
58
  if (closed.tagName !== tag.tagName) {
@@ -68,7 +69,13 @@ export const jspToJS = (app, src, option) => {
68
69
  else if (i3 === 2 && str) {
69
70
  // <% scriptlet %>
70
71
  const item = new Scriptlet(app, str);
71
- tree.get().append(item);
72
+ const toJS = (option) => {
73
+ const js = item.toJS(option);
74
+ if (/^\/\//.test(js))
75
+ return js; // comment
76
+ return `${vName} => ${js}`; // array function
77
+ };
78
+ tree.get().append({ toJS });
72
79
  }
73
80
  else if (i3 === 0) {
74
81
  // text node
@@ -37,7 +37,7 @@ export class Scriptlet {
37
37
  }
38
38
  app.log(`${type} found: ${src?.substring(0, 1000)}`);
39
39
  src = /`|\$\{/.test(src) ? JSON.stringify(src) : "`" + src + "`";
40
- src = `${vName} => ${nspName}.process("${type}", ${src}, ${vName})`;
40
+ src = `${nspName}.process("${type}", ${src}, ${vName})`;
41
41
  return src;
42
42
  }
43
43
  }
@@ -101,7 +101,7 @@ export class Tag {
101
101
  const nextLF = LF + SP;
102
102
  const nextOption = { SP, LF: (body ? nextLF : LF) };
103
103
  const type = `parse.tag.${tagName}`;
104
- const def = { app, name: tagName, attr, body, LF: LF, nextLF };
104
+ const def = { app, name: tagName, attr, body, LF, nextLF };
105
105
  const tagJS = app.process(type, def) ?? this.getTagJS(def, nextOption);
106
106
  return commentJS ? commentJS + tagJS : tagJS;
107
107
  }
@@ -48,7 +48,11 @@ const textToJS = (app, src, option) => {
48
48
  value = value.replace(/\s*}$/s, "");
49
49
  const item = new EL(app, value);
50
50
  if (isAsync) {
51
- items.push({ toJS: (option) => `await ${item.toJS(option)}` });
51
+ const toJS = (option) => {
52
+ const js = item.toJS(option);
53
+ return `await ${js}`;
54
+ };
55
+ items.push({ toJS });
52
56
  }
53
57
  else {
54
58
  items.push(item);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nsp-server-pages",
3
3
  "description": "NSP JavaScript Server Pages for Node.js",
4
- "version": "0.2.2",
4
+ "version": "0.2.3",
5
5
  "author": "@kawanet",
6
6
  "bugs": {
7
7
  "url": "https://github.com/kawanet/nsp-server-pages/issues"
@@ -13,8 +13,9 @@
13
13
  "devDependencies": {
14
14
  "@rollup/plugin-node-resolve": "^15.2.1",
15
15
  "@types/mocha": "^10.0.1",
16
- "@types/node": "^20.5.7",
16
+ "@types/node": "^20.5.8",
17
17
  "mocha": "^10.2.0",
18
+ "nyc": "^15.1.0",
18
19
  "typescript": "^5.2.2"
19
20
  },
20
21
  "exports": {
@@ -49,6 +50,7 @@
49
50
  },
50
51
  "scripts": {
51
52
  "build": "make all",
53
+ "coverage": "npx tsc -p tsconfig-cjs.json && npx nyc npx mocha cjs/test",
52
54
  "fixpack": "fixpack",
53
55
  "prepack": "make clean test-title all test",
54
56
  "test": "make test"
package/types/index.d.ts CHANGED
@@ -19,7 +19,7 @@ export declare namespace NSP {
19
19
 
20
20
  type TagFn<A, T = any> = (tag: TagDef<A, T>) => (NodeFn<T> | VoidFn<T>);
21
21
 
22
- type TagCon<A, T = any> = { new(tag: NSP.TagDef<A>, context: T): TagClass };
22
+ type TagCon<A, T = any, P extends TagClass = TagClass> = { new(tag: NSP.TagDef<A>, context: T): P, prototype: P };
23
23
 
24
24
  type LoaderFn = (path: string) => Promise<NodeFn<any> | undefined>;
25
25