Skip to content

Commit 192f108

Browse files
benleshalxhub
authored andcommittedMay 8, 2019
fix: ensure strict mode when evaluating in JIT (#30122)
PR Close #30122
1 parent dd8651d commit 192f108

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed
 

‎packages/compiler/src/output/output_jit.ts

+12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ export class JitEvaluator {
3131
createSourceMaps: boolean): {[key: string]: any} {
3232
const converter = new JitEmitterVisitor(reflector);
3333
const ctx = EmitterVisitorContext.createRoot();
34+
// Ensure generated code is in strict mode
35+
if (statements.length > 0 && !isUseStrictStatement(statements[0])) {
36+
statements = [
37+
o.literal('use strict').toStmt(),
38+
...statements,
39+
];
40+
}
3441
converter.visitAllStatements(statements, ctx);
3542
converter.createReturnStmt(ctx);
3643
return this.evaluateCode(sourceUrl, ctx, converter.getArgs(), createSourceMaps);
@@ -150,3 +157,8 @@ export class JitEmitterVisitor extends AbstractJsEmitterVisitor {
150157
ctx.print(ast, this._evalArgNames[id]);
151158
}
152159
}
160+
161+
162+
function isUseStrictStatement(statement: o.Statement): boolean {
163+
return statement.isEquivalent(o.literal('use strict').toStmt());
164+
}

‎packages/compiler/test/output/output_jit_spec.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

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

1415
const anotherModuleUrl = 'somePackage/someOtherPath';
@@ -32,5 +33,31 @@ const anotherModuleUrl = 'somePackage/someOtherPath';
3233
expect(Object.keys(args).length).toBe(20);
3334
});
3435
});
36+
37+
it('should use strict mode', () => {
38+
const evaluator = new JitEvaluator();
39+
expect(() => {
40+
evaluator.evaluateStatements(
41+
'http://angular.io/something.ts',
42+
[
43+
// Set an undeclared variable
44+
// foo = "bar";
45+
o.variable('foo').equals(o.literal('bar')).toStmt(),
46+
],
47+
new R3JitReflector({}), false);
48+
}).toThrowError();
49+
});
50+
51+
it('should not add more than one strict mode statement if there is already one present', () => {
52+
const converter = new JitEmitterVisitor(new JitReflector());
53+
const ctx = EmitterVisitorContext.createRoot();
54+
converter.visitAllStatements(
55+
[
56+
o.literal('use strict').toStmt(),
57+
],
58+
ctx);
59+
const matches = ctx.toSource().match(/'use strict';/g) !;
60+
expect(matches.length).toBe(1);
61+
});
3562
});
36-
}
63+
}

1 commit comments

Comments
 (1)

IgorMinar commented on May 14, 2019

@IgorMinar
Contributor

@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.