@makano/rew 1.2.73 → 1.2.75

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,7 +9,9 @@ const execOptions = {
9
9
  jsxPragma: '__using__.JSX.createElement',
10
10
  jsx: false,
11
11
  typescript: false,
12
- decorators: false
12
+ decorators: false,
13
+
14
+ _syntaxAliases: {}
13
15
  };
14
16
 
15
17
  module.exports.execOptions = execOptions;
@@ -29,18 +29,22 @@ module.exports.Usage = class Usage {
29
29
  return new Usage(name, trigger, save);
30
30
  }
31
31
 
32
- group(group, props){
33
- return new Usage.Group(group, props);
32
+ group(...group){
33
+ return new Usage.Group(group, {});
34
34
  }
35
35
 
36
36
  static Group = class UsageGroup {
37
- g = [{}, () => {}]
38
- constructor(g, props){
37
+ g = []
38
+ constructor(g, props = {}){
39
39
  this.g = g;
40
- for(let i in props){
40
+ if(props) for(let i in props){
41
41
  this[i] = props[i];
42
42
  }
43
43
  }
44
+
45
+ with(props){
46
+ return new UsageGroup(this.g, props);
47
+ }
44
48
  }
45
49
  }
46
50
 
@@ -6,7 +6,7 @@ const { getFile, file } = require('./fs');
6
6
  const babel = require('@babel/core');
7
7
  const path = require('path');
8
8
  const babelReact = require('@babel/preset-react');
9
- const { readFileSync } = require('fs');
9
+ const { readFileSync, existsSync } = require('fs');
10
10
  const { wait } = require('../functions/wait');
11
11
  const { REW_FILE_TYPE } = require('../const/ext');
12
12
  const { USING_DEFAULT } = require('../const/usage');
@@ -122,12 +122,43 @@ const fnextToken = (i, tokens, type, value) => {
122
122
  .find((t) => t.type == type && (value ? t.value == value : true));
123
123
  };
124
124
 
125
+ function declareAlias(aliases, token) {
126
+ const regex = /^#declare(\*)?\s+(\w+)\s+"([^"]+)"\s*=\s*([\s\S]*);$/;
127
+ const match = token.value.trim().match(regex);
128
+
129
+ if (match) {
130
+ const isPublic = !!match[1];
131
+ const type = match[2] == "key" ? 'IDENTIFIER' : match[2];
132
+ const name = match[3];
133
+ const value = match[4].trim();
134
+
135
+ const aliasValue = value.startsWith('${')
136
+ ? new Function('token', 'tokens', 'code', value.slice(2, -1))
137
+ : value;
138
+
139
+ aliases[type] = aliases[type] || {};
140
+ aliases[type][name] = aliasValue;
141
+
142
+ if(isPublic){
143
+ execOptions._syntaxAliases[type] = execOptions._syntaxAliases[type] || {};
144
+ execOptions._syntaxAliases[type][name] = aliasValue;
145
+ }
146
+ }
147
+ }
148
+
125
149
  function compileRewStuff(content, options) {
126
150
  const tokens = tokenizeCoffeeScript(content);
127
151
  let result = '';
152
+ let multilineDeclareBuffer = [];
153
+ let multilineDeclare = false;
128
154
 
129
155
  let hooks = [];
130
156
 
157
+
158
+ const aliases = {
159
+ ...execOptions._syntaxAliases
160
+ }
161
+
131
162
  for (let i = 0; i < tokens.length; i++) {
132
163
  const token = tokens[i];
133
164
  let { token: nextToken, n } = gnextToken(i, 1, tokens) || {};
@@ -136,6 +167,30 @@ function compileRewStuff(content, options) {
136
167
  continue;
137
168
  }
138
169
 
170
+ if ((token.type === "COMMENT" && multilineDeclare) || (token.type !== "COMMENT" && multilineDeclare)) {
171
+ if(token.type === "COMMENT"){
172
+ multilineDeclareBuffer.push(token.value.startsWith('###') ? token.value.slice(3) : token.value.slice(1));
173
+ if (token.value.includes(';')) {
174
+ multilineDeclare = false;
175
+ const combinedDeclaration = multilineDeclareBuffer.join('\n');
176
+ declareAlias(aliases, { ...token, value: combinedDeclaration });
177
+ multilineDeclareBuffer = [];
178
+ }
179
+ } else {
180
+ multilineDeclare = false;
181
+ multilineDeclareBuffer = [];
182
+ }
183
+ }
184
+
185
+ if (token.type === "COMMENT" && token.value.startsWith('#declare')) {
186
+ if (token.value.includes(';')) {
187
+ declareAlias(aliases, token);
188
+ } else {
189
+ multilineDeclare = true;
190
+ multilineDeclareBuffer.push(token.value);
191
+ }
192
+ }
193
+
139
194
  if (token.type === 'IDENTIFIER' && token.value === 'opt.set') {
140
195
  const { token: nextNextToken } = gnextToken(i, 2, tokens) || {};
141
196
  if (nextNextToken && nextNextToken.value.slice(1).slice(0, -1) == 'jsxPragma') {
@@ -269,6 +324,17 @@ function compileRewStuff(content, options) {
269
324
  });
270
325
  }
271
326
 
327
+ const aliasType = aliases[token.type];
328
+ if (aliasType && aliasType[token.value]) {
329
+ const aliasValue = aliasType[token.value];
330
+ if (typeof aliasValue === 'function') {
331
+ result += aliasValue(token, tokens, result) || "";
332
+ } else {
333
+ result += aliasValue;
334
+ }
335
+ continue;
336
+ }
337
+
272
338
  // if(token.type === 'TRIPLE_STRING'){
273
339
  // token.value = '```'+token.value.slice(3).slice(0, -3).replace(/\#\{/g, '${')+'```';
274
340
  // }
@@ -282,10 +348,19 @@ function compileRewStuff(content, options) {
282
348
  }
283
349
  });
284
350
  }
351
+
352
+ if (token.type === "COMMENT" && token.value.startsWith('#include')) {
353
+ const includeContent = token.value.split(' ')[1] || '';
354
+ const filename = options.filename ? path.resolve(path.dirname(options.filename), includeContent) : includeContent;
355
+ if (existsSync(filename)) {
356
+ result += '\n'+ compileRewStuff(readFileSync(filename).toString(), {
357
+ ...options,
358
+ filename
359
+ });
360
+ }
361
+ }
285
362
  }
286
363
 
287
- // console.log(result)
288
-
289
364
  return result;
290
365
  }
291
366
 
@@ -298,7 +373,10 @@ const compileCivetStuff = (file, options) => {
298
373
  js: true
299
374
  };
300
375
 
301
- const prepared = compileRewStuff(file.content, options);
376
+ const prepared = compileRewStuff(file.content, {
377
+ filename: file.path,
378
+ ...options
379
+ });
302
380
  let compiled = options.async ? compileCivet(prepared, compileOptions) : wait(compileCivet, prepared, compileOptions);
303
381
 
304
382
  return {
@@ -107,7 +107,7 @@ module.exports.prepareContext = function (
107
107
 
108
108
  context.using = (name, ...params) => {
109
109
  if(name instanceof Usage.Group){
110
- params.unshift(name.g[1])
110
+ params.unshift(...name.g.slice(1));
111
111
  name = name.g[0];
112
112
  }
113
113
  if(USING_DEFAULT[name]){
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makano/rew",
3
- "version": "1.2.73",
3
+ "version": "1.2.75",
4
4
  "description": "A simple coffescript runtime and app manager",
5
5
  "main": "main.js",
6
6
  "directories": {
package/runtime.d.ts CHANGED
@@ -953,5 +953,5 @@ declare class Usage<T = () => void> {
953
953
  save: boolean;
954
954
  constructor(name: string, trigger: T, save: boolean);
955
955
  create(name: string, trigger: T, save: boolean): Usage;
956
- group<T = any[], U = any>(group: T, props?: U): { g: T, [key: string]: any }
956
+ group(...group: any[]): { g: T, with: (props: any) => { g: T, [key: string]: any }, [key: string]: any }
957
957
  }