Skip to content
Permalink
Browse files

fix: ensure strict mode when evaluating in JIT (#30122)

PR Close #30122
  • Loading branch information...
benlesh authored and alxhub committed Apr 25, 2019
1 parent dd8651d commit 192f108b0f025b50541f372843a5c613e68940b1
Showing with 41 additions and 2 deletions.
  1. +12 −0 packages/compiler/src/output/output_jit.ts
  2. +29 −2 packages/compiler/test/output/output_jit_spec.ts
@@ -31,6 +31,13 @@ export class JitEvaluator {
createSourceMaps: boolean): {[key: string]: any} {
const converter = new JitEmitterVisitor(reflector);
const ctx = EmitterVisitorContext.createRoot();
// Ensure generated code is in strict mode
if (statements.length > 0 && !isUseStrictStatement(statements[0])) {
statements = [
o.literal('use strict').toStmt(),
...statements,
];
}
converter.visitAllStatements(statements, ctx);
converter.createReturnStmt(ctx);
return this.evaluateCode(sourceUrl, ctx, converter.getArgs(), createSourceMaps);
@@ -150,3 +157,8 @@ export class JitEmitterVisitor extends AbstractJsEmitterVisitor {
ctx.print(ast, this._evalArgNames[id]);
}
}


function isUseStrictStatement(statement: o.Statement): boolean {
return statement.isEquivalent(o.literal('use strict').toStmt());
}
@@ -8,7 +8,8 @@

import {EmitterVisitorContext} from '@angular/compiler/src/output/abstract_emitter';
import * as o from '@angular/compiler/src/output/output_ast';
import {JitEmitterVisitor} from '@angular/compiler/src/output/output_jit';
import {JitEmitterVisitor, JitEvaluator} from '@angular/compiler/src/output/output_jit';
import {R3JitReflector} from '@angular/compiler/src/render3/r3_jit';
import {JitReflector} from '@angular/platform-browser-dynamic/src/compiler_reflector';

const anotherModuleUrl = 'somePackage/someOtherPath';
@@ -32,5 +33,31 @@ const anotherModuleUrl = 'somePackage/someOtherPath';
expect(Object.keys(args).length).toBe(20);
});
});

it('should use strict mode', () => {
const evaluator = new JitEvaluator();
expect(() => {
evaluator.evaluateStatements(
'http://angular.io/something.ts',
[
// Set an undeclared variable
// foo = "bar";
o.variable('foo').equals(o.literal('bar')).toStmt(),
],
new R3JitReflector({}), false);
}).toThrowError();
});

it('should not add more than one strict mode statement if there is already one present', () => {
const converter = new JitEmitterVisitor(new JitReflector());
const ctx = EmitterVisitorContext.createRoot();
converter.visitAllStatements(
[
o.literal('use strict').toStmt(),
],
ctx);
const matches = ctx.toSource().match(/'use strict';/g) !;
expect(matches.length).toBe(1);
});
});
}
}

1 comment on commit 192f108

@IgorMinar

This comment has been minimized.

Copy link
Member

commented on 192f108 May 14, 2019

@benlesh can you please create better commit messages in the future. fix(core): ... with description of what this change does and why is this fix important. thanks!

Please sign in to comment.
You can’t perform that action at this time.