diff --git a/.gitignore b/.gitignore index f6191ccc..6618bc77 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ npm-debug.log* node_modules/ !fixtures/e2e/node_modules !fixtures/e2e/completion/node_modules +!fixtures/e2e/pkg-import/node_modules # Compiled and temporary files dist/ diff --git a/e2e/src/suite/definition/definitions.test.ts b/e2e/src/suite/definition/definitions.test.ts index 4a85da43..811adfc5 100644 --- a/e2e/src/suite/definition/definitions.test.ts +++ b/e2e/src/suite/definition/definitions.test.ts @@ -12,12 +12,15 @@ describe("SCSS Definition Test", function () { const vueDocUri = getDocUri("definition/AppButton.vue"); const svelteDocUri = getDocUri("definition/AppButton.svelte"); const astroDocUri = getDocUri("definition/AppButton.astro"); + const pkgImportUri = getDocUri("pkg-import/src/styles.scss"); + const scopedPkgImportUri = getDocUri("pkg-import/src/scoped.scss"); before(async () => { await showFile(docUri); await showFile(vueDocUri); await showFile(svelteDocUri); await showFile(astroDocUri); + await showFile(pkgImportUri); await sleepCI(); }); @@ -77,4 +80,20 @@ describe("SCSS Definition Test", function () { await testDefinition(svelteDocUri, position(18, 17), expectedLocation); await testDefinition(astroDocUri, position(21, 17), expectedLocation); }); + + it("finds symbol from pkg: import", async () => { + const expectedDocumentUri = getDocUri( + "pkg-import/node_modules/my-components/styles/colors.scss", + ); + const expectedLocation = sameLineLocation(expectedDocumentUri, 3, 1, 15); + await testDefinition(pkgImportUri, position(4, 19), expectedLocation); + }); + + it("finds symbol from scoped pkg: import", async () => { + const expectedDocumentUri = getDocUri( + "pkg-import/node_modules/@my-scope/my-components/styles/colors.scss", + ); + const expectedLocation = sameLineLocation(expectedDocumentUri, 3, 1, 15); + await testDefinition(scopedPkgImportUri, position(4, 19), expectedLocation); + }); }); diff --git a/e2e/src/suite/hover/hover.test.ts b/e2e/src/suite/hover/hover.test.ts index 8bb4d282..a256e0fe 100644 --- a/e2e/src/suite/hover/hover.test.ts +++ b/e2e/src/suite/hover/hover.test.ts @@ -7,6 +7,7 @@ describe("SCSS Hover Test", function () { const vueDocUri = getDocUri("hover/AppButton.vue"); const svelteDocUri = getDocUri("hover/AppButton.svelte"); const astroDocUri = getDocUri("hover/AppButton.astro"); + const pkgImportUri = getDocUri("pkg-import/src/styles.scss"); before(async () => { await showFile(docUri); @@ -14,6 +15,7 @@ describe("SCSS Hover Test", function () { await showFile(vueDocUri); await showFile(svelteDocUri); await showFile(astroDocUri); + await showFile(pkgImportUri); await sleepCI(); }); @@ -129,4 +131,11 @@ describe("SCSS Hover Test", function () { await testHover(collisionUri, position(5, 20), expectedContents); }); + + it("shows hover for symbols from pkg: import", async () => { + const expectedContents = { + contents: ["Primary brand color"], + }; + await testHover(pkgImportUri, position(4, 19), expectedContents); + }); }); diff --git a/fixtures/e2e/pkg-import/.gitignore b/fixtures/e2e/pkg-import/.gitignore new file mode 100644 index 00000000..9a51caed --- /dev/null +++ b/fixtures/e2e/pkg-import/.gitignore @@ -0,0 +1,4 @@ +dist/ +node_modules/* +!node_modules/my-components +!node_modules/@my-scope diff --git a/fixtures/e2e/pkg-import/README.md b/fixtures/e2e/pkg-import/README.md new file mode 100644 index 00000000..6039497f --- /dev/null +++ b/fixtures/e2e/pkg-import/README.md @@ -0,0 +1,3 @@ +# pkg-import + +https://sass-lang.com/blog/announcing-pkg-importers/ diff --git a/fixtures/e2e/pkg-import/node_modules/@my-scope/my-components/index.js b/fixtures/e2e/pkg-import/node_modules/@my-scope/my-components/index.js new file mode 100644 index 00000000..184dfcc9 --- /dev/null +++ b/fixtures/e2e/pkg-import/node_modules/@my-scope/my-components/index.js @@ -0,0 +1 @@ +console.log("Hello, World!"); diff --git a/fixtures/e2e/pkg-import/node_modules/@my-scope/my-components/package.json b/fixtures/e2e/pkg-import/node_modules/@my-scope/my-components/package.json new file mode 100644 index 00000000..7b879ee1 --- /dev/null +++ b/fixtures/e2e/pkg-import/node_modules/@my-scope/my-components/package.json @@ -0,0 +1,15 @@ +{ + "name": "@my-scope/my-components", + "version": "1.0.0", + "description": "", + "main": "index.js", + "exports": { + ".": { + "sass": "./styles/index.scss", + "default": "./index.js" + }, + "./*.scss": { + "sass": "./styles/*.scss" + } + } +} diff --git a/fixtures/e2e/pkg-import/node_modules/@my-scope/my-components/styles/colors.scss b/fixtures/e2e/pkg-import/node_modules/@my-scope/my-components/styles/colors.scss new file mode 100644 index 00000000..48b07207 --- /dev/null +++ b/fixtures/e2e/pkg-import/node_modules/@my-scope/my-components/styles/colors.scss @@ -0,0 +1,3 @@ +/// Primary brand color +/// @type {Color} +$color-primary: green; diff --git a/fixtures/e2e/pkg-import/node_modules/@my-scope/my-components/styles/index.scss b/fixtures/e2e/pkg-import/node_modules/@my-scope/my-components/styles/index.scss new file mode 100644 index 00000000..8166996f --- /dev/null +++ b/fixtures/e2e/pkg-import/node_modules/@my-scope/my-components/styles/index.scss @@ -0,0 +1 @@ +@forward "./colors.scss"; diff --git a/fixtures/e2e/pkg-import/node_modules/my-components/index.js b/fixtures/e2e/pkg-import/node_modules/my-components/index.js new file mode 100644 index 00000000..184dfcc9 --- /dev/null +++ b/fixtures/e2e/pkg-import/node_modules/my-components/index.js @@ -0,0 +1 @@ +console.log("Hello, World!"); diff --git a/fixtures/e2e/pkg-import/node_modules/my-components/package.json b/fixtures/e2e/pkg-import/node_modules/my-components/package.json new file mode 100644 index 00000000..ce8f4c74 --- /dev/null +++ b/fixtures/e2e/pkg-import/node_modules/my-components/package.json @@ -0,0 +1,15 @@ +{ + "name": "my-components", + "version": "1.0.0", + "description": "", + "main": "index.js", + "exports": { + ".": { + "sass": "./styles/index.scss", + "default": "./index.js" + }, + "./*.scss": { + "sass": "./styles/*.scss" + } + } +} diff --git a/fixtures/e2e/pkg-import/node_modules/my-components/styles/colors.scss b/fixtures/e2e/pkg-import/node_modules/my-components/styles/colors.scss new file mode 100644 index 00000000..48b07207 --- /dev/null +++ b/fixtures/e2e/pkg-import/node_modules/my-components/styles/colors.scss @@ -0,0 +1,3 @@ +/// Primary brand color +/// @type {Color} +$color-primary: green; diff --git a/fixtures/e2e/pkg-import/node_modules/my-components/styles/index.scss b/fixtures/e2e/pkg-import/node_modules/my-components/styles/index.scss new file mode 100644 index 00000000..8166996f --- /dev/null +++ b/fixtures/e2e/pkg-import/node_modules/my-components/styles/index.scss @@ -0,0 +1 @@ +@forward "./colors.scss"; diff --git a/fixtures/e2e/pkg-import/package-lock.json b/fixtures/e2e/pkg-import/package-lock.json new file mode 100644 index 00000000..71dffaf1 --- /dev/null +++ b/fixtures/e2e/pkg-import/package-lock.json @@ -0,0 +1,230 @@ +{ + "name": "pkg-import", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "pkg-import", + "version": "1.0.0", + "devDependencies": { + "sass": "^1.71.0" + } + }, + "node_modules/anymatch": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", + "integrity": "sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==", + "dev": true, + "dependencies": { + "normalize-path": "^3.0.0", + "picomatch": "^2.0.4" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/binary-extensions": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", + "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/braces": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", + "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "dev": true, + "dependencies": { + "fill-range": "^7.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/chokidar": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz", + "integrity": "sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==", + "dev": true, + "dependencies": { + "anymatch": "~3.1.2", + "braces": "~3.0.2", + "glob-parent": "~5.1.2", + "is-binary-path": "~2.1.0", + "is-glob": "~4.0.1", + "normalize-path": "~3.0.0", + "readdirp": "~3.6.0" + }, + "engines": { + "node": ">= 8.10.0" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + }, + "optionalDependencies": { + "fsevents": "~2.3.2" + } + }, + "node_modules/fill-range": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", + "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "dev": true, + "dependencies": { + "to-regex-range": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/glob-parent": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", + "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", + "dev": true, + "dependencies": { + "is-glob": "^4.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/immutable": { + "version": "4.3.5", + "resolved": "https://registry.npmjs.org/immutable/-/immutable-4.3.5.tgz", + "integrity": "sha512-8eabxkth9gZatlwl5TBuJnCsoTADlL6ftEr7A4qgdaTsPyreilDSnUk57SO+jfKcNtxPa22U5KK6DSeAYhpBJw==", + "dev": true + }, + "node_modules/is-binary-path": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", + "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", + "dev": true, + "dependencies": { + "binary-extensions": "^2.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/is-extglob": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", + "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-glob": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", + "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", + "dev": true, + "dependencies": { + "is-extglob": "^2.1.1" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "dev": true, + "engines": { + "node": ">=0.12.0" + } + }, + "node_modules/normalize-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "dev": true, + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "node_modules/readdirp": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", + "integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==", + "dev": true, + "dependencies": { + "picomatch": "^2.2.1" + }, + "engines": { + "node": ">=8.10.0" + } + }, + "node_modules/sass": { + "version": "1.71.0", + "resolved": "https://registry.npmjs.org/sass/-/sass-1.71.0.tgz", + "integrity": "sha512-HKKIKf49Vkxlrav3F/w6qRuPcmImGVbIXJ2I3Kg0VMA+3Bav+8yE9G5XmP5lMj6nl4OlqbPftGAscNaNu28b8w==", + "dev": true, + "dependencies": { + "chokidar": ">=3.0.0 <4.0.0", + "immutable": "^4.0.0", + "source-map-js": ">=0.6.2 <2.0.0" + }, + "bin": { + "sass": "sass.js" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/source-map-js": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.0.2.tgz", + "integrity": "sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/to-regex-range": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "dev": true, + "dependencies": { + "is-number": "^7.0.0" + }, + "engines": { + "node": ">=8.0" + } + } + } +} diff --git a/fixtures/e2e/pkg-import/package.json b/fixtures/e2e/pkg-import/package.json new file mode 100644 index 00000000..9dbf4fc2 --- /dev/null +++ b/fixtures/e2e/pkg-import/package.json @@ -0,0 +1,13 @@ +{ + "name": "pkg-import", + "version": "1.0.0", + "private": true, + "type": "commonjs", + "description": "Test using the pgk import from a module", + "scripts": { + "test": "node --test sass.test.js" + }, + "devDependencies": { + "sass": "^1.71.0" + } +} diff --git a/fixtures/e2e/pkg-import/sass.test.js b/fixtures/e2e/pkg-import/sass.test.js new file mode 100644 index 00000000..6126faf2 --- /dev/null +++ b/fixtures/e2e/pkg-import/sass.test.js @@ -0,0 +1,20 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +const assert = require("node:assert"); +const { test } = require("node:test"); +const sass = require("sass"); + +test("noscope", () => { + const css = sass.compile("src/styles.scss", { + importers: [new sass.NodePackageImporter()], + }); + assert.ok(css); + assert.match(css.css, /color: green/); +}); + +test("scope", () => { + const css = sass.compile("src/scoped.scss", { + importers: [new sass.NodePackageImporter()], + }); + assert.ok(css); + assert.match(css.css, /color: green/); +}); diff --git a/fixtures/e2e/pkg-import/src/scoped.scss b/fixtures/e2e/pkg-import/src/scoped.scss new file mode 100644 index 00000000..2b244732 --- /dev/null +++ b/fixtures/e2e/pkg-import/src/scoped.scss @@ -0,0 +1,5 @@ +@use "pkg:@my-scope/my-components/colors"; + +button { + color: colors.$color-primary; +} diff --git a/fixtures/e2e/pkg-import/src/styles.scss b/fixtures/e2e/pkg-import/src/styles.scss new file mode 100644 index 00000000..eb301d10 --- /dev/null +++ b/fixtures/e2e/pkg-import/src/styles.scss @@ -0,0 +1,5 @@ +@use "pkg:my-components/colors"; + +button { + color: colors.$color-primary; +} diff --git a/server/src/parser/language-service.ts b/server/src/parser/language-service.ts index ea2d3929..0ea783e3 100644 --- a/server/src/parser/language-service.ts +++ b/server/src/parser/language-service.ts @@ -34,6 +34,9 @@ export function getLanguageService(): LanguageService { }; } }, + getContent(uri, encoding) { + return fs.readFile(URI.parse(uri), encoding); + }, }; ls = getSCSSLanguageService({ fileSystemProvider, clientCapabilities }); diff --git a/server/src/parser/parser.ts b/server/src/parser/parser.ts index 600929b2..05db82bd 100644 --- a/server/src/parser/parser.ts +++ b/server/src/parser/parser.ts @@ -362,19 +362,21 @@ function ensureIndex(target: string): string { return `${path}/${fileName}/index${extension}`; } -function urlMatches(url: string, linkTarget: string): boolean { - let safeUrl = url; +function urlMatches(importString: string, fileUrl: string): boolean { + let safeUrl = importString.replace("pkg:", ""); while (/^[./@~]/.exec(safeUrl)) { safeUrl = safeUrl.slice(1); } - let match = linkTarget.includes(safeUrl); + // With pkg: and subpatch exports, the URL doesn't necessarily match the import string, + // but it should contain all parts of it. + let match = safeUrl.split("/").every((part) => fileUrl.includes(part)); if (!match) { const lastSlash = safeUrl.lastIndexOf("/"); const toLastSlash = safeUrl.slice(0, Math.max(0, lastSlash)); const restOfUrl = safeUrl.slice(Math.max(0, lastSlash + 1)); const partial = `${toLastSlash}/_${restOfUrl}`; - match = linkTarget.includes(partial); + match = fileUrl.includes(partial); } return match;