Skip to content

Commit

Permalink
Merge pull request #342 from Galooshi/remove-local-config
Browse files Browse the repository at this point in the history
Remove deprecated local config and snake_cased config handling
  • Loading branch information
lencioni authored Aug 22, 2016
2 parents a6f4c67 + 696ec75 commit cd5cdc8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 424 deletions.
49 changes: 0 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,55 +381,6 @@ The logfile is written to "importjs.log" in your operating system's default
directory for temporary files. You can get the path to the log file by running
`importjsd logpath`.

## Local configuration (*deprecated*)

_This way of configuring ImportJS is deprecated and will be removed in a future
release. See [Dynamic Configuration](#dynamic-configuration) for a better way
of accomplishing the same thing_.
_
You can dynamically apply configuration to different directory trees within your
project by turning the `.importjs.js` file into an array of configuration
objects. Each configuration specifies what part of the tree it applies to
through the `appliesTo` and `appliesFrom` options.

```javascript
[
{
appliesTo: 'app/**',
declarationKeyword: 'import',
},
{
appliesTo: 'app/**',
declarationKeyword: 'const',
},
{
appliesFrom: 'tests/**',
appliesTo: 'app/**',
declarationKeyword: 'var',
importFunction: 'mockRequire',
},
]
```

Use glob patterns supported by [minimatch][] for the `appliesTo` and
`appliesFrom` values. If any of the patterns are omitted, the default catch-all
"globstar" pattern (`**`) is used. The difference between the two patterns is
that `appliesTo` is matched with the file you are currently editing (relative
to the project root). The `appliesFrom` pattern is matched with the file you
are currently importing (also relative to the project root) will be used when
matching.

[minimatch]: https://github.com/isaacs/minimatch

Put more specific configuration at the bottom of the configuration file and the
default, catch-all configuration at the top.

When using `appliesFrom` only a subset of configurations are supported:

- `declarationKeyword`
- `importFunction`
- `stripFileExtensions`

## Dynamic configuration

Different sections of your application may have special importing needs. For
Expand Down
87 changes: 7 additions & 80 deletions lib/Configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import path from 'path';

import minimatch from 'minimatch';
import semver from 'semver';

import FileUtils from './FileUtils';
Expand Down Expand Up @@ -51,24 +50,8 @@ const MERGABLE_CONFIG_OPTIONS = [
'namedExports',
];

const RENAMED_CONFIGURATION_OPTIONS = {
applies_to: 'appliesTo',
applies_from: 'appliesFrom',
declaration_keyword: 'declarationKeyword',
named_exports: 'namedExports',
group_imports: 'groupImports',
ignore_package_prefixes: 'ignorePackagePrefixes',
import_dev_dependencies: 'importDevDependencies',
import_function: 'importFunction',
max_line_length: 'maxLineLength',
minimum_version: 'minimumVersion',
strip_file_extensions: 'stripFileExtensions',
};

const KNOWN_CONFIGURATION_OPTIONS = [
'aliases',
'appliesFrom',
'appliesTo',
'coreModules',
'declarationKeyword',
'environments',
Expand All @@ -92,25 +75,6 @@ const ENVIRONMENTS = {
meteor: meteorEnvironment,
};

function convertRenamedConfiguration(config: Object): Object {
const convertedConfig = Object.assign({}, config);
const messages = [];

Object.keys(RENAMED_CONFIGURATION_OPTIONS).forEach((oldKey: string) => {
const newKey = RENAMED_CONFIGURATION_OPTIONS[oldKey];
if (Object.prototype.hasOwnProperty.call(config, oldKey)) {
convertedConfig[newKey] = convertedConfig[oldKey];
delete convertedConfig[oldKey];

messages.push(
`Deprecated configuration: \`${oldKey}\` has changed to \`${newKey}\``
);
}
});

return { config: convertedConfig, messages };
}

function checkForUnknownConfiguration(config: Object): Array<string> {
const messages = [];

Expand Down Expand Up @@ -192,34 +156,18 @@ export default class Configuration {
this.messages.push(
`Unable to parse configuration file. Reason:\n${error.stack}`);
}
if (userConfig) {
if (Array.isArray(userConfig)) {
this.messages.push(
'Deprecated configuration: Array style configuration detected. ' +
'Local configuration using `appliesTo`/`appliesFrom` will be ' +
'removed in a future version. Use a JavaScript configuration file ' +
'and specify the configuration through functions (where needed) ' +
'instead.'
);
} else {
userConfig = [userConfig];
}

// Check for deprecated configuration and add messages if we find any.
// Clone array to prevent mutating the original.
Array.from(userConfig).reverse().forEach((config: Object) => {
const convertedConfig = convertRenamedConfiguration(config);
this.configs.push(convertedConfig.config);
this.messages.push(...convertedConfig.messages);
this.messages.push(...checkForUnknownConfiguration(convertedConfig.config));
});
if (userConfig) {
this.configs.push(userConfig);
this.messages.push(...checkForUnknownConfiguration(userConfig));

// Add configurations for the environments specified in the user config
// file.
(this.get('environments') || []).forEach((environment: string) => {
this.configs.push(ENVIRONMENTS[environment]);
});
}

this.configs.push(DEFAULT_CONFIG);

checkCurrentVersion(this.get('minimumVersion'));
Expand All @@ -235,31 +183,10 @@ export default class Configuration {
moduleName?: string,
} = {}
): any {
const applyingConfigs = this.configs.filter((config: Object): boolean => {
const {
appliesTo = '**',
appliesFrom = '**',
} = config;

if (!(key in config)) {
return false;
}

if (!minimatch(this.pathToCurrentFile,
normalizePath(appliesTo, this.workingDirectory))) {
// This configuration does not apply to the current file being edited.
return false;
}

if (!pathToImportedModule) {
return true;
}
const applyingConfigs = this.configs.filter((config: Object): boolean => (
Object.prototype.hasOwnProperty.call(config, key)
));

return minimatch(
normalizePath(pathToImportedModule, this.workingDirectory),
normalizePath(appliesFrom, this.workingDirectory)
);
});
return mergedValue(
applyingConfigs.map((config: Object): any => config[key]),
key,
Expand Down
Loading

0 comments on commit cd5cdc8

Please sign in to comment.