grunt
grunt copied to clipboard
config.get('foo.bar.baz') does not process intermediate templates in path
When getting a config "path" (passing e.g. "foo.bar.baz" or ['foo', 'bar', 'baz'] to grunt.config.get) grunt doesn't process templates as it's stepping through the object graph.
grunt.initConfig({
getFooBarBaz: '<%= foo.bar.baz %>',
foo: '<%= _foo %>',
_foo: {
bar: '<%= _bar %>'
},
_bar: {
baz: 123
}
});
grunt.config('foo');
// returns { bar: { baz: 123 } }
grunt.config('foo.bar.baz');
grunt.config('getFooBarBaz')
// Both return { bar: { baz: 123 } } but I expected 123
Did you intend on having underscores in front of _foo and _bar?
We have pretty good test coverage for this example: https://github.com/gruntjs/grunt/blob/master/test/grunt/config_test.js Let us know if it's not working as expected though but I believe this isn't a valid issue.
Yes, the underscores are intentional. Big idea is that these should be the same:
grunt.config('foo').bar.baz
grunt.config('foo.bar.baz')
I added a failing test here: https://github.com/cspotcode/grunt/commit/912be5c40bd956695b781720f13254dfdc4cb16f
In me example from above, foo equals _foo, and _foo.bar equals _bar. For grunt.config.get('foo.bar.baz'), grunt should do something equivalent to the following:
- On the root config object, get "foo". If it's a template string, expand it.*
- On the resulting value, get "bar". If it's a template string, expand it.*
- On the resulting value, get "baz". If it's a template string, expand it.*
grunt.config.process()the result and return it.
* Only applies to template strings that might evaluate to a non-string (e.g. <%= other.value %> not foo<%= bar %>baz)
It makes sense that grunt should expand template strings as it goes, since that's what happens with grunt.config.get: templates are expanded recursively. If a template string evaluates to another object, then that objects is recursively traversed for more template strings.
Thanks for the failing test. That should work so reopening.
I create some patch and tests are passed now. Should I add more tests? Also, I don't like this https://github.com/gruntjs/grunt/pull/1524/files#diff-b682e275731164f4f9ea18bcee957dbaR52 I guess I should avoid it. Anyway, it works.