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

The composition of the extension options is broken #447

Open
axtgr opened this issue Mar 30, 2020 · 2 comments
Open

The composition of the extension options is broken #447

axtgr opened this issue Mar 30, 2020 · 2 comments

Comments

@axtgr
Copy link

axtgr commented Mar 30, 2020

Today I was trying to use XO to lint Svelte files. It wasn't working, so I dug a little. Turns out, the extension(s) option doesn't work as I expected, and I think there is more than one issue with it.

This is basically what the docs say about declaring file extensions: you can either pass one or more extension (singular) options via CLI, or specify an array as extensions (plural) in a config file. That's it.

This is what happens in reality:

  1. All options are merged into a single object, basically {...configOptions, ...cliOptions}. It means that both singular and plural aliases are allowed everywhere.

    xo/lib/options-manager.js

    Lines 240 to 244 in 2e39794

    const mergedOptions = normalizeOptions({
    ...xoOptions,
    nodeVersion: enginesOptions && enginesOptions.node && semver.validRange(enginesOptions.node),
    ...options
    });
  2. The options are normalized. The plural alias is preferred over the singular.
    let value = options[plural] || options[singular];
  3. The result is overridden by concatenating some default extensions with the original extensions option passed from CLI, which makes it the only working option.
    mergedOptions.extensions = DEFAULT_EXTENSION.concat(options.extensions || []);

And these are IMHO the problems of the current approach:

  1. The priority of options is weird. It's basically this:
    Plural CLI option > Plural config option > Singular CLI option > Singular config option
    I think CLI options should always override config options, which can be achieved by normalizing them separately before merging.
  2. Step 3 is probably a bug. I think it's meant to use the result of the first two steps instead of the original option. Can be easily fixed by changing the variable used.
  3. It's not very well documented, so it's not entirely clear what the intended algorithm was and if it even needs fixing.
@sindresorhus
Copy link
Member

Thanks for investigating this.


And these are IMHO the problems of the current approach:

  1. Agreed. CLI options should take priority.
  2. Yup. Looks like it.
  3. You're right. It should be better documented. The options have evolved over time.

@sindresorhus
Copy link
Member

Ideally, we should throw if the singular form is used in the config or plural is used in the CLI instead of just silently accepting it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants