Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
fix(Compiler): Emit all string literals as raw for generated injectors.
Browse files Browse the repository at this point in the history
I imagine this is a safe change, but I will run all internal tests just-in-case.

Closes #1591.
Closes #1592.

PiperOrigin-RevId: 209671699
  • Loading branch information
matanlurey committed Aug 22, 2018
1 parent 506238c commit af98b80
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
12 changes: 6 additions & 6 deletions _goldens/test/_files/di/generated_injectors.template.golden
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ class _Injector$doGenerate extends _i2.HierarchicalInjector {
_i3.Example _field0;

_i3.Example _getExample$0() => _field0 ??= new _i3.Example();
String _getString$1() => 'dynamic';
String _getString$2() => 'String';
String _getString$1() => r'dynamic';
String _getString$2() => r'String';
dynamic _getdynamic$3() => const _i3.Example();
dynamic _getdynamic$4() => _i3.instanceOfExamplePrime;
dynamic _getdynamic$5() => _i3.ExampleTheta.instance;
dynamic _getdynamic$6() => const [const _i3.Example(), _i3.instanceOfExamplePrime, _i3.ExampleTheta.instance];
dynamic _getdynamic$7() => const {'instanceOfExample': const _i3.Example(), _i3.instanceOfExamplePrime: 'instanceOfExamplePrime'};
dynamic _getdynamic$8() => 'ABC123';
String _getString$9() => 'A';
String _getString$10() => 'B';
dynamic _getdynamic$7() => const {r'instanceOfExample': const _i3.Example(), _i3.instanceOfExamplePrime: r'instanceOfExamplePrime'};
dynamic _getdynamic$8() => r'ABC123';
String _getString$9() => r'A';
String _getString$10() => r'B';
_i1.Injector _getInjector$11() => this;
@override
Object injectFromSelfOptional(Object token, [Object orElse = _i1.throwIfNotFound]) {
Expand Down
11 changes: 11 additions & 0 deletions _tests/test/di/generated/injector_use_value_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ void main() {
StringIdentityFn fn = injector.get(stringIdentityToken);
expect(fn('a'), 'a');
});

test('raw string', () {
String raw = injector.get(stringRawToken);
expect(raw, r'$5.00 USD');
});
});
}

Expand Down Expand Up @@ -106,6 +111,10 @@ const stringIdentityToken = OpaqueToken<StringIdentityFn>();
stringIdentityToken,
StaticClass.staticMethod,
),
ValueProvider.forToken(
stringRawToken,
r'$5.00 USD',
)
])
final InjectorFactory example = ng.example$Injector;

Expand Down Expand Up @@ -154,3 +163,5 @@ int topLevelMethod(int a) => a;
class StaticClass {
static String staticMethod(String a) => a;
}

const stringRawToken = OpaqueToken<String>('stringRawToken');
5 changes: 5 additions & 0 deletions angular/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
* [#1500][]: Configuring a provider with `FactoryProvider(Foo, null)` is now
a compile-time error, instead of a misleading runtime error.

* [#1592][]: Using `@GeneratedInjector` with a `ValueProvider` bound to a
`String` instance that expects a _raw_ string (i.e `r'$5.00'`) no longer
generates invalid code. Now _all_ strings are emitted as _raw_.

[#434]: https://github.com/dart-lang/angular/issues/434
[#880]: https://github.com/dart-lang/angular/issues/880
[#1500]: https://github.com/dart-lang/angular/issues/1500
Expand All @@ -75,6 +79,7 @@
[#1540]: https://github.com/dart-lang/angular/issues/1540
[#1558]: https://github.com/dart-lang/angular/issues/1558
[#1570]: https://github.com/dart-lang/angular/issues/1570
[#1592]: https://github.com/dart-lang/angular/pull/1592

### Other improvements

Expand Down
2 changes: 2 additions & 0 deletions angular_compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Catches an (invalid) `null` value of the function for `FactoryProvider`.

* Emits all strings for `@GeneratedInjector` as raw (`r'$5.00'`).

## 0.4.0

### New Features
Expand Down
5 changes: 5 additions & 0 deletions angular_compiler/lib/src/analyzer/di/injector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ class InjectorReader {
return _reviveMap(provider, reader.mapValue);
}
if (reader.isLiteral) {
// Always emit strings as raw in order to emit valid code.
// See https://github.com/dart-lang/angular/issues/1591.
if (reader.isString) {
return literalString(reader.literalValue, raw: true);
}
return literal(reader.literalValue);
}
final revive = reader.revive();
Expand Down

0 comments on commit af98b80

Please sign in to comment.