google / closure-compiler

A JavaScript checker and optimizer.
https://developers.google.com/closure/compiler/
Apache License 2.0
7.39k stars 1.15k forks source link

TypeScript keyword in ES6 export list causes a parse error #2005

Closed pygy closed 6 years ago

pygy commented 8 years ago

type is a valid ES6 identifier yet the Closure compiler will refuse to compile files that try to use it as a variable.

It would be nice if the restriction was either lifted in ES6 input mode or at least documented. The docs currently say:

  • The Compiler only recognizes ECMAScript.

    ECMAScript 5 is the version of JavaScript supported almost everywhere. However the compiler also supports many of the features in ECMAScript 6. The compiler only supports official language features.

Searching for "TypeScript" doesn't return any page that mentions the language.

Edit: This bug may have a narrower scope than I initially thought... The problematic construct is

export {type};

I'll check later today if there are other issues but right now I must leave.

pygy commented 8 years ago

Using type in import statements is also problematic:

Using the closure-compiler-js (but AFAICT it is a core issue):

let compile = require("google-closure-compiler-js").compile;
console.log(compile({jsCode: [
  {
    path:"./foo.js",
    src:"const type = 5; export {type};"
  }, {
    src:"import {type} from './foo.js';"
  }
]});

gives

{ compiledCode: '',
  errors:
   [ { file: './foo.js',
       description: 'Parse error. cannot use keyword \'type\' here.',
       type: 'JSC_PARSE_ERROR',
       lineNo: 1,
       charNo: 24 },
     { file: 'Input_1',
       description: 'Parse error. \'as\' expected',
       type: 'JSC_PARSE_ERROR',
       lineNo: 1,
       charNo: 12 } ],
  warnings: [] }
dimvar commented 8 years ago

Repro in the debugger: https://closure-compiler-debugger.appspot.com/#input0%3Dconst%2520type%2520%253D%25205%253B%250Aexport%2520%257Btype%257D%253B%26input1%26conformanceConfig%26externs%26refasterjs-template%26includeDefaultExterns%3D1%26CHECK_SYMBOLS%3D1%26CHECK_TYPES%3D1%26CLOSURE_PASS%3D1%26LANG_IN_IS_ES6%3D1%26MISSING_PROPERTIES%3D1%26PRESERVE_TYPE_ANNOTATIONS%3D1%26PRETTY_PRINT%3D1%26TRANSPILE%3D1

AFAIU, the bug is that we should warn only when the input language is ECMASCRIPT6_TYPED.

pygy commented 8 years ago

@dimvar thanks for the repro, I'm not familiar with the Closure ecosystem. Here's an extended version with import {type} as well.

thheller commented 6 years ago

We just ran into this trying to compile d3 ES6 from npm.

import {namespace} from "d3-selection";

https://github.com/d3/d3-transition/blob/master/src/transition/attr.js#L2 https://github.com/d3/d3-transition/blob/master/src/transition/attrTween.js#L1

namespace is another of those TypeScript only keywords. https://github.com/google/closure-compiler/blob/424e341b5def61dac73ad39adb1b7c78ca9e9716/src/com/google/javascript/jscomp/parsing/parser/Keywords.java#L83-L87

jbouwman commented 6 years ago

Also hitting errors on TypeScript-specific keywords, in the same place as @thheller, when compiling D3. Example:

$ cat a.js

var namespace = 42;
export { namespace }

$ cat lint_6.json

{
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module"
  }
}

$ eslint --no-eslintrc --config lint_6.json a.js

  (no output)

$ java -jar target/closure-compiler-1.0-SNAPSHOT.jar a.js

a.js:3: ERROR - Parse error. cannot use keyword 'namespace' here.
export { namespace }
         ^
1 error(s), 0 warning(s)
MatrixFrog commented 6 years ago

It looks like this is fixed now? (If you put "Closes #xx" or "Fixes #xx" instead of "Addresses #xx" then Github should close it automatically.)

If there are other spots where we're treating TS keywords as though they were JS keywords, please file another bug for that.