Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
resolveConfig
to Node.js API (#5734)
- Loading branch information
1 parent
315d3b6
commit 981ac5c
Showing
7 changed files
with
252 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const pluginWarnAboutFoo = require('./fixtures/plugin-warn-about-foo'); | ||
const replaceBackslashes = require('../testUtils/replaceBackslashes'); | ||
const stylelint = require('..'); | ||
|
||
describe('resolveConfig', () => { | ||
it('should resolve to undefined without a path', () => { | ||
return expect(stylelint.resolveConfig()).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('should use getConfigForFile to retrieve the config', async () => { | ||
const filepath = replaceBackslashes( | ||
path.join(__dirname, 'fixtures/getConfigForFile/a/b/foo.css'), | ||
); | ||
|
||
const config = await stylelint.resolveConfig(filepath); | ||
|
||
expect(config).toStrictEqual({ | ||
plugins: [path.join(__dirname, '/fixtures/plugin-warn-about-foo.js')], | ||
rules: { | ||
'block-no-empty': [true], | ||
'plugin/warn-about-foo': ['always'], | ||
}, | ||
pluginFunctions: { | ||
'plugin/warn-about-foo': pluginWarnAboutFoo.rule, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should apply config overrides', async () => { | ||
const filepath = replaceBackslashes( | ||
path.join(__dirname, 'fixtures/config-overrides/testPrintConfig/style.css'), | ||
); | ||
|
||
const config = await stylelint.resolveConfig(filepath); | ||
|
||
expect(config).toStrictEqual({ | ||
rules: { | ||
'block-no-empty': [true], | ||
'color-named': ['never'], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should respect the passed config', async () => { | ||
const filepath = replaceBackslashes( | ||
path.join(__dirname, 'fixtures/config-overrides/testPrintConfig/style.css'), | ||
); | ||
|
||
const config = await stylelint.resolveConfig(filepath, { | ||
config: { | ||
rules: { | ||
'color-no-invalid-hex': true, | ||
'color-no-named': 'always', | ||
}, | ||
}, | ||
}); | ||
|
||
expect(config).toStrictEqual({ | ||
rules: { | ||
'color-no-invalid-hex': [true], | ||
'color-no-named': [], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should use the passed config file path', async () => { | ||
const filepath = replaceBackslashes( | ||
path.join(__dirname, 'fixtures/config-overrides/testPrintConfig/style.css'), | ||
); | ||
|
||
const config = await stylelint.resolveConfig(filepath, { | ||
configFile: path.join(__dirname, 'fixtures/getConfigForFile/a/.stylelintrc'), | ||
}); | ||
|
||
expect(config).toStrictEqual({ | ||
pluginFunctions: { | ||
'plugin/warn-about-foo': expect.any(Function), | ||
}, | ||
plugins: [expect.stringMatching(/plugin-warn-about-foo/)], | ||
rules: { | ||
'block-no-empty': [true], | ||
'plugin/warn-about-foo': ['always'], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should use the passed config base directory', async () => { | ||
const filepath = replaceBackslashes( | ||
path.join(__dirname, 'fixtures/config-overrides/testPrintConfig/style.css'), | ||
); | ||
|
||
const config = await stylelint.resolveConfig(filepath, { | ||
configBasedir: path.join(__dirname, 'fixtures'), | ||
config: { | ||
extends: './config-extending-two', | ||
}, | ||
}); | ||
|
||
expect(config).toStrictEqual({ | ||
rules: { | ||
'block-no-empty': [true], | ||
'color-no-invalid-hex': [true], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should use the passed cwd', async () => { | ||
const filepath = replaceBackslashes( | ||
path.join(__dirname, 'fixtures/config-overrides/testPrintConfig/style.css'), | ||
); | ||
|
||
const config = await stylelint.resolveConfig(filepath, { | ||
cwd: path.join(__dirname, 'fixtures'), | ||
config: { | ||
extends: './config-extending-two', | ||
}, | ||
}); | ||
|
||
expect(config).toStrictEqual({ | ||
rules: { | ||
'block-no-empty': [true], | ||
'color-no-invalid-hex': [true], | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
const createStylelint = require('./createStylelint'); | ||
const path = require('path'); | ||
|
||
/** | ||
* Resolves the effective configuation for a given file. Resolves to `undefined` | ||
* if no config is found. | ||
* @param {string} filePath - The path to the file to get the config for. | ||
* @param {Pick< | ||
* import('stylelint').LinterOptions, | ||
* | 'cwd' | ||
* | 'config' | ||
* | 'configBasedir' | ||
* | 'configFile' | ||
* >} [options] - The options to use when creating the Stylelint instance. | ||
* @returns {Promise<import('stylelint').Config | undefined>} | ||
*/ | ||
module.exports = async function resolveConfig( | ||
filePath, | ||
{ cwd = process.cwd(), config, configBasedir, configFile } = {}, | ||
) { | ||
if (!filePath) { | ||
return undefined; | ||
} | ||
|
||
const stylelint = createStylelint({ | ||
config, | ||
configFile, | ||
configBasedir, | ||
cwd, | ||
}); | ||
|
||
const absoluteFilePath = !path.isAbsolute(filePath) | ||
? path.join(cwd, filePath) | ||
: path.normalize(filePath); | ||
|
||
const configSearchPath = stylelint._options.configFile || absoluteFilePath; | ||
|
||
const resolved = await stylelint.getConfigForFile(configSearchPath, absoluteFilePath); | ||
|
||
if (!resolved) { | ||
return undefined; | ||
} | ||
|
||
return resolved.config; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters