From 19982f0c46a2b9a717de7016fede66e1d9e94e3d Mon Sep 17 00:00:00 2001 From: jeanp413 Date: Mon, 3 May 2021 10:46:28 -0500 Subject: [PATCH 1/2] Fixes https://github.com/microsoft/vscode/issues/121586 --- src/services/cssNavigation.ts | 12 +++++++----- src/services/scssNavigation.ts | 25 +++++++++++-------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/services/cssNavigation.ts b/src/services/cssNavigation.ts index ae3e06e5..7450bb0f 100644 --- a/src/services/cssNavigation.ts +++ b/src/services/cssNavigation.ts @@ -115,7 +115,7 @@ export class CSSNavigation { for (let link of links) { const target = link.target; if (target && !(/^\w+:\/\//g.test(target))) { - const resolvedTarget = await this.resolveRelativeReference(target, document.uri, documentContext); + const resolvedTarget = await this.resolveRelativeReference(target, document.uri, documentContext, link.isRawLink); if (resolvedTarget !== undefined) { link.target = resolvedTarget; resolvedLinks.push(link); @@ -128,8 +128,8 @@ export class CSSNavigation { } - private findUnresolvedLinks(document: TextDocument, stylesheet: nodes.Stylesheet): DocumentLink[] { - const result: DocumentLink[] = []; + private findUnresolvedLinks(document: TextDocument, stylesheet: nodes.Stylesheet): (DocumentLink & { isRawLink: boolean })[] { + const result: (DocumentLink & { isRawLink: boolean })[] = []; const collect = (uriStringNode: nodes.Node) => { let rawUri = uriStringNode.getText(); @@ -142,7 +142,9 @@ export class CSSNavigation { if (startsWith(rawUri, `'`) || startsWith(rawUri, `"`)) { rawUri = rawUri.slice(1, -1); } - result.push({ target: rawUri, range }); + + const isRawLink = uriStringNode.parent ? this.isRawStringDocumentLinkNode(uriStringNode.parent) : false; + result.push({ target: rawUri, range, isRawLink }); }; stylesheet.accept(candidate => { @@ -275,7 +277,7 @@ export class CSSNavigation { }; } - protected async resolveRelativeReference(ref: string, documentUri: string, documentContext: DocumentContext): Promise { + protected async resolveRelativeReference(ref: string, documentUri: string, documentContext: DocumentContext, isRawLink?: boolean): Promise { // Following [css-loader](https://github.com/webpack-contrib/css-loader#url) // and [sass-loader's](https://github.com/webpack-contrib/sass-loader#imports) // convention, if an import path starts with ~ then use node module resolution diff --git a/src/services/scssNavigation.ts b/src/services/scssNavigation.ts index dacf5135..bc96443e 100644 --- a/src/services/scssNavigation.ts +++ b/src/services/scssNavigation.ts @@ -23,27 +23,24 @@ export class SCSSNavigation extends CSSNavigation { ); } - protected async resolveRelativeReference(ref: string, documentUri: string, documentContext: DocumentContext): Promise { + protected async resolveRelativeReference(ref: string, documentUri: string, documentContext: DocumentContext, isRawLink?: boolean): Promise { if (startsWith(ref, 'sass:')) { return undefined; // sass library } - const target = await super.resolveRelativeReference(ref, documentUri, documentContext); - if (this.fileSystemProvider && target) { + const target = await super.resolveRelativeReference(ref, documentUri, documentContext, isRawLink); + if (this.fileSystemProvider && target && isRawLink) { const parsedUri = URI.parse(target); - if (parsedUri.path && Utils.extname(parsedUri).length === 0) { - try { - const pathVariations = toPathVariations(parsedUri); - if (pathVariations) { - for (let j = 0; j < pathVariations.length; j++) { - if (await this.fileExists(pathVariations[j])) { - return pathVariations[j]; - } + try { + const pathVariations = toPathVariations(parsedUri); + if (pathVariations) { + for (let j = 0; j < pathVariations.length; j++) { + if (await this.fileExists(pathVariations[j])) { + return pathVariations[j]; } } - return undefined; - } catch (e) { - // ignore } + } catch (e) { + // ignore } } return target; From fc554e6f59d61dd164cce68a6c9fa2a3918df6df Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Fri, 28 May 2021 15:20:53 +0200 Subject: [PATCH 2/2] fix tests --- src/services/cssNavigation.ts | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/services/cssNavigation.ts b/src/services/cssNavigation.ts index 7450bb0f..f4f938b5 100644 --- a/src/services/cssNavigation.ts +++ b/src/services/cssNavigation.ts @@ -6,7 +6,7 @@ import { Color, ColorInformation, ColorPresentation, DocumentHighlight, DocumentHighlightKind, DocumentLink, Location, - Position, Range, SymbolInformation, SymbolKind, TextEdit, WorkspaceEdit, TextDocument, DocumentContext, FileSystemProvider, DocumentUri, FileType + Position, Range, SymbolInformation, SymbolKind, TextEdit, WorkspaceEdit, TextDocument, DocumentContext, FileSystemProvider, FileType } from '../cssLanguageTypes'; import * as nls from 'vscode-nls'; import * as nodes from '../parser/cssNodes'; @@ -17,6 +17,8 @@ import { dirname, joinPath } from '../utils/resources'; const localize = nls.loadMessageBundle(); +type UnresolvedLinkData = { link: DocumentLink, isRawLink: boolean } + export class CSSNavigation { constructor(protected fileSystemProvider: FileSystemProvider | undefined) { @@ -96,26 +98,30 @@ export class CSSNavigation { } public findDocumentLinks(document: TextDocument, stylesheet: nodes.Stylesheet, documentContext: DocumentContext): DocumentLink[] { - const links = this.findUnresolvedLinks(document, stylesheet); - for (let i = 0; i < links.length; i++) { - const target = links[i].target; + const linkData = this.findUnresolvedLinks(document, stylesheet); + const resolvedLinks: DocumentLink[] = []; + for (let data of linkData) { + const link = data.link; + const target = link.target; if (target && !(/^\w+:\/\//g.test(target))) { const resolved = documentContext.resolveReference(target, document.uri); if (resolved) { - links[i].target = resolved; + link.target = resolved; } } + resolvedLinks.push(link); } - return links; + return resolvedLinks; } public async findDocumentLinks2(document: TextDocument, stylesheet: nodes.Stylesheet, documentContext: DocumentContext): Promise { - const links = this.findUnresolvedLinks(document, stylesheet); + const linkData = this.findUnresolvedLinks(document, stylesheet); const resolvedLinks: DocumentLink[] = []; - for (let link of links) { + for (let data of linkData) { + const link = data.link; const target = link.target; if (target && !(/^\w+:\/\//g.test(target))) { - const resolvedTarget = await this.resolveRelativeReference(target, document.uri, documentContext, link.isRawLink); + const resolvedTarget = await this.resolveRelativeReference(target, document.uri, documentContext, data.isRawLink); if (resolvedTarget !== undefined) { link.target = resolvedTarget; resolvedLinks.push(link); @@ -128,8 +134,8 @@ export class CSSNavigation { } - private findUnresolvedLinks(document: TextDocument, stylesheet: nodes.Stylesheet): (DocumentLink & { isRawLink: boolean })[] { - const result: (DocumentLink & { isRawLink: boolean })[] = []; + private findUnresolvedLinks(document: TextDocument, stylesheet: nodes.Stylesheet): UnresolvedLinkData[] { + const result: UnresolvedLinkData[] = []; const collect = (uriStringNode: nodes.Node) => { let rawUri = uriStringNode.getText(); @@ -144,7 +150,7 @@ export class CSSNavigation { } const isRawLink = uriStringNode.parent ? this.isRawStringDocumentLinkNode(uriStringNode.parent) : false; - result.push({ target: rawUri, range, isRawLink }); + result.push({ link: { target: rawUri, range }, isRawLink }); }; stylesheet.accept(candidate => {