Skip to content

Commit

Permalink
Update README + add directory overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
GZolla committed Nov 12, 2024
1 parent 4c6e600 commit 912c247
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 21 deletions.
56 changes: 42 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,58 @@ npm install eslint-config-brightspace

## Usage

Set the `extends` property in the `.eslintrc.json` file, replacing `<environment>` with the desired environment-specific config.
Shared configurations can be directly exported or included as part of a custom configuration in the `eslint.config..js` file.
```js
export { nodeConfig as default } from './index.js';
```

```js
import { nodeConfig } from 'eslint-config-brightspace';

export default [
...nodeConfig,
// Custom configuration
];
```

### Utils

Since the `--ext` flag is deprecated, include extensions by using the `addExtenstion` function from `utils.js`.
```js
import { nodeConfig } from 'eslint-config-brightspace';

export default addExtensions(nodeConfig, ['.js','.html']);
```

```json
{
"extends": "brightspace/<environment>"
}
To include different configurations for specific directories, use the `setDirectoryConfigs` function from `utils.js`. This replaces the [configuration hierarchy](https://eslint.org/docs/v8.x/use/configure/configuration-files#cascading-and-hierarchy) from `eslint8`. To use it, include the global configuration and specify the directory configurations, these will apply to all files inside the directory and recursively to any of its subdirectories.
```js
import { setDirectoryConfigs } from 'eslint-config-brightspace/utils.js';
import { litConfig, nodeConfig, testingConfig } from 'eslint-config-brightspace';

export default setDirectoryConfigs(
litConfig,
{
'test': testingConfig,
'test/cli': nodeConfig
}
);
```
Note that each set configuration will force all prior configurations to ignore it. For example, for the above configuration, `litConfig` will ignore any files in the `test` directory; and `testingConfig` will ignore any files in the `test/cli` directory.

### Environment Specific Configs

| Environment | Description |
|--|--|
| `browser-config` | use with code that runs in a browser |
| `lit-config` | use with [Lit](https://lit.dev/) projects |
| `testing-config` | use with [@brightspace-ui/testing](https://github.com/BrightspaceUI/testing) test code |
| `node-config` | use with [Node.js](https://nodejs.org) projects |
| `react-config` | use with [React](https://react.dev/) projects |
| `browserConfig` | use with code that runs in a browser |
| `litConfig` | use with [Lit](https://lit.dev/) projects |
| `testingConfig` | use with [@brightspace-ui/testing](https://github.com/BrightspaceUI/testing) test code |
| `nodeConfig` | use with [Node.js](https://nodejs.org) projects |
| `reactConfig` | use with [React](https://react.dev/) projects |

Example:

```json
{
"extends": "brightspace/lit-config"
}
```js
export { nodeConfig as default } from './index.js';
```

See the [eslint rules](https://eslint.org/docs/latest/rules/) for more details on rule configuration. See the [eslint shareable configs](https://eslint.org/docs/latest/extend/shareable-configs.html) for more details on creating configs.
Expand Down
4 changes: 1 addition & 3 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { nodeConfig } from './index.js';

export default nodeConfig;
export { nodeConfig as default } from './index.js';
42 changes: 38 additions & 4 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ts-check
const defaultGroups = {
'accessor-pairs': [{ 'accessorPair': true, 'sort': 'alphabetical' }],
'accessors': [{ 'kind': 'get', 'accessorPair': false, 'sort': 'alphabetical' }, { 'kind': 'set', 'accessorPair': false, 'sort': 'alphabetical' }],
Expand Down Expand Up @@ -35,11 +36,44 @@ export const getSortMemberRules = (order, groups) => {
};
};

export function overrideFiles(config, files) {
return config.map(c => ({ files, ...c }));
export function setDirectoryConfigs(globalConfig, directoryConfigs) {
const configs = globalConfig.map(c => ({ ...c }));
for (const dir in directoryConfigs) {
const pattern = `${dir}/**/*`;
for (const baseConfig of configs) {
if (!(baseConfig.ignores)) baseConfig.ignores = [];
baseConfig.ignores.push(pattern);
}

for (const dirConfig of directoryConfigs[dir]) {
const files = dirConfig.files ? dirConfig.files.map(f => {
if (f.startsWith('./')) return `${dir}${f.slice(1)}`;
if (f.startsWith('*')) return `${dir}${f}`;
return `${dir}/${f}`;
}) : [pattern];
configs.push({
...dirConfig,
files
});
}
}

return configs;
}

export function addExtensions(config, extensions) {
const files = extensions.map(ext => `**/*${ext}`);
return overrideFiles(config, files);
return config.map(c => {
const newFiles = [];
for (const f of (c.files ?? ['**/*'])) {
if (f.includes('.')) newFiles.push(f);
else {
for (const ext of extensions)
newFiles.push(`${f}${f.endsWith('*') ? '' : '*'}${ext}`);
}
}
return {
...c,
files: newFiles
};
});
}

0 comments on commit 912c247

Please sign in to comment.