Skip to content

Commit

Permalink
feat: handle default and project level resolve namespace (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
idoros authored Jun 5, 2024
1 parent 33b36dd commit 6b286df
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions fixtures/config-errors/errors.st.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.root::unknownPseudoElement {}
4 changes: 4 additions & 0 deletions fixtures/config-errors/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "test-project-with-config-errors",
"version": "1.0.0"
}
11 changes: 11 additions & 0 deletions fixtures/config-errors/stylable.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@ts-check

module.exports = {
defaultConfig() {
return {
resolveNamespace(namespace) {
throw new Error('resolveNamespace test error for namespace: ' + namespace);
},
};
},
};
1 change: 1 addition & 0 deletions run-e2e-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ async function runAllSuites() {
await runSuite('latest');
await runSuite('v5-with-no-config');
await runSuite('v5-with-config');
await runSuite('config-errors');
}

runAllSuites();
12 changes: 12 additions & 0 deletions src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ connection.onInitialize(async (params) => {
fileSystem: wrappedFs,
requireModule: require,
cssParser: safeParse,
resolveNamespace(namespace, origin, source) {
try {
if (config.resolveNamespace) {
return config.resolveNamespace(namespace, origin, source);
}
} catch (e: unknown) {
// can be used to log project level errors
// console.warn('resolveNamespace failed', e);
}
// fallback to initial namespace
return namespace;
},
})
);

Expand Down
54 changes: 54 additions & 0 deletions test/e2e/config-errors/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import fs from '@file-services/node';
import vscode from 'vscode';
import { expect } from 'chai';
import path from 'path';

suite('configuration with errors', function () {
this.timeout(60000);

let rootDir: string | null;

suiteSetup(() => {
rootDir = fs.dirname(fs.findClosestFileSync(__dirname, 'package.json')!);
});

async function getWorkingDocument(...testSubPath: string[]) {
const casesPath = path.join(rootDir!, 'fixtures', 'config-errors', ...testSubPath);
const ext = vscode.extensions.getExtension('wix.stylable-intelligence');

if (ext) {
const doc = await vscode.workspace.openTextDocument(casesPath);
await ext.activate();
return doc;
} else {
throw new Error('Where is my extension?!!');
}
}
function collectDiagnostics(source: 'stylable' | 'css', file: string) {
const diags = vscode.languages.getDiagnostics();
const res = [];

for (const [pathObj, fileDiags] of diags) {
for (const diag of fileDiags) {
if (diag.source === source && file === pathObj.fsPath) {
res.push({
message: diag.message,
range: diag.range,
severity: diag.severity,
filePath: fs.realpathSync.native(pathObj.fsPath),
});
}
}
}

return res;
}

test('should handle errors in resolve namespace config', async () => {
const testDoc = await getWorkingDocument('errors.st.css');
await vscode.window.showTextDocument(testDoc);
const diags = collectDiagnostics('stylable', testDoc.uri.fsPath);
// make sure extension functions correctly
expect(diags.length).to.not.eql(0);
});
});
5 changes: 5 additions & 0 deletions test/e2e/config-errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { collectAndRunTests } from '../../lsp-testkit/collect-and-run-tests';

export function run() {
return collectAndRunTests({ testsRoot: __dirname, suiteName: 'config errors' });
}

0 comments on commit 6b286df

Please sign in to comment.