@mojir/lits 1.2.0 → 1.2.1-alpha.0

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/dist/cli/cli.js CHANGED
@@ -92,7 +92,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
92
92
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
93
93
  };
94
94
 
95
- var version = "1.2.0";
95
+ var version = "1.2.1-alpha.0";
96
96
 
97
97
  var AstNodeType;
98
98
  (function (AstNodeType) {
@@ -8701,163 +8701,6 @@ var Lits = /** @class */ (function () {
8701
8701
  return Lits;
8702
8702
  }());
8703
8703
 
8704
- function runTest(_a) {
8705
- var filePath = _a.testPath, testNamePattern = _a.testNamePattern;
8706
- var includedFilePaths = getIncludedFilePaths(filePath);
8707
- var testResult = {
8708
- tap: 'TAP version 13\n',
8709
- success: true,
8710
- };
8711
- try {
8712
- var testChunks = getTestChunks(filePath);
8713
- testResult.tap += "1..".concat(testChunks.length, "\n");
8714
- testChunks.forEach(function (testChunkProgram, index) {
8715
- var testNumber = index + 1;
8716
- if (testNamePattern && !testNamePattern.test(testChunkProgram.name)) {
8717
- testResult.tap += "ok ".concat(testNumber, " ").concat(testChunkProgram.name, " # skip - Not matching testNamePattern ").concat(testNamePattern, "\n");
8718
- }
8719
- else if (testChunkProgram.directive === 'SKIP') {
8720
- testResult.tap += "ok ".concat(testNumber, " ").concat(testChunkProgram.name, " # skip\n");
8721
- }
8722
- else {
8723
- try {
8724
- var lits = new Lits({ debug: true });
8725
- var contexts = getContexts(includedFilePaths, lits);
8726
- lits.run(testChunkProgram.program, {
8727
- contexts: contexts,
8728
- filePath: filePath,
8729
- });
8730
- testResult.tap += "ok ".concat(testNumber, " ").concat(testChunkProgram.name, "\n");
8731
- }
8732
- catch (error) {
8733
- testResult.success = false;
8734
- testResult.tap += "not ok ".concat(testNumber, " ").concat(testChunkProgram.name).concat(getErrorYaml(error));
8735
- }
8736
- }
8737
- });
8738
- }
8739
- catch (error) {
8740
- testResult.tap += "Bail out! ".concat(getErrorMessage$1(error), "\n");
8741
- testResult.success = false;
8742
- }
8743
- return testResult;
8744
- }
8745
- function readLitsFile(litsPath) {
8746
- if (!litsPath.endsWith('.lits'))
8747
- throw new Error("Expected .lits file, got ".concat(litsPath));
8748
- return fs.readFileSync(litsPath, { encoding: 'utf-8' });
8749
- }
8750
- function getContexts(includedFilePaths, lits) {
8751
- return includedFilePaths.reduce(function (acc, filePath) {
8752
- var fileContent = readLitsFile(filePath);
8753
- acc.push(lits.context(fileContent, { filePath: filePath, contexts: acc }));
8754
- return acc;
8755
- }, []);
8756
- }
8757
- function getIncludedFilePaths(absoluteFilePath) {
8758
- var result = [];
8759
- getIncludesRecursively(absoluteFilePath, result);
8760
- return result.reduce(function (acc, entry) {
8761
- if (!acc.includes(entry))
8762
- acc.push(entry);
8763
- return acc;
8764
- }, []);
8765
- function getIncludesRecursively(filePath, includedFilePaths) {
8766
- var includeFilePaths = readIncludeDirectives(filePath);
8767
- includeFilePaths.forEach(function (includeFilePath) {
8768
- getIncludesRecursively(includeFilePath, includedFilePaths);
8769
- includedFilePaths.push(includeFilePath);
8770
- });
8771
- }
8772
- }
8773
- function readIncludeDirectives(filePath) {
8774
- var fileContent = readLitsFile(filePath);
8775
- var dirname = path.dirname(filePath);
8776
- var okToInclude = true;
8777
- return fileContent.split('\n').reduce(function (acc, line) {
8778
- var includeMatch = line.match(/^\s*;+\s*@include\s*(\S+)\s*$/);
8779
- if (includeMatch) {
8780
- if (!okToInclude)
8781
- throw new Error("@include must be in the beginning of file: ".concat(filePath, ":").concat(line + 1));
8782
- var relativeFilePath = includeMatch[1];
8783
- acc.push(path.resolve(dirname, relativeFilePath));
8784
- }
8785
- if (!line.match(/^\s*;.*$/))
8786
- okToInclude = false;
8787
- return acc;
8788
- }, []);
8789
- }
8790
- // Splitting test file based on @test annotations
8791
- function getTestChunks(testPath) {
8792
- var testProgram = readLitsFile(testPath);
8793
- var currentTest;
8794
- var setupCode = '';
8795
- return testProgram.split('\n').reduce(function (result, line, index) {
8796
- var _a;
8797
- var currentLineNbr = index + 1;
8798
- // eslint-disable-next-line regexp/no-super-linear-backtracking
8799
- var testNameAnnotationMatch = line.match(/^\s*;+\s*@(?:(skip)-)?test\s*(.*)$/);
8800
- if (testNameAnnotationMatch) {
8801
- var directive = ((_a = testNameAnnotationMatch[1]) !== null && _a !== void 0 ? _a : '').toUpperCase();
8802
- var testName_1 = testNameAnnotationMatch[2];
8803
- if (!testName_1)
8804
- throw new Error("Missing test name on line ".concat(currentLineNbr));
8805
- if (result.find(function (chunk) { return chunk.name === testName_1; }))
8806
- throw new Error("Duplicate test name ".concat(testName_1));
8807
- currentTest = {
8808
- directive: (directive || null),
8809
- name: testName_1,
8810
- // Adding new-lines to make lits debug information report correct rows
8811
- program: setupCode + __spreadArray([], __read(Array(currentLineNbr + 2 - setupCode.split('\n').length).keys()), false).map(function () { return ''; }).join('\n'),
8812
- };
8813
- result.push(currentTest);
8814
- return result;
8815
- }
8816
- if (!currentTest)
8817
- setupCode += "".concat(line, "\n");
8818
- else
8819
- currentTest.program += "".concat(line, "\n");
8820
- return result;
8821
- }, []);
8822
- }
8823
- function getErrorYaml(error) {
8824
- var message = getErrorMessage$1(error);
8825
- /* v8 ignore next 7 */
8826
- if (!isAbstractLitsError(error)) {
8827
- return "\n ---\n message: ".concat(JSON.stringify(message), "\n ...\n");
8828
- }
8829
- var sourceCodeInfo = error.sourceCodeInfo;
8830
- /* v8 ignore next 8 */
8831
- if (!sourceCodeInfo || typeof sourceCodeInfo === 'string') {
8832
- return "\n ---\n message: ".concat(JSON.stringify(message), "\n error: ").concat(JSON.stringify(error.name), "\n ...\n");
8833
- }
8834
- var formattedMessage = message.includes('\n')
8835
- ? "|\n ".concat(message.split(/\r?\n/).join('\n '))
8836
- : JSON.stringify(message);
8837
- return "\n ---\n error: ".concat(JSON.stringify(error.name), "\n message: ").concat(formattedMessage, "\n location: ").concat(JSON.stringify(getLocation(sourceCodeInfo)), "\n code:\n - \"").concat(sourceCodeInfo.code, "\"\n - \"").concat(getCodeMarker(sourceCodeInfo), "\"\n ...\n");
8838
- }
8839
- function getLocation(sourceCodeInfo) {
8840
- var terms = [];
8841
- if (sourceCodeInfo.filePath)
8842
- terms.push(sourceCodeInfo.filePath);
8843
- if (sourceCodeInfo.position) {
8844
- terms.push("".concat(sourceCodeInfo.position.line));
8845
- terms.push("".concat(sourceCodeInfo.position.column));
8846
- }
8847
- return terms.join(':');
8848
- }
8849
- function getErrorMessage$1(error) {
8850
- if (!isAbstractLitsError(error)) {
8851
- // error should always be an Error (other cases is just for kicks)
8852
- /* v8 ignore next 1 */
8853
- return typeof error === 'string' ? error : error instanceof Error ? error.message : 'Unknown error';
8854
- }
8855
- return error.shortMessage;
8856
- }
8857
- function isAbstractLitsError(error) {
8858
- return error instanceof LitsError;
8859
- }
8860
-
8861
8704
  var collectionReference = {
8862
8705
  'count': {
8863
8706
  title: 'count',
@@ -15317,6 +15160,163 @@ Object.values(apiReference).forEach(function (ref) {
15317
15160
  ref.title = ref.title.replace(/"/g, '"');
15318
15161
  });
15319
15162
 
15163
+ function runTest(_a) {
15164
+ var filePath = _a.testPath, testNamePattern = _a.testNamePattern;
15165
+ var includedFilePaths = getIncludedFilePaths(filePath);
15166
+ var testResult = {
15167
+ tap: 'TAP version 13\n',
15168
+ success: true,
15169
+ };
15170
+ try {
15171
+ var testChunks = getTestChunks(filePath);
15172
+ testResult.tap += "1..".concat(testChunks.length, "\n");
15173
+ testChunks.forEach(function (testChunkProgram, index) {
15174
+ var testNumber = index + 1;
15175
+ if (testNamePattern && !testNamePattern.test(testChunkProgram.name)) {
15176
+ testResult.tap += "ok ".concat(testNumber, " ").concat(testChunkProgram.name, " # skip - Not matching testNamePattern ").concat(testNamePattern, "\n");
15177
+ }
15178
+ else if (testChunkProgram.directive === 'SKIP') {
15179
+ testResult.tap += "ok ".concat(testNumber, " ").concat(testChunkProgram.name, " # skip\n");
15180
+ }
15181
+ else {
15182
+ try {
15183
+ var lits = new Lits({ debug: true });
15184
+ var contexts = getContexts(includedFilePaths, lits);
15185
+ lits.run(testChunkProgram.program, {
15186
+ contexts: contexts,
15187
+ filePath: filePath,
15188
+ });
15189
+ testResult.tap += "ok ".concat(testNumber, " ").concat(testChunkProgram.name, "\n");
15190
+ }
15191
+ catch (error) {
15192
+ testResult.success = false;
15193
+ testResult.tap += "not ok ".concat(testNumber, " ").concat(testChunkProgram.name).concat(getErrorYaml(error));
15194
+ }
15195
+ }
15196
+ });
15197
+ }
15198
+ catch (error) {
15199
+ testResult.tap += "Bail out! ".concat(getErrorMessage$1(error), "\n");
15200
+ testResult.success = false;
15201
+ }
15202
+ return testResult;
15203
+ }
15204
+ function readLitsFile(litsPath) {
15205
+ if (!litsPath.endsWith('.lits'))
15206
+ throw new Error("Expected .lits file, got ".concat(litsPath));
15207
+ return fs.readFileSync(litsPath, { encoding: 'utf-8' });
15208
+ }
15209
+ function getContexts(includedFilePaths, lits) {
15210
+ return includedFilePaths.reduce(function (acc, filePath) {
15211
+ var fileContent = readLitsFile(filePath);
15212
+ acc.push(lits.context(fileContent, { filePath: filePath, contexts: acc }));
15213
+ return acc;
15214
+ }, []);
15215
+ }
15216
+ function getIncludedFilePaths(absoluteFilePath) {
15217
+ var result = [];
15218
+ getIncludesRecursively(absoluteFilePath, result);
15219
+ return result.reduce(function (acc, entry) {
15220
+ if (!acc.includes(entry))
15221
+ acc.push(entry);
15222
+ return acc;
15223
+ }, []);
15224
+ function getIncludesRecursively(filePath, includedFilePaths) {
15225
+ var includeFilePaths = readIncludeDirectives(filePath);
15226
+ includeFilePaths.forEach(function (includeFilePath) {
15227
+ getIncludesRecursively(includeFilePath, includedFilePaths);
15228
+ includedFilePaths.push(includeFilePath);
15229
+ });
15230
+ }
15231
+ }
15232
+ function readIncludeDirectives(filePath) {
15233
+ var fileContent = readLitsFile(filePath);
15234
+ var dirname = path.dirname(filePath);
15235
+ var okToInclude = true;
15236
+ return fileContent.split('\n').reduce(function (acc, line) {
15237
+ var includeMatch = line.match(/^\s*;+\s*@include\s*(\S+)\s*$/);
15238
+ if (includeMatch) {
15239
+ if (!okToInclude)
15240
+ throw new Error("@include must be in the beginning of file: ".concat(filePath, ":").concat(line + 1));
15241
+ var relativeFilePath = includeMatch[1];
15242
+ acc.push(path.resolve(dirname, relativeFilePath));
15243
+ }
15244
+ if (!line.match(/^\s*;.*$/))
15245
+ okToInclude = false;
15246
+ return acc;
15247
+ }, []);
15248
+ }
15249
+ // Splitting test file based on @test annotations
15250
+ function getTestChunks(testPath) {
15251
+ var testProgram = readLitsFile(testPath);
15252
+ var currentTest;
15253
+ var setupCode = '';
15254
+ return testProgram.split('\n').reduce(function (result, line, index) {
15255
+ var _a;
15256
+ var currentLineNbr = index + 1;
15257
+ // eslint-disable-next-line regexp/no-super-linear-backtracking
15258
+ var testNameAnnotationMatch = line.match(/^\s*;+\s*@(?:(skip)-)?test\s*(.*)$/);
15259
+ if (testNameAnnotationMatch) {
15260
+ var directive = ((_a = testNameAnnotationMatch[1]) !== null && _a !== void 0 ? _a : '').toUpperCase();
15261
+ var testName_1 = testNameAnnotationMatch[2];
15262
+ if (!testName_1)
15263
+ throw new Error("Missing test name on line ".concat(currentLineNbr));
15264
+ if (result.find(function (chunk) { return chunk.name === testName_1; }))
15265
+ throw new Error("Duplicate test name ".concat(testName_1));
15266
+ currentTest = {
15267
+ directive: (directive || null),
15268
+ name: testName_1,
15269
+ // Adding new-lines to make lits debug information report correct rows
15270
+ program: setupCode + __spreadArray([], __read(Array(currentLineNbr + 2 - setupCode.split('\n').length).keys()), false).map(function () { return ''; }).join('\n'),
15271
+ };
15272
+ result.push(currentTest);
15273
+ return result;
15274
+ }
15275
+ if (!currentTest)
15276
+ setupCode += "".concat(line, "\n");
15277
+ else
15278
+ currentTest.program += "".concat(line, "\n");
15279
+ return result;
15280
+ }, []);
15281
+ }
15282
+ function getErrorYaml(error) {
15283
+ var message = getErrorMessage$1(error);
15284
+ /* v8 ignore next 7 */
15285
+ if (!isAbstractLitsError(error)) {
15286
+ return "\n ---\n message: ".concat(JSON.stringify(message), "\n ...\n");
15287
+ }
15288
+ var sourceCodeInfo = error.sourceCodeInfo;
15289
+ /* v8 ignore next 8 */
15290
+ if (!sourceCodeInfo || typeof sourceCodeInfo === 'string') {
15291
+ return "\n ---\n message: ".concat(JSON.stringify(message), "\n error: ").concat(JSON.stringify(error.name), "\n ...\n");
15292
+ }
15293
+ var formattedMessage = message.includes('\n')
15294
+ ? "|\n ".concat(message.split(/\r?\n/).join('\n '))
15295
+ : JSON.stringify(message);
15296
+ return "\n ---\n error: ".concat(JSON.stringify(error.name), "\n message: ").concat(formattedMessage, "\n location: ").concat(JSON.stringify(getLocation(sourceCodeInfo)), "\n code:\n - \"").concat(sourceCodeInfo.code, "\"\n - \"").concat(getCodeMarker(sourceCodeInfo), "\"\n ...\n");
15297
+ }
15298
+ function getLocation(sourceCodeInfo) {
15299
+ var terms = [];
15300
+ if (sourceCodeInfo.filePath)
15301
+ terms.push(sourceCodeInfo.filePath);
15302
+ if (sourceCodeInfo.position) {
15303
+ terms.push("".concat(sourceCodeInfo.position.line));
15304
+ terms.push("".concat(sourceCodeInfo.position.column));
15305
+ }
15306
+ return terms.join(':');
15307
+ }
15308
+ function getErrorMessage$1(error) {
15309
+ if (!isAbstractLitsError(error)) {
15310
+ // error should always be an Error (other cases is just for kicks)
15311
+ /* v8 ignore next 1 */
15312
+ return typeof error === 'string' ? error : error instanceof Error ? error.message : 'Unknown error';
15313
+ }
15314
+ return error.shortMessage;
15315
+ }
15316
+ function isAbstractLitsError(error) {
15317
+ return error instanceof LitsError;
15318
+ }
15319
+
15320
15320
  function stringifyValue(value, html) {
15321
15321
  var _a;
15322
15322
  var gt = '>';
@@ -10,3 +10,4 @@ export { Lits } from './Lits/Lits';
10
10
  export { type LitsError, isLitsError } from './errors';
11
11
  export type { LitsParams, LitsRuntimeInfo, LazyValue } from './Lits/Lits';
12
12
  export { createNativeJsFunction } from './utils';
13
+ export { apiReference } from '../reference';