Skip to content

Commit

Permalink
Add customSyntax option to configuration object (#4843) (#5538)
Browse files Browse the repository at this point in the history
  • Loading branch information
hudochenkov committed Sep 15, 2021
1 parent e8d1f98 commit 6acf73f
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/__tests__/fixtures/custom-parser.js
@@ -1,3 +1,3 @@
'use strict';

module.exports = require('postcss-safe-parser/lib/safe-parse');
module.exports = require('postcss-safe-parser');
7 changes: 6 additions & 1 deletion lib/__tests__/fixtures/custom-syntax.js
@@ -1,3 +1,8 @@
'use strict';

module.exports = require('postcss-scss/lib/scss-syntax');
const postcssScss = require('postcss-scss');

module.exports = {
parse: postcssScss.parse,
stringify: postcssScss.stringify,
};
154 changes: 154 additions & 0 deletions lib/__tests__/standalone-syntax.test.js
Expand Up @@ -97,6 +97,52 @@ it('standalone with path to custom syntax', () => {
});
});

it('standalone with custom syntax as npm package name', () => {
const config = {
rules: {
'block-no-empty': true,
},
};

return standalone({
config,
customSyntax: 'postcss-scss',
code: '$foo: bar; // foo;\nb {}',
formatter: stringFormatter,
}).then((linted) => {
const results = linted.results;

expect(results).toHaveLength(1);
expect(results[0].warnings).toHaveLength(1);
expect(results[0].warnings[0].line).toBe(2);
expect(results[0].warnings[0].column).toBe(3);
expect(results[0].warnings[0].rule).toBe('block-no-empty');
});
});

it('standalone with custom syntax as npm package', () => {
const config = {
rules: {
'block-no-empty': true,
},
};

return standalone({
config,
customSyntax: require('postcss-scss'),
code: '$foo: bar; // foo;\nb {}',
formatter: stringFormatter,
}).then((linted) => {
const results = linted.results;

expect(results).toHaveLength(1);
expect(results[0].warnings).toHaveLength(1);
expect(results[0].warnings[0].line).toBe(2);
expect(results[0].warnings[0].column).toBe(3);
expect(results[0].warnings[0].rule).toBe('block-no-empty');
});
});

it('rejects on unknown custom syntax option', async () => {
await expect(
standalone({
Expand Down Expand Up @@ -140,3 +186,111 @@ it('rejects when customSyntax and syntax are set', async () => {
'The "syntax" (--syntax) option has been removed. Install the appropriate syntax and use the "customSyntax" (--custom-syntax) option instead',
);
});

describe('customSyntax set in the config', () => {
it('standalone with path to custom parser', () => {
const config = {
customSyntax: `${fixturesPath}/custom-parser`,
rules: {
'block-no-empty': true,
},
};

return standalone({
config,
code: '.foo { width: 200px }\n.bar {',
formatter: stringFormatter,
}).then((linted) => {
const results = linted.results;

expect(results).toHaveLength(1);
expect(results[0].warnings).toHaveLength(1);
expect(results[0].warnings[0].line).toBe(2);
expect(results[0].warnings[0].column).toBe(6);
expect(results[0].warnings[0].rule).toBe('block-no-empty');
});
});

it('standalone with custom syntax as npm package name', () => {
const config = {
customSyntax: 'postcss-scss',
rules: {
'block-no-empty': true,
},
};

return standalone({
config,
code: '$foo: bar; // foo;\nb {}',
formatter: stringFormatter,
}).then((linted) => {
const results = linted.results;

expect(results).toHaveLength(1);
expect(results[0].warnings).toHaveLength(1);
expect(results[0].warnings[0].line).toBe(2);
expect(results[0].warnings[0].column).toBe(3);
expect(results[0].warnings[0].rule).toBe('block-no-empty');
});
});

it('standalone with custom syntax as npm package', () => {
const config = {
customSyntax: require('postcss-scss'),
rules: {
'block-no-empty': true,
},
};

return standalone({
config,
code: '$foo: bar; // foo;\nb {}',
formatter: stringFormatter,
}).then((linted) => {
const results = linted.results;

expect(results).toHaveLength(1);
expect(results[0].warnings).toHaveLength(1);
expect(results[0].warnings[0].line).toBe(2);
expect(results[0].warnings[0].column).toBe(3);
expect(results[0].warnings[0].rule).toBe('block-no-empty');
});
});

it('standalone with path to custom syntax', () => {
const config = {
customSyntax: `${fixturesPath}/custom-syntax`,
rules: {
'block-no-empty': true,
},
};

return standalone({
config,
code: '$foo: bar; // foo;\nb {}',
formatter: stringFormatter,
}).then((linted) => {
const results = linted.results;

expect(results).toHaveLength(1);
expect(results[0].warnings).toHaveLength(1);
expect(results[0].warnings[0].line).toBe(2);
expect(results[0].warnings[0].column).toBe(3);
expect(results[0].warnings[0].rule).toBe('block-no-empty');
});
});

it('rejects on unknown custom syntax option', async () => {
await expect(
standalone({
code: '',
config: {
customSyntax: 'unknown-module',
rules: { 'block-no-empty': 'wahoo' },
},
}),
).rejects.toThrow(
'Cannot resolve custom syntax module unknown-module. Check that module unknown-module is available and spelled correctly.',
);
});
});
4 changes: 4 additions & 0 deletions lib/augmentConfig.js
Expand Up @@ -471,6 +471,10 @@ function addOptions(stylelint, config) {
stylelint._options.reportDescriptionlessDisables;
}

if (stylelint._options.customSyntax) {
augmentedConfig.customSyntax = stylelint._options.customSyntax;
}

return augmentedConfig;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/getPostcssResult.js
Expand Up @@ -19,7 +19,7 @@ const postcssProcessor = postcss();
*
* @returns {Promise<Result>}
*/
module.exports = function (stylelint, options = {}) {
module.exports = function getPostcssResult(stylelint, options = {}) {
const cached = options.filePath ? stylelint._postcssResultCache.get(options.filePath) : undefined;

if (cached) return Promise.resolve(cached);
Expand Down Expand Up @@ -48,8 +48,8 @@ module.exports = function (stylelint, options = {}) {
/** @type {Syntax | null} */
let syntax = null;

if (stylelint._options.customSyntax) {
syntax = getCustomSyntax(stylelint._options.customSyntax);
if (options.customSyntax) {
syntax = getCustomSyntax(options.customSyntax);
} else {
syntax = cssSyntax(stylelint, options.filePath);
}
Expand Down
1 change: 1 addition & 0 deletions lib/lintSource.js
Expand Up @@ -89,6 +89,7 @@ module.exports = function lintSource(stylelint, options = {}) {
codeFilename: options.codeFilename,
filePath: inputFilePath,
codeProcessors: config.codeProcessors,
customSyntax: config.customSyntax,
})
.then((postcssResult) => {
const stylelintPostcssResult = Object.assign(postcssResult, {
Expand Down
1 change: 1 addition & 0 deletions types/stylelint/index.d.ts
Expand Up @@ -56,6 +56,7 @@ declare module 'stylelint' {
reportInvalidScopeDisables?: DisableSettings;
reportDescriptionlessDisables?: DisableSettings;
overrides?: StylelintConfigOverride[];
customSyntax?: CustomSyntax;
};

// A meta-type that returns a union over all properties of `T` whose values
Expand Down

0 comments on commit 6acf73f

Please sign in to comment.