Skip to content

Commit

Permalink
fix: ensure strict mode when evaluating in JIT (#30122)
Browse files Browse the repository at this point in the history
PR Close #30122
  • Loading branch information
benlesh authored and alxhub committed May 8, 2019
1 parent dd8651d commit 192f108
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
12 changes: 12 additions & 0 deletions packages/compiler/src/output/output_jit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
31 changes: 29 additions & 2 deletions packages/compiler/test/output/output_jit_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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