Skip to content

Commit

Permalink
fix: fire didchangevalidatelang… on first config (#270)
Browse files Browse the repository at this point in the history
Also adds Less to the extension manifest defaults, from which it was
missing.
  • Loading branch information
adalinesimonian committed Oct 22, 2021
1 parent b079e85 commit 041b995
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 2 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -114,6 +114,7 @@
},
"default": [
"css",
"less",
"postcss"
],
"description": "An array of language ids which should be validated by stylelint."
Expand All @@ -126,6 +127,7 @@
},
"default": [
"css",
"less",
"postcss"
],
"description": "An array of language ids which snippets are provided by stylelint."
Expand Down
63 changes: 63 additions & 0 deletions src/server/__tests__/__snapshots__/server.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
],
}
`;
110 changes: 110 additions & 0 deletions src/server/__tests__/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -402,6 +510,8 @@ describe('StylelintLanguageServer', () => {
const onDidChangeConfigurationHandler =
mockConnection.onDidChangeConfiguration.mock.calls[0][0];

onDidChangeConfigurationHandler({ settings: { stylelint: {} } });

onDidChangeConfigurationHandler({
settings: {
stylelint: {
Expand Down
10 changes: 9 additions & 1 deletion src/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand All @@ -391,6 +397,8 @@ class StylelintLanguageServer {
}

this.#invokeHandlers('onDidChangeConfiguration', { settings: this.#options });

this.#hasSentInitialConfiguration = true;
}
}

Expand Down

0 comments on commit 041b995

Please sign in to comment.