Loading ...
Sorry, an error occurred while loading the content.
 

Locate repeated strings with the Tree

Expand Messages
  • Deva Satyam
    I have JSLint integrated with my IDE and now, to the regular output, if there are no errors I added a list of literal strings and numbers and how often they
    Message 1 of 7 , Feb 13, 2011
      I have JSLint integrated with my IDE and now, to the regular output, if there are no errors I added a list of literal strings and numbers and how often they are repeated in the source. That makes them candidates to change them into 'constants' (only because they are all-uppercase variables) at the start of the module.

      To do that, if the JSLINT() run goes Ok, then I loop through the JSLINT.tree property and look for property 'arity' with values 'string' or 'number' and make a hash with the value of property 'value' (the contents of the string or number) holding a count of how many times I find it.

      If I find a property named 'first', 'second', 'block' or a string containing an integer value, then if the content is an object (which I believe it always is) I analize this object.

      So far the results match what I can find by counting them by hand. However, I would like to be sure those are the branches to explore and I'm not missing any, or if there is any easier way to do it.
    • Douglas Crockford
      third
      Message 2 of 7 , Feb 13, 2011
        third
      • Douglas Crockford
        else
        Message 3 of 7 , Feb 13, 2011
          else
        • Deva Satyam
          thanks
          Message 4 of 7 , Feb 13, 2011
            thanks
          • Deva Satyam
            If anyone is interested, the code to show the repeated literals follows. It is for Windows. If no errors are found it lists the constants found in increasing
            Message 5 of 7 , Feb 14, 2011
              If anyone is interested, the code to show the repeated literals follows.
              It is for Windows.
              If no errors are found it lists the constants found in increasing order
              of repetition (so the most frequent show at the bottom, which is the
              only part visible once the console screen scrolls).
              var fso = new ActiveXObject('Scripting.FileSystemObject'),
              f = fso.OpenTextFile(WScript.Arguments(0), 1);
              var clean = function (str) {
              return str ? (str.length <= 200 ? str.replace(/^\s*(\S*(\s+\S+)*)\s*$/,
              '$1') : '[Code Evidence Omitted: Greater than 200 chars]') : '';
              };
              if (JSLINT(f.ReadAll())) {
              var strs = {},
              re = /^(first|second|third|else|block|\d+)$/;
              var traverse = function (what) {
              var k,
              v = what.arity;
              if (v === 'string' || v === 'number') {
              v = what.value;
              if (v in strs) {
              strs[v] += 1;
              } else {
              strs[v] = 1;
              }
              }

              for (k in what) {
              if (what.hasOwnProperty(k)) {
              if (re.test(k)) {
              v = what[k];
              if (v) {
              traverse(v);
              }
              }
              }
              }
              };

              traverse(JSLINT.tree);
              WScript.Echo('Ok\n\nConstants:\n');
              var now = 2,
              next,
              k,
              v;
              while (now < Number.MAX_VALUE) {
              WScript.Echo(now + ':');
              next = Number.MAX_VALUE;
              for (k in strs) {
              if (strs.hasOwnProperty(k)) {
              v = strs[k];
              if (v === now) {
              WScript.Echo(' ' + k);
              } else {
              if (v > now) {
              next = Math.min(next, v);
              }
              }
              }
              }
              now = next;
              }
              } else {
              var i,
              len,
              l;
              for (i = 0, len = JSLINT.errors.length; i < len; i++) {
              l = JSLINT.errors[i];
              if (l !== null) {
              WScript.Echo('\n[' + l.line + ',' + l.character + ']: ' + l.reason +
              '\n' + clean(l.evidence));
              }
              }
              }
              f.close();



              [Non-text portions of this message have been removed]
            • Luke Page
              I quite like this. I ve modified it to be included in my visual studio extension as an option marked experimental. It s also occurred to me that 0 and 1 should
              Message 6 of 7 , Feb 16, 2011
                I quite like this. I've modified it to be included in my visual studio
                extension as an option marked experimental.

                It's also occurred to me that 0 and 1 should be allowed for a = a + 1 and >=
                1. Any other ok repeated constants?
                On 14 Feb 2011 22:34, "Deva Satyam" <satyam@...> wrote:


                [Non-text portions of this message have been removed]
              • Deva Satyam
                ... Though 0 and 1 might be repeated several times, as any other numerical constant might, though in lesser numbers, I don t see a good reason to replace them
                Message 7 of 7 , Feb 18, 2011
                  --- In jslint_com@yahoogroups.com, Luke Page <luke.a.page@...> wrote:
                  >
                  > I quite like this. I've modified it to be included in my visual studio
                  > extension as an option marked experimental.
                  >
                  > It's also occurred to me that 0 and 1 should be allowed for a = a + 1 and >=
                  > 1. Any other ok repeated constants?
                  > On 14 Feb 2011 22:34, "Deva Satyam" <satyam@...> wrote:
                  >
                  >
                  > [Non-text portions of this message have been removed]
                  >
                  Though 0 and 1 might be repeated several times, as any other numerical constant might, though in lesser numbers, I don't see a good reason to replace them by constants without a very good reason.

                  It makes sense that codes turn into constants, like HTTP_OK = 200, and that is because it is a numeric code which has a meaning unrelated to its value as a number.

                  There is no reason to have ONE = 1 or ZERO = 0 since not all 0s and 1s mean the same. Besides, the program would not compress as much, as it does when replacing repeated strings by constants and certainly not run faster. Nor is it helpful in maintenance since 1 is always 1, but ONE might have any value and they don't help in typing as Math.PI does
                Your message has been successfully submitted and would be delivered to recipients shortly.