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

Add customSyntax option to configuration object (#4843) #5538

Merged
merged 1 commit into from Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using internals.

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,
};
Comment on lines +5 to +8
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better represents custom syntax.

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');
});
});
Comment on lines +100 to +144
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We never had these tests, but should have, as it's a documented behavior.


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