Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cspotcode opened this issue Jan 25, 2016 · 4 comments
Open

Comments

@cspotcode
Copy link

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
@shama
Copy link
Member

shama commented Jan 26, 2016

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 shama closed this as completed Jan 26, 2016
cspotcode added a commit to cspotcode/grunt that referenced this issue Jan 26, 2016
@cspotcode
Copy link
Author

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: cspotcode@912be5c

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.

@shama
Copy link
Member

shama commented Jan 26, 2016

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

@linoleum-js
Copy link

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 added a commit to linoleum-js/grunt that referenced this issue Jun 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants