Skip to content

Commit b3d7360

Browse files
authored
Merge pull request #17 from wkillerud/fix/root-imports
fix: root imports for scoped npm packages
2 parents 33bb60a + eb4a8da commit b3d7360

File tree

5 files changed

+56
-79
lines changed

5 files changed

+56
-79
lines changed

.vscodeignore

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ npm-debug.log*
99
# Configuration
1010
.github/
1111
.vscode/
12+
.husky/
1213
.editorconfig
1314
.eslintrc.json
1415
.gitattributes
@@ -26,9 +27,11 @@ node_modules/
2627
.nyc_output/
2728
coverage/
2829
fixtures/
30+
dist/test/
2931
src/
3032
types/
3133
patches/
34+
CONTRIBUTING.md
3235
CHANGELOG.md
3336

3437
# Build artifacts

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "some-sass",
33
"displayName": "Some Sass",
44
"description": "Provides code suggestions, documentation and code navigation for modern SCSS",
5-
"version": "2.6.0",
5+
"version": "2.6.1",
66
"publisher": "SomewhatStationery",
77
"license": "MIT",
88
"engines": {
@@ -156,8 +156,9 @@
156156
"prepare": "husky install",
157157
"lint-staged": "lint-staged",
158158
"vscode:prepublish": "npm run build",
159-
"build": "webpack --mode production --devtool hidden-source-map",
160-
"dev": "webpack --mode development --watch",
159+
"clean": "rimraf dist",
160+
"build": "npm run clean && webpack --mode production --devtool hidden-source-map",
161+
"dev": "npm run clean && webpack --mode development --watch",
161162
"lint": "eslint \"src/**/*.ts\" --cache",
162163
"test": "npm run test:clean && npm run test:compile && npm run test:unit",
163164
"test:clean": "rimraf out",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
diff --git a/node_modules/vscode-css-languageservice/lib/esm/services/cssNavigation.js b/node_modules/vscode-css-languageservice/lib/esm/services/cssNavigation.js
2+
index 63b64d7..be64b1d 100644
3+
--- a/node_modules/vscode-css-languageservice/lib/esm/services/cssNavigation.js
4+
+++ b/node_modules/vscode-css-languageservice/lib/esm/services/cssNavigation.js
5+
@@ -370,9 +370,14 @@ function toTwoDigitHex(n) {
6+
return r.length !== 2 ? '0' + r : r;
7+
}
8+
function getModuleNameFromPath(path) {
9+
- // If a scoped module (starts with @) then get up until second instance of '/', otherwise get until first instance of '/'
10+
+ // If a scoped module (starts with @) then get up until second instance of '/', or to the end of the string for root-level imports.
11+
if (path[0] === '@') {
12+
- return path.substring(0, path.indexOf('/', path.indexOf('/') + 1));
13+
+ const secondSlash = path.indexOf('/', path.indexOf('/') + 1);
14+
+ if (secondSlash === -1) {
15+
+ return path;
16+
+ }
17+
+ return path.substring(0, path.indexOf('/', path.indexOf('/') + 1));
18+
}
19+
- return path.substring(0, path.indexOf('/'));
20+
+ // Otherwise get until first instance of '/'
21+
+ return path.substring(0, path.indexOf('/'));
22+
}
23+
diff --git a/node_modules/vscode-css-languageservice/lib/umd/services/cssNavigation.js b/node_modules/vscode-css-languageservice/lib/umd/services/cssNavigation.js
24+
index 3a42b53..d6725fd 100644
25+
--- a/node_modules/vscode-css-languageservice/lib/umd/services/cssNavigation.js
26+
+++ b/node_modules/vscode-css-languageservice/lib/umd/services/cssNavigation.js
27+
@@ -382,10 +382,15 @@
28+
return r.length !== 2 ? '0' + r : r;
29+
}
30+
function getModuleNameFromPath(path) {
31+
- // If a scoped module (starts with @) then get up until second instance of '/', otherwise get until first instance of '/'
32+
+ // If a scoped module (starts with @) then get up until second instance of '/', or to the end of the string for root-level imports.
33+
if (path[0] === '@') {
34+
- return path.substring(0, path.indexOf('/', path.indexOf('/') + 1));
35+
+ const secondSlash = path.indexOf('/', path.indexOf('/') + 1);
36+
+ if (secondSlash === -1) {
37+
+ return path;
38+
+ }
39+
+ return path.substring(0, path.indexOf('/', path.indexOf('/') + 1));
40+
}
41+
- return path.substring(0, path.indexOf('/'));
42+
+ // Otherwise get until first instance of '/'
43+
+ return path.substring(0, path.indexOf('/'));
44+
}
45+
});

src/server/parser/document.ts

+3-75
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,9 @@
1-
import { dirname, join } from "path";
21
import type { DocumentContext } from "vscode-css-languageservice";
3-
import { URI } from "vscode-uri";
4-
import { FileSystemProvider } from "../../shared/file-system";
5-
import type { NodeFileSystem } from "../../shared/node-file-system";
6-
7-
/*---------------------------------------------------------------------------------------------
8-
* Copyright (c) Microsoft Corporation. All rights reserved.
9-
* Licensed under the MIT License.
10-
* See https://github.com/microsoft/vscode/blob/main/LICENSE.txt for license information.
11-
*--------------------------------------------------------------------------------------------*/
12-
function getModuleNameFromPath(path: string) {
13-
// If a scoped module (starts with @) then get up until second instance of '/', otherwise get until first isntance of '/'
14-
if (path.startsWith("@")) {
15-
return path.slice(0, Math.max(0, path.indexOf("/", path.indexOf("/") + 1)));
16-
}
17-
18-
return path.slice(0, Math.max(0, path.indexOf("/")));
19-
}
20-
21-
function resolvePathToModule(
22-
moduleName: string,
23-
relativeTo: string,
24-
fs: FileSystemProvider,
25-
): string | undefined {
26-
// Resolve the module relative to the document. We can't use `require` here as the code is webpacked.
27-
// fsPath use is OK here since we never reach this function unless the URI is a file://.
28-
const documentFolder = dirname(URI.parse(relativeTo).fsPath);
29-
30-
// Assume this path exists. If not, let VS Code deal with the "404" and have the user fix a typo or install node_modules.
31-
const packPath = join(
32-
documentFolder,
33-
"node_modules",
34-
moduleName,
35-
"package.json",
36-
);
37-
38-
if (Object.prototype.hasOwnProperty.call(fs, "existsSync")) {
39-
if ((fs as NodeFileSystem).existsSync(packPath)) {
40-
return URI.file(packPath).toString();
41-
}
42-
}
43-
44-
return undefined;
45-
}
46-
47-
function resolve(from: string, to: string): string {
48-
const resolvedUrl = new URL(to, new URL(from, "resolve://"));
49-
if (resolvedUrl.protocol === "resolve:") {
50-
// `from` is a relative URL.
51-
const { pathname, search, hash } = resolvedUrl;
52-
return pathname + search + hash;
53-
}
54-
return resolvedUrl.toString();
55-
}
2+
import { URI, Utils } from "vscode-uri";
563

574
export function buildDocumentContext(
585
documentUri: string,
596
workspaceRoot: URI,
60-
fs: FileSystemProvider,
617
): DocumentContext {
628
function getRootFolder(): string | undefined {
639
let folderURI = workspaceRoot.toString();
@@ -83,26 +29,8 @@ export function buildDocumentContext(
8329
return folderUri + ref.slice(1);
8430
}
8531
}
86-
87-
// Following [css-loader](https://github.com/webpack-contrib/css-loader#url)
88-
// and [sass-loader's](https://github.com/webpack-contrib/sass-loader#imports)
89-
// convention, if an import path starts with ~ then use node module resolution
90-
// *unless* it starts with "~/" as this refers to the user's home directory.
91-
if (ref.startsWith("~") && ref[1] !== "/") {
92-
ref = ref.slice(1);
93-
if (base.startsWith("file://")) {
94-
const moduleName = getModuleNameFromPath(ref);
95-
const modulePath = resolvePathToModule(moduleName, base, fs);
96-
if (modulePath) {
97-
const pathWithinModule = ref.slice(
98-
Math.max(0, moduleName.length + 1),
99-
);
100-
return resolve(modulePath, pathWithinModule);
101-
}
102-
}
103-
}
104-
105-
return resolve(base, ref);
32+
base = base.substr(0, base.lastIndexOf("/") + 1);
33+
return Utils.resolvePath(URI.parse(base), ref).toString();
10634
},
10735
};
10836
}

src/server/parser/parser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async function findDocumentSymbols(
5757
const links = await ls.findDocumentLinks2(
5858
document,
5959
ast,
60-
buildDocumentContext(document.uri, workspaceRoot, fs),
60+
buildDocumentContext(document.uri, workspaceRoot),
6161
);
6262

6363
const text = document.getText();

0 commit comments

Comments
 (0)