diff --git a/package.json b/package.json index c6aa0ff4..5c751cd1 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "vscode-stylelint", "version": "1.0.0", "license": "MIT", - "description": "Modern CSS/SCSS/Less linter", + "description": "Modern CSS/Less/SCSS linter", "main": "dist/index.js", "displayName": "Stylelint", "publisher": "stylelint", @@ -114,6 +114,7 @@ }, "default": [ "css", + "less", "postcss" ], "description": "An array of language ids which should be validated by stylelint." @@ -126,6 +127,7 @@ }, "default": [ "css", + "less", "postcss" ], "description": "An array of language ids which snippets are provided by stylelint." diff --git a/src/server/__tests__/__snapshots__/server.js.snap b/src/server/__tests__/__snapshots__/server.js.snap index f60f872a..4e37399a 100644 --- a/src/server/__tests__/__snapshots__/server.js.snap +++ b/src/server/__tests__/__snapshots__/server.js.snap @@ -208,6 +208,41 @@ Object { } `; +exports[`StylelintLanguageServer should fire onDidChangeValidateLanguages when first settings are sent to server 1`] = ` +Object { + "config": undefined, + "configBasedir": undefined, + "configFile": undefined, + "customSyntax": undefined, + "ignoreDisables": undefined, + "packageManager": "npm", + "reportInvalidScopeDisables": undefined, + "reportNeedlessDisables": undefined, + "snippet": Array [ + "css", + "less", + "postcss", + ], + "stylelintPath": undefined, + "validate": Array [ + "css", + "less", + "postcss", + ], +} +`; + +exports[`StylelintLanguageServer should fire onDidChangeValidateLanguages when first settings are sent to server 2`] = ` +Object { + "languages": Set { + "css", + "less", + "postcss", + }, + "removedLanguages": Set {}, +} +`; + exports[`StylelintLanguageServer should fire onDidChangeValidateLanguages when validate option changes 1`] = ` Object { "config": undefined, @@ -273,3 +308,31 @@ Object { ], } `; + +exports[`StylelintLanguageServer should receive updates to settings from the client and pass them to modules 1`] = ` +Object { + "config": Object { + "rules": Object { + "block-no-empty": true, + }, + }, + "configBasedir": undefined, + "configFile": undefined, + "customSyntax": undefined, + "ignoreDisables": true, + "packageManager": "npm", + "reportInvalidScopeDisables": undefined, + "reportNeedlessDisables": undefined, + "snippet": Array [ + "css", + "less", + "postcss", + ], + "stylelintPath": undefined, + "validate": Array [ + "css", + "less", + "postcss", + ], +} +`; diff --git a/src/server/__tests__/server.js b/src/server/__tests__/server.js index 4e14f09b..b3aeaf4d 100644 --- a/src/server/__tests__/server.js +++ b/src/server/__tests__/server.js @@ -354,6 +354,114 @@ describe('StylelintLanguageServer', () => { expect(options).toMatchSnapshot(); }); + test('should receive updates to settings from the client and pass them to modules', () => { + /** + * @type {LanguageServerOptions | undefined} + */ + let options; + + class TestModule { + static id = 'test-module'; + + /** + * @param {DidChangeConfigurationParams} params + */ + onDidChangeConfiguration({ settings }) { + options = settings; + } + } + + const server = new StylelintLanguageServer({ + connection: mockConnection, + logger: mockLogger, + modules: [TestModule], + }); + + server.start(); + + const onInitializeHandler = mockConnection.onInitialize.mock.calls[0][0]; + + onInitializeHandler( + /** @type {any} */ ({ capabilities: {} }), + /** @type {any} */ ({}), + /** @type {any} */ ({}), + ); + + const onDidChangeConfigurationHandler = + mockConnection.onDidChangeConfiguration.mock.calls[0][0]; + + onDidChangeConfigurationHandler({ settings: { stylelint: {} } }); + + onDidChangeConfigurationHandler({ + settings: { + stylelint: { + config: { + rules: { + 'block-no-empty': true, + }, + }, + ignoreDisables: true, + }, + }, + }); + + expect(options).toMatchSnapshot(); + }); + + test('should fire onDidChangeValidateLanguages when first settings are sent to server', () => { + /** + * @type {LanguageServerOptions | undefined} + */ + let options; + + /** + * @type {DidChangeValidateLanguagesParams | undefined} + */ + let languageParams; + + class TestModule { + static id = 'test-module'; + + /** + * @param {DidChangeConfigurationParams} params + */ + onDidChangeConfiguration({ settings }) { + options = settings; + } + + /** + * @param {DidChangeValidateLanguagesParams} params + */ + onDidChangeValidateLanguages(params) { + languageParams = params; + } + } + + const server = new StylelintLanguageServer({ + connection: mockConnection, + logger: mockLogger, + modules: [TestModule], + }); + + server.start(); + + const onInitializeHandler = mockConnection.onInitialize.mock.calls[0][0]; + + onInitializeHandler( + /** @type {any} */ ({ capabilities: {} }), + /** @type {any} */ ({}), + /** @type {any} */ ({}), + ); + + const onDidChangeConfigurationHandler = + mockConnection.onDidChangeConfiguration.mock.calls[0][0]; + + onDidChangeConfigurationHandler({ settings: { stylelint: {} } }); + + expect(options).toMatchSnapshot(); + expect(languageParams).toMatchSnapshot(); + }); + test('should fire onDidChangeValidateLanguages when validate option changes', () => { /** * @type {LanguageServerOptions | undefined} @@ -402,6 +510,8 @@ describe('StylelintLanguageServer', () => { const onDidChangeConfigurationHandler = mockConnection.onDidChangeConfiguration.mock.calls[0][0]; + onDidChangeConfigurationHandler({ settings: { stylelint: {} } }); + onDidChangeConfigurationHandler({ settings: { stylelint: { diff --git a/src/server/server.js b/src/server/server.js index 9e10548f..edc65a8a 100644 --- a/src/server/server.js +++ b/src/server/server.js @@ -69,6 +69,12 @@ class StylelintLanguageServer { */ #modules = new Map(); + /** + * Whether or not the server has sent its first configuration change to + * modules. + */ + #hasSentInitialConfiguration = false; + /** * Creates a new Stylelint language server. * @param {LanguageServerConstructorParameters} params @@ -376,7 +382,7 @@ class StylelintLanguageServer { } } - if (changed) { + if (changed || !this.#hasSentInitialConfiguration) { if (this.#logger?.isDebugEnabled()) { this.#logger?.debug('Languages that should be validated changed', { languages: [...validateLanguageSet], @@ -391,6 +397,8 @@ class StylelintLanguageServer { } this.#invokeHandlers('onDidChangeConfiguration', { settings: this.#options }); + + this.#hasSentInitialConfiguration = true; } }