grunt icon indicating copy to clipboard operation
grunt copied to clipboard

config.get('foo.bar.baz') does not process intermediate templates in path

Open cspotcode opened this issue 9 years ago • 4 comments

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

cspotcode avatar Jan 25 '16 21:01 cspotcode

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.

shama avatar Jan 26 '16 00:01 shama

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:

  1. On the root config object, get "foo". If it's a template string, expand it.*
  2. On the resulting value, get "bar". If it's a template string, expand it.*
  3. On the resulting value, get "baz". If it's a template string, expand it.*
  4. 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.

cspotcode avatar Jan 26 '16 01:01 cspotcode

Thanks for the failing test. That should work so reopening.

shama avatar Jan 26 '16 02:01 shama

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.

linoleum-js avatar Jun 19 '16 16:06 linoleum-js