rspivak / slimit

SlimIt - a JavaScript minifier/parser in Python
MIT License
550 stars 94 forks source link

slimit does not parse javascript "const" expression #74

Open belonesox opened 9 years ago

belonesox commented 9 years ago

Try to parse ««« const theconst = 1; »»» Slimit fails with

SyntaxError: Unexpected token (CONST, u'const') at 1:1 between LexToken(LINE_TERMINATOR,u'\n',1,0) and LexToken(ID,u'theconst',1,7)

Is it bug or a feature?

fflexo commented 7 years ago

Although the token was recognised by the lexer no grammar rules actually used it. I've made a patch that adds some, basically assuming that syntactically it works like var except the initialiser is mandatory and it can't be used for iteration.

metatoaster commented 7 years ago

Also const is an ES6 concept, which the parser really is only ES5 so a separate ES6 lexer/parser should be created and used instead.

Luke-SF commented 7 years ago

It works in every browser back to IE11... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

I'm surprised this is not working, why not consider it as similar to a "var" statement?

shamoons commented 5 years ago

Any update on const?

shamoons commented 5 years ago
const preFormattedBlockNames = {
  'api-projects': 'API Projects',
  'basic-css': 'Basic CSS',
  'basic-html-and-html5': 'Basic HTML and HTML5',
  'css-flexbox': 'CSS Flexbox',
  'css-grid': 'CSS Grid',
  devops: 'DevOps',
  es6: 'ES6',
  'information-security-with-helmetjs': 'Information Security with HelmetJS',
  jquery: 'jQuery',
  'json-apis-and-ajax': 'JSON APIs and Ajax',
  'mongodb-and-mongoose': 'MongoDB and Mongoose',
  'the-dom': 'The DOM',
  'apis-and-microservices': 'APIs and Microservices',
  'apis-and-microservices-projects': 'APIs and Microservices Projects'
};

const noFormatting = ['and', 'for', 'of', 'the', 'up', 'with'];

exports.blockNameify = function blockNameify(phrase) {
  const preFormatted = preFormattedBlockNames[phrase] || '';
  if (preFormatted) {
    return preFormatted;
  }
  return phrase
    .split('-')
    .map(word => {
      if (noFormatting.indexOf(word) !== -1) {
        return word;
      }
      if (word === 'javascript') {
        return 'JavaScript';
      }
      return word.charAt(0).toUpperCase() + word.slice(1);
    })
    .join(' ');
};

Error:  Unexpected token (CONST, 'const') at 1:0 between None and LexToken(ID,'preFormattedBlockNames',1,6)