kungfooman / RuntimeTypeInspector.js

Checking JSDoc types at runtime for high-quality types - Trust is good, control is better.
MIT License
8 stars 0 forks source link

Fix `Comment` newline stringification #17

Closed kungfooman closed 12 months ago

kungfooman commented 12 months ago

Current diff (bunch of newline issues) in test/typechecking/comments unit test:

--- a/test/typechecking/comments-output.mjs
+++ b/test/typechecking/comments-output.mjs
@@ -1,3 +1,4 @@
+
 // start
 export const uniformTypeToName = [
   'bool',
@@ -5,18 +6,29 @@ export const uniformTypeToName = [
   'mat4',
   'sampler2D',
   'samplerCube',
-  '', // not directly handled: UNIFORMTYPE_FLOATARRAY
-  'sampler2DShadow',
+  '',
+   // not directly handled: UNIFORMTYPE_FLOATARRAY
+'sampler2DShadow',
   'samplerCubeShadow',
   'sampler3D',
-  '', // not directly handled: UNIFORMTYPE_VEC2ARRAY
-  '', // not directly handled: UNIFORMTYPE_VEC3ARRAY
-  '' // not directly handled: UNIFORMTYPE_VEC4ARRAY
-];
+  '',
+   // not directly handled: UNIFORMTYPE_VEC2ARRAY
+'',
+   // not directly handled: UNIFORMTYPE_VEC3ARRAY
+'' // not directly handled: UNIFORMTYPE_VEC4ARRAY
+
+]; /**
+ * A WebGL 1 device type.
+ *
+ * @type {string}
+ */
+
 /**
  * A WebGL 1 device type.
  *
  * @type {string}
  */
 export const DEVICETYPE_WEBGL1 = 'webgl1'; // 123
+
 // end

Edit:

So I tried to work it out, but I'm not implementing these end-of-line-singleline-comments, because:

Reasons: 1) Requires quite some complicated/error-prone extra code 2) Resulting in less performant code 3) Babel itself doesn't handle it either [1]

[1] https://babeljs.io/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=PTAEGcBcEMCdIFAFMAeAHA9vUBjDA7KUAV3wEsAzLAWwBUBPNJWjAOWmqVAF5QBtBKFAByAEYYMAG2EAaQSLL5Is-cOrRIAFhVDh4DmklJYAJgAiOkfuqHjAYWKikl4bNAhQ-DJFAATMrBIOJCS9KAAFtD4vka-AFygAKqsAJIAYgDyAEoAsrQAmgAKAKIA-mkAMhkAgrTVWVnV-arWtqZmAMqRvhgA7i6tRrAOTl3QPf1yuoPGAMwWUyJuHl4-_oHBoRFRMUjxSamZuQUlpQBqxXYm9Y3Nustgq34BQSFhkdGxCcnp2XlFZQudlmNyaqmE7ke3meGze20-e2-hz-J0Bl00oOaAF0ANwIYAAKgJggJoGqoAA6khRABxCqgACMfiQADcyDguJBGEgAHQkkmgAACXKYoAA3lBYIoAOYAXxJwGQ6CwPjwhB8ZmKZxSdmKqNKFOKACE6UzeMJetTpZIGcIcZDGSZZviwEhogggA&debug=false&forceAllTransforms=false&modules=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=false&presets=&prettier=false&targets=&version=7.23.2&externalPlugins=&assumptions=%7B%7D

However, I still need to deal with some other issues here, PR is under preparation.

WIP code that I had worked out for the case that I revisit this:

  ArrayExpression(node) {
    const {elements, extra, loc} = node;
    const e = !!extra?.trailingComma ? ',' : '';
    const sameLine = loc.start.line === loc.end.line;
    if (sameLine) {
      return '[' + this.mapToSource(elements).join(', ') + e + ']';
    }
    let out = '[';
    this.numSpaces++;
    const {spaces} = this;
    let needNewline = true;
    let n = elements.length;
    let first = true;
    for (let i = 0; i < n; i++) {
      const element = elements[i];
      const {leadingComments, trailingComments} = element;
      const nextLeadingComments = elements[i + 1]?.leadingComments;
      console.log("nextleadingComments", nextLeadingComments);
      if (first) {
        out += '\n';
      } else {
        if (!nextLeadingComments) {
          out += '\n';
        }
      }
      first = false;
      out += spaces;
      out += this.toSource(element);
      out += ',';
      // the comment on the previous line is actually
      // considered a "leading comment" of the next line.
      // so 
      if (leadingComments) {
        const comments = this.mapToSource(leadingComments).join('');
        out += ' ' + comments.trim();
        console.log("comments", comments, comments.length);
        //out += '\n';
        //out += spaces;
      } else {
        //out += '\n';
        //out += spaces;
      }
      //out += this.toSource(leadingComments[0]);
      //out += 'nComments=' + element.leadingComments?.length;
      //out += ',';
      continue;
    }
    this.numSpaces--;
    out += e;
    out += '\n' + this.spaces + ']';
    return out;
  }