New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pygy opened this Issue Sep 7, 2016 · 6 comments

Comments

Projects
None yet
5 participants
@pygy
Copy link

pygy commented Sep 7, 2016

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 pygy changed the title ES6 is not TypeScript TypeScript in ES6 export list causes a parse error Sep 7, 2016

@pygy pygy changed the title TypeScript in ES6 export list causes a parse error TypeScript keyword in ES6 export list causes a parse error Sep 7, 2016

@pygy

This comment has been minimized.

Copy link

pygy commented Sep 7, 2016

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 dimvar added the P3 label Sep 12, 2016

@pygy

This comment has been minimized.

Copy link

pygy commented Sep 12, 2016

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

@thheller

This comment has been minimized.

Copy link

thheller commented Oct 21, 2017

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.

// TypeScript
DECLARE("declare", TokenType.DECLARE),
TYPE("type", TokenType.TYPE),
MODULE("module", TokenType.MODULE), // Only accepted as alias for namespaces.
NAMESPACE("namespace", TokenType.NAMESPACE);

@jbouwman

This comment has been minimized.

Copy link
Contributor

jbouwman commented Mar 2, 2018

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)

lauraharker added a commit that referenced this issue Mar 9, 2018

Allow typescript keywords in import and export statements
Default behavior forbids the use of TypeScript keywords in import and export statements: modify scanner to emit IdentifierTokens when parseTypeSyntax is false.

Export decl parser coerces identifiers in exports to keywords: check there also.

Addresses #2005

Closes #2835

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=188511416

sebasjm pushed a commit to sebasjm/closure-compiler that referenced this issue Mar 11, 2018

Allow typescript keywords in import and export statements
Default behavior forbids the use of TypeScript keywords in import and export statements: modify scanner to emit IdentifierTokens when parseTypeSyntax is false.

Export decl parser coerces identifiers in exports to keywords: check there also.

Addresses google#2005

Closes google#2835

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=188511416
@MatrixFrog

This comment has been minimized.

Copy link
Contributor

MatrixFrog commented Apr 9, 2018

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.

@MatrixFrog MatrixFrog closed this Apr 9, 2018

@MatrixFrog MatrixFrog removed their assignment Apr 9, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment