angular icon indicating copy to clipboard operation
angular copied to clipboard

Unable to view AngularJS 2.0 Beta 1 in Internet Explorer: The use of a keyword for an identifier is invalid

Open kneuse opened this issue 10 years ago • 59 comments

I am unable to render AngularJS 2.0 Beta 1 in Internet Explore.

I have stripped down a page to as small as possible and can reproduce the issue where all I have as an empty app component that references an html page.

Downgrading to Angular JS 2.0 Beta 0 resolves the issue.

Below is the stack trace of the error:

HTML1300: Navigation occurred. File: localhost:8000 EXCEPTION: SyntaxError: The use of a keyword for an identifier is invalid EXCEPTION: SyntaxError: The use of a keyword for an identifier is invalid STACKTRACE: SyntaxError: The use of a keyword for an identifier is invalid at evalExpression (http://localhost:8000/lib/js/angular2.dev.js:452:5) at TemplateCompiler.prototype._createViewFactoryRuntime (http://localhost:8000/lib/js/angular2.dev.js:23969:9) at Anonymous function (http://localhost:8000/lib/js/angular2.dev.js:23924:13) at run (http://localhost:8000/lib/js/angular2-polyfills.js:138:7) at Anonymous function (http://localhost:8000/lib/js/angular2.dev.js:13247:15) at zoneBoundFn (http://localhost:8000/lib/js/angular2-polyfills.js:111:7) at lib$es6$promise$$internal$$tryCatch (http://localhost:8000/lib/js/angular2-polyfills.js:1511:9) at lib$es6$promise$$internal$$invokeCallback (http://localhost:8000/lib/js/angular2-polyfills.js:1523:9) at Anonymous function (http://localhost:8000/lib/js/angular2-polyfills.js:2071:13) at Anonymous function (http://localhost:8000/lib/js/angular2-polyfills.js:243:5)

-----async gap----- Error at _getStacktraceWithCaughtError (http://localhost:8000

SyntaxError: The use of a keyword for an identifier is invalid { [functions]: , proto: { }, description: "The use of a keyword for an identifier is invalid", message: "The use of a keyword for an identifier is invalid", name: "SyntaxError", number: -2146827240, stack: "SyntaxError: The use of a keyword for an identifier is invalid at evalExpression (http://localhost:8000/lib/js/angular2.dev.js:452:5) at TemplateCompiler.prototype._createViewFactoryRuntime (http://localhost:8000/lib/js/angular2.dev.js:23969:9) at Anonymous function (http://localhost:8000/lib/js/angular2.dev.js:23924:13) at run (http://localhost:8000/lib/js/angular2-polyfills.js:138:7) at Anonymous function (http://localhost:8000/lib/js/angular2.dev.js:13247:15) at zoneBoundFn (http://localhost:8000/lib/js/angular2-polyfills.js:111:7) at lib$es6$promise$$internal$$tryCatch (http://localhost:8000/lib/js/angular2-polyfills.js:1511:9) at lib$es6$promise$$internal$$invokeCallback (http://localhost:8000/lib/js/angular2-polyfills.js:1523:9) at Anonymous function (http://localhost:8000/lib/js/angular2-polyfills.js:2071:13) at Anonymous function (http://localhost:8000/lib/js/angular2-polyfills.js:243:5)" }

kneuse avatar Jan 15 '16 08:01 kneuse

The Internet Explorer version is 11 (11.0.9600.18125CO) with rendering engine set to Edge.

kneuse avatar Jan 15 '16 08:01 kneuse

Same for me. Here is the example in Plunker. It will works in IE 11/10 only if you change all "beta.1" to "beta.0" http://plnkr.co/edit/wqvzQ4Ma4F1avHqwsIUd?p=preview

VeselovAndrey avatar Jan 15 '16 08:01 VeselovAndrey

I am not sure how you found bug #213, I specifically tried "The use of a keyword for an identifier is invalid" as a search criteria and found nothing. Anyways, I updated the title just in-case the body of these bug reports are not very searchable.

kneuse avatar Jan 15 '16 14:01 kneuse

I see why I never found it, because I searched angular/angular rather than angular.

kneuse avatar Jan 15 '16 14:01 kneuse

Same with me. And if my root component contains another component in its view markup then I also get the "SyntaxError: Expected ';'" exception. This happens only in IE11 (ok in Chrome/FF/Edge) and it was not the issue in Beta.0. The "The use of a keyword for an identifier is invalid" happens in the 'evalExpression' function in its last line starting with 'return new (Function......', line 452 in angular2.dev.js.

alexi2014 avatar Jan 15 '16 16:01 alexi2014

I think the vars argument array is suspect. While debugging in IE 11 I see keys such as:

function RouterLink(_router, _location) {\r" and function RouterOutlet(_elementRef, _loader, _parentRouter, nameAttr) {\r

I don't know how or why they are there, but these keys are then used to populate the fnArgNames array which are used to create the new function arguments hence triggering the "The use of a keyword for an identifier is invalid", which I assume is because we are trying to name a function parameter function RouterLink(_router,_location){\r

dtabuenc avatar Jan 15 '16 22:01 dtabuenc

I further traced it down to directive name in the metadata being incorrect. For some reason in IE11 dirMmeta.type.name for a router directive is being set to function RouterLink(_router, _location) {\r" while in chrome it is simply RouterLink

dtabuenc avatar Jan 15 '16 22:01 dtabuenc

While trying to figure out why the compiler was creating incorrect directive metadata, the issue just suddenly fixed itself on my computer and everything works. For those who are still experiencing the issue, can you try fully clearing cache or using incognito mode to see if it still reproduces? Anybody have any insight as to why the type name of a directive would include the first line of the entire function declaration?

dtabuenc avatar Jan 15 '16 22:01 dtabuenc

Same here for me. I got just a simple view, and that fails on IE11.

eelwayadmin avatar Jan 16 '16 09:01 eelwayadmin

I tried your example from plunker, it works in Chrome but not in IE (11.0.9600.18097). Having the same issue "SyntaxError: The use of a keyword for an identifier is invalid" on "angular2.dev.js:452:5": return new (Function.bind.apply(Function, [void 0].concat(fnArgNames.concat(fnBody))))().apply(void 0, fnArgValues);

Clearing IE cache didn't solve the problem.

Using angular2 beta 0 I get the same error but at a different location.. angular2.dev.js:23969:9: return lang_1.evalExpression("viewFactory_" + compMeta.type.name, viewFactoryExpr, declarationsWithoutImports, mergeStringMaps([vars, change_detector_compiler_1.CHANGE_DETECTION_JIT_IMPORTS, proto_view_compiler_1.PROTO_VIEW_JIT_IMPORTS, view_compiler_1.VIEW_JIT_IMPORTS]));

cyberstorm256 avatar Jan 18 '16 13:01 cyberstorm256

Dup of #6450?

Tragetaschen avatar Jan 19 '16 12:01 Tragetaschen

+1. beta.0 is fine thought.

dlebedynskyi avatar Jan 19 '16 14:01 dlebedynskyi

+1 Downgrading to beta.0 fixed it for me as well

zybreak avatar Jan 20 '16 09:01 zybreak

Ok, mine is the dup :)

Let me just quote #6450 for reference:

In beta.1 (so 7ae23ad is likely the culprit) in IE11, the generated JavaScript from jitting contains syntax errors:

var appProtoView0_function SvgDefsImpl() {}0 = AppProtoView.create(resolvedMetadataCache, 1, [], {});
var renderType56_function SvgDefsImpl() {} = null;
var styles57_function SvgDefsImpl() {} = styles;

Instead of a sanitized name, function SvgDefsImpl() {} appears in the variable names on virtually every line.

Tragetaschen avatar Jan 21 '16 08:01 Tragetaschen

I found that in lang.ts, the stringify function returns the entire first line of the token when the token.name is undefined (IE does not support function.name) so the returned value is function functionName(arg) { Therefore the identifier is named function which is, of course, a keyword.

I have verified that the problem is fixed if I add the following:

if(typeof token === 'function') {
    return /^function\s+([\w\$]+)\s*\(/.exec(token.toString())[1];
}

to the stringify function, but it seems like it might be more appropriate to create an IE specific shim to address the missing name property.

sanbornc avatar Jan 22 '16 20:01 sanbornc

We have this shim in the repo as function.name is used in some tests: https://github.com/angular/angular/blob/master/modules/angular2/src/testing/shims_for_IE.js#L1-L14 Does it solve the issue if it is added to the application?

marclaval avatar Jan 23 '16 00:01 marclaval

@mlaval I have been using your IE shims file for my tests on sauce labs, and my builds are still broken on all versions of IE with Beta 1.

I'm not sure I can create a Plunkr of it, but you can see my Travis/Sauce sadness here: https://travis-ci.org/justindujardin/ng2-material/builds/103002845

Here's the (sparse) stack trace information:

LOG: SyntaxError{description: 'Expected ')'', number: -2146827282, stack: 'SyntaxError: Expected ')'
   at evalExpression (Unknown script code:427:5)
   at _createViewFactoryRuntime (Unknown script code:179:13)
   at Anonymous function (Unknown script code:140:21)
   at run (http://localhost:9876/base/node_modules/zone.js/dist/zone-microtask.js?2c40f1490bd97f0e177fa1ec2a3c3000d0f08bd4:120:7)
   at zoneBoundFn (http://localhost:9876/base/node_modules/zone.js/dist/zone-microtask.js?2c40f1490bd97f0e177fa1ec2a3c3000d0f08bd4:93:7)
   at lib$es6$promise$$internal$$tryCatch (http://localhost:9876/base/node_modules/zone.js/dist/zone-microtask.js?2c40f1490bd97f0e177fa1ec2a3c3000d0f08bd4:1493:9)
   at lib$es6$promise$$internal$$invokeCallback (http://localhost:9876/base/node_modules/zone.js/dist/zone-microtask.js?2c40f1490bd97f0e177fa1ec2a3c3000d0f08bd4:1505:9)
   at Anonymous function (http://localhost:9876/base/node_modules/zone.js/dist/zone-microtask.js?2c40f1490bd97f0e177fa1ec2a3c3000d0f08bd4:2053:13)
   at Anonymous function (http://localhost:9876/base/node_modules/zone.js/dist/zone-microtask.js?2c40f1490bd97f0e177fa1ec2a3c3000d0f08bd4:225:5)
   at run (http://localhost:9876/base/node_modules/zone.js/dist/zone-microtask.js?2c40f1490bd97f0e177fa1ec2a3c3000d0f08bd4:120:7)
   at zoneBoundFn (http://localhost:9876/base/node_modules/zone.js/dist/zone-microtask.js?2c40f1490bd97f0e177fa1ec2a3c3000d0f08bd4:93:7)
   at lib$es6$promise$asap$$flush (http://localhost:9876/base/node_modules/zone.js/dist/zone-microtask.js?2c40f1490bd97f0e177fa1ec2a3c3000d0f08bd4:1287:9)
   at Anonymous function (http://localhost:9876/base/node_modules/zone.js/dist/zone-microtask.js?2c40f1490bd97f0e177fa1ec2a3c3000d0f08bd4:475:11)
   at Anonymous function (http://localhost:9876/base/node_modules/zone.js/dist/zone-microtask.js?2c40f1490bd97f0e177fa1ec2a3c3000d0f08bd4:100:7)
   at run (http://localhost:9876/base/node_modules/zone.js/dist/zone-microtask.js?2c40f1490bd97f0e177fa1ec2a3c3000d0f08bd4:120:7)
   at zoneBoundFn (http://localhost:9876/base/node_modules/zone.js/dist/zone-microtask.js?2c40f1490bd97f0e177fa1ec2a3c3000d0f08bd4:93:7)'}

justindujardin avatar Jan 23 '16 21:01 justindujardin

Adding the function name shim in https://github.com/angular/angular/blob/master/modules/angular2/src/testing/shims_for_IE.js#L1-L14 Solves the issue for me.

sanbornc avatar Jan 25 '16 14:01 sanbornc

It seems using beta-0 for angular2.dev.js solve all problems with IE9-10-11.

BenoitDelrue avatar Jan 28 '16 08:01 BenoitDelrue

As @sanbornc commented, adding the following lines in a script tag or JS file before the loading of any other libraries will fix the problem for IE. (as a temporary fix for beta.1)

if (!Object.hasOwnProperty('name')) {
  Object.defineProperty(Function.prototype, 'name', {
    get: function() {
      var matches = this.toString().match(/^\s*function\s*(\S*)\s*\(/);
      var name = matches && matches.length > 1 ? matches[1] : "";
      Object.defineProperty(this, 'name', {value: name});
      return name;
    }
  });
}

BobbyTable avatar Jan 28 '16 10:01 BobbyTable