Skip to content

Commit

Permalink
Allow @ngInject to be used in ES6 modules
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=158319858
  • Loading branch information
tbreisacher authored and brad4d committed Jun 8, 2017
1 parent d936ac8 commit c7ce36b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/com/google/javascript/jscomp/AngularPass.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ private void addNode(Node n, NodeTraversal t) {
return;
}
// report an error if the function declaration did not take place in a block or global scope
if (!target.getParent().isScript() && !target.getParent().isNormalBlock()) {
if (!target.getParent().isScript()
&& !target.getParent().isNormalBlock()
&& !target.getParent().isModuleBody()) {
compiler.report(t.makeError(n, INJECT_IN_NON_GLOBAL_OR_BLOCK_ERROR));
return;
}
Expand Down
5 changes: 5 additions & 0 deletions src/com/google/javascript/jscomp/DefaultPassConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,11 @@ protected FeatureSet featureSet() {
protected HotSwapCompilerPass create(AbstractCompiler compiler) {
return new AngularPass(compiler);
}

@Override
public FeatureSet featureSet() {
return ES8_MODULES;
}
};

/**
Expand Down
13 changes: 13 additions & 0 deletions test/com/google/javascript/jscomp/AngularPassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,19 @@ public void testInGoogModule() {
"/** @public */ fn['$inject'] = ['a', 'b'];"));
}

public void testInEsModule() {
String js = LINE_JOINER.join(
"import {Foo} from './foo';",
"",
"class Bar extends Foo { /** @ngInject */ constructor(x, y) {} }");
test(
js,
LINE_JOINER.join(
js,
"/** @public */",
"Bar['$inject'] = ['x', 'y'];"));
}

public void testInGoogScope() {
enableRewriteClosureCode();
test(
Expand Down
21 changes: 21 additions & 0 deletions test/com/google/javascript/jscomp/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,27 @@ public void testAngularPassOn() {
"var b = function f(a, b, c) {}; b['$inject']=['a', 'b', 'c']");
}

public void testAngularPassOn_transpile() {
CompilerOptions options = createCompilerOptions();
options.setLanguageIn(LanguageMode.ECMASCRIPT_2015);
options.setLanguageOut(LanguageMode.ECMASCRIPT5);
options.angularPass = true;
test(options,
"class C { /** @ngInject */ constructor(x) {} }",
"var C = function(x){}; C['$inject'] = ['x'];");
}

public void testAngularPassOn_Es6Out() {
CompilerOptions options = createCompilerOptions();
options.setLanguageIn(LanguageMode.ECMASCRIPT_2015);
options.setLanguageOut(LanguageMode.ECMASCRIPT_2015);
options.setSkipTranspilationAndCrash(true);
options.angularPass = true;
test(options,
"class C { /** @ngInject */ constructor(x) {} }",
"class C { constructor(x){} } C['$inject'] = ['x'];");
}

public void testExportTestFunctionsOff() {
testSame(createCompilerOptions(), "function testFoo() {}");
}
Expand Down

0 comments on commit c7ce36b

Please sign in to comment.