diff --git a/extensions/emmet/src/abbreviationActions.ts b/extensions/emmet/src/abbreviationActions.ts index ca19b490b77..23cb3185c25 100644 --- a/extensions/emmet/src/abbreviationActions.ts +++ b/extensions/emmet/src/abbreviationActions.ts @@ -340,9 +340,7 @@ export function expandEmmetAbbreviation(args: any): Thenable { - if (!success) { - return fallbackTab(); - } + return success ? Promise.resolve(undefined) : fallbackTab(); }); } @@ -581,7 +579,7 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex /** * Expands abbreviation as detailed in given input. */ -function expandAbbr(input: ExpandAbbreviationInput): string | undefined { +function expandAbbr(input: ExpandAbbreviationInput): string { const helper = getEmmetHelper(); const expandOptions = helper.getExpandOptions(input.syntax, getEmmetConfiguration(input.syntax), input.filter); @@ -601,9 +599,9 @@ function expandAbbr(input: ExpandAbbreviationInput): string | undefined { } } + let expandedText; try { // Expand the abbreviation - let expandedText; if (input.textToWrap) { let parsedAbbr = helper.parseAbbreviation(input.abbreviation, expandOptions); @@ -628,13 +626,11 @@ function expandAbbr(input: ExpandAbbreviationInput): string | undefined { expandedText = helper.expandAbbreviation(input.abbreviation, expandOptions); } - return expandedText; - } catch (e) { vscode.window.showErrorMessage('Failed to expand abbreviation'); } - + return expandedText; } function getSyntaxFromArgs(args: { [x: string]: string }): string | undefined { diff --git a/extensions/emmet/src/defaultCompletionProvider.ts b/extensions/emmet/src/defaultCompletionProvider.ts index 92888ea1c4b..daddd3c6131 100644 --- a/extensions/emmet/src/defaultCompletionProvider.ts +++ b/extensions/emmet/src/defaultCompletionProvider.ts @@ -12,8 +12,8 @@ export class DefaultCompletionItemProvider implements vscode.CompletionItemProvi private lastCompletionType: string | undefined; - public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, context: vscode.CompletionContext): Thenable | undefined { - const completionResult = this.provideCompletionItemsInternal(document, position, token, context); + public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, _: vscode.CancellationToken, context: vscode.CompletionContext): Thenable | undefined { + const completionResult = this.provideCompletionItemsInternal(document, position, context); if (!completionResult) { this.lastCompletionType = undefined; return; @@ -38,7 +38,7 @@ export class DefaultCompletionItemProvider implements vscode.CompletionItemProvi }); } - private provideCompletionItemsInternal(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, context: vscode.CompletionContext): Thenable | undefined { + private provideCompletionItemsInternal(document: vscode.TextDocument, position: vscode.Position, context: vscode.CompletionContext): Thenable | undefined { const emmetConfig = vscode.workspace.getConfiguration('emmet'); const excludedLanguages = emmetConfig['excludeLanguages'] ? emmetConfig['excludeLanguages'] : []; if (excludedLanguages.indexOf(document.languageId) > -1) { diff --git a/extensions/emmet/src/editPoint.ts b/extensions/emmet/src/editPoint.ts index afd73a4ddcb..d9151185702 100644 --- a/extensions/emmet/src/editPoint.ts +++ b/extensions/emmet/src/editPoint.ts @@ -69,4 +69,5 @@ function findEditPoint(lineNum: number, editor: vscode.TextEditor, position: vsc if (winner > -1) { return new vscode.Selection(lineNum, winner + 1, lineNum, winner + 1); } + return; } diff --git a/extensions/emmet/src/incrementDecrement.ts b/extensions/emmet/src/incrementDecrement.ts index 522836cf093..9e0afa4acf1 100644 --- a/extensions/emmet/src/incrementDecrement.ts +++ b/extensions/emmet/src/incrementDecrement.ts @@ -45,7 +45,7 @@ export function update(numString: string, delta: number): string { if (m = numString.match(/^\-?(0\d+)/)) { // padded number: preserve padding - output = output.replace(/^(\-?)(\d+)/, (str, minus, prefix) => + output = output.replace(/^(\-?)(\d+)/, (_, minus, prefix) => minus + '0'.repeat(Math.max(0, (m ? m[1].length : 0) - prefix.length)) + prefix); } @@ -102,6 +102,8 @@ export function locate(document: vscode.TextDocument, pos: vscode.Position): vsc if (start !== end && isValidNumber(line.slice(start, end))) { return new vscode.Range(pos.line, start, pos.line, end); } + + return; } /** diff --git a/extensions/emmet/src/locateFile.ts b/extensions/emmet/src/locateFile.ts index 819248331fa..84ab1db9cf8 100644 --- a/extensions/emmet/src/locateFile.ts +++ b/extensions/emmet/src/locateFile.ts @@ -51,7 +51,7 @@ function resolveAbsolute(basePath: string, filePath: string): Promise { const next = (ctx: string) => { tryFile(path.resolve(ctx, filePath)) - .then(resolve, err => { + .then(resolve, () => { const dir = path.dirname(ctx); if (!dir || dir === ctx) { return reject(`Unable to locate absolute file ${filePath}`); diff --git a/extensions/emmet/src/selectItem.ts b/extensions/emmet/src/selectItem.ts index 30396073fc8..9fa8a5a461b 100644 --- a/extensions/emmet/src/selectItem.ts +++ b/extensions/emmet/src/selectItem.ts @@ -7,24 +7,13 @@ import * as vscode from 'vscode'; import { validate, parseDocument, isStyleSheet } from './util'; import { nextItemHTML, prevItemHTML } from './selectItemHTML'; import { nextItemStylesheet, prevItemStylesheet } from './selectItemStylesheet'; +import { HtmlNode, CssNode } from 'EmmetNode'; export function fetchSelectItem(direction: string): void { if (!validate() || !vscode.window.activeTextEditor) { return; } const editor = vscode.window.activeTextEditor; - - let nextItem: any; - let prevItem: any; - - if (isStyleSheet(editor.document.languageId)) { - nextItem = nextItemStylesheet; - prevItem = prevItemStylesheet; - } else { - nextItem = nextItemHTML; - prevItem = prevItemHTML; - } - let rootNode = parseDocument(editor.document); if (!rootNode) { return; @@ -35,7 +24,12 @@ export function fetchSelectItem(direction: string): void { const selectionStart = selection.isReversed ? selection.active : selection.anchor; const selectionEnd = selection.isReversed ? selection.anchor : selection.active; - let updatedSelection = direction === 'next' ? nextItem(selectionStart, selectionEnd, editor, rootNode) : prevItem(selectionStart, selectionEnd, editor, rootNode); + let updatedSelection; + if (isStyleSheet(editor.document.languageId)) { + updatedSelection = direction === 'next' ? nextItemStylesheet(selectionStart, selectionEnd, rootNode!) : prevItemStylesheet(selectionStart, selectionEnd, rootNode!); + } else { + updatedSelection = direction === 'next' ? nextItemHTML(selectionStart, selectionEnd, editor, rootNode!) : prevItemHTML(selectionStart, selectionEnd, editor, rootNode!); + } newSelections.push(updatedSelection ? updatedSelection : selection); }); editor.selections = newSelections; diff --git a/extensions/emmet/src/selectItemHTML.ts b/extensions/emmet/src/selectItemHTML.ts index 6fe1825fd87..0eee7a6cf72 100644 --- a/extensions/emmet/src/selectItemHTML.ts +++ b/extensions/emmet/src/selectItemHTML.ts @@ -6,6 +6,7 @@ import * as vscode from 'vscode'; import { getDeepestNode, findNextWord, findPrevWord, getHtmlNode } from './util'; import { HtmlNode } from 'EmmetNode'; +import { isNumber } from 'util'; export function nextItemHTML(selectionStart: vscode.Position, selectionEnd: vscode.Position, editor: vscode.TextEditor, rootNode: HtmlNode): vscode.Selection | undefined { let currentNode = getHtmlNode(editor.document, rootNode, selectionEnd, false); @@ -18,12 +19,12 @@ export function nextItemHTML(selectionStart: vscode.Position, selectionEnd: vsco if (currentNode.type !== 'comment') { // If cursor is in the tag name, select tag if (selectionEnd.isBefore(currentNode.open.start.translate(0, currentNode.name.length))) { - return getSelectionFromNode(currentNode, editor.document); + return getSelectionFromNode(currentNode); } // If cursor is in the open tag, look for attributes if (selectionEnd.isBefore(currentNode.open.end)) { - let attrSelection = getNextAttribute(selectionStart, selectionEnd, editor.document, currentNode); + let attrSelection = getNextAttribute(selectionStart, selectionEnd, currentNode); if (attrSelection) { return attrSelection; } @@ -50,7 +51,7 @@ export function nextItemHTML(selectionStart: vscode.Position, selectionEnd: vsco } } - return nextNode && getSelectionFromNode(nextNode, editor.document); + return nextNode && getSelectionFromNode(nextNode); } export function prevItemHTML(selectionStart: vscode.Position, selectionEnd: vscode.Position, editor: vscode.TextEditor, rootNode: HtmlNode): vscode.Selection | undefined { @@ -98,11 +99,11 @@ export function prevItemHTML(selectionStart: vscode.Position, selectionEnd: vsco return undefined; } - let attrSelection = getPrevAttribute(selectionStart, selectionEnd, editor.document, prevNode); - return attrSelection ? attrSelection : getSelectionFromNode(prevNode, editor.document); + let attrSelection = getPrevAttribute(selectionStart, selectionEnd, prevNode); + return attrSelection ? attrSelection : getSelectionFromNode(prevNode); } -function getSelectionFromNode(node: HtmlNode, document: vscode.TextDocument): vscode.Selection | undefined { +function getSelectionFromNode(node: HtmlNode): vscode.Selection | undefined { if (node && node.open) { let selectionStart = (node.open.start).translate(0, 1); let selectionEnd = selectionStart.translate(0, node.name.length); @@ -112,7 +113,7 @@ function getSelectionFromNode(node: HtmlNode, document: vscode.TextDocument): vs return undefined; } -function getNextAttribute(selectionStart: vscode.Position, selectionEnd: vscode.Position, document: vscode.TextDocument, node: HtmlNode): vscode.Selection | undefined { +function getNextAttribute(selectionStart: vscode.Position, selectionEnd: vscode.Position, node: HtmlNode): vscode.Selection | undefined { if (!node.attributes || node.attributes.length === 0 || node.type === 'comment') { return; @@ -153,6 +154,9 @@ function getNextAttribute(selectionStart: vscode.Position, selectionEnd: vscode. if (pos !== undefined) { let [newSelectionStartOffset, newSelectionEndOffset] = findNextWord(attr.value.toString(), pos); + if (!isNumber(newSelectionStartOffset) || !isNumber(newSelectionEndOffset)) { + return; + } if (newSelectionStartOffset >= 0 && newSelectionEndOffset >= 0) { const newSelectionStart = (attr.value.start).translate(0, newSelectionStartOffset); const newSelectionEnd = (attr.value.start).translate(0, newSelectionEndOffset); @@ -161,9 +165,11 @@ function getNextAttribute(selectionStart: vscode.Position, selectionEnd: vscode. } } + + return; } -function getPrevAttribute(selectionStart: vscode.Position, selectionEnd: vscode.Position, document: vscode.TextDocument, node: HtmlNode): vscode.Selection | undefined { +function getPrevAttribute(selectionStart: vscode.Position, selectionEnd: vscode.Position, node: HtmlNode): vscode.Selection | undefined { if (!node.attributes || node.attributes.length === 0 || node.type === 'comment') { return; @@ -194,12 +200,15 @@ function getPrevAttribute(selectionStart: vscode.Position, selectionEnd: vscode. let pos = selectionStart.isAfter(attr.value.end) ? attr.value.toString().length : selectionStart.character - attr.value.start.character; let [newSelectionStartOffset, newSelectionEndOffset] = findPrevWord(attr.value.toString(), pos); + if (!isNumber(newSelectionStartOffset) || !isNumber(newSelectionEndOffset)) { + return; + } if (newSelectionStartOffset >= 0 && newSelectionEndOffset >= 0) { const newSelectionStart = (attr.value.start).translate(0, newSelectionStartOffset); const newSelectionEnd = (attr.value.start).translate(0, newSelectionEndOffset); return new vscode.Selection(newSelectionStart, newSelectionEnd); } - - } + + return; } \ No newline at end of file diff --git a/extensions/emmet/src/selectItemStylesheet.ts b/extensions/emmet/src/selectItemStylesheet.ts index 29df659c564..420b4c1524d 100644 --- a/extensions/emmet/src/selectItemStylesheet.ts +++ b/extensions/emmet/src/selectItemStylesheet.ts @@ -7,7 +7,7 @@ import * as vscode from 'vscode'; import { getDeepestNode, findNextWord, findPrevWord, getNode } from './util'; import { Node, CssNode, Rule, Property } from 'EmmetNode'; -export function nextItemStylesheet(startOffset: vscode.Position, endOffset: vscode.Position, editor: vscode.TextEditor, rootNode: Node): vscode.Selection | undefined { +export function nextItemStylesheet(startOffset: vscode.Position, endOffset: vscode.Position, rootNode: Node): vscode.Selection | undefined { let currentNode = getNode(rootNode, endOffset, true); if (!currentNode) { currentNode = rootNode; @@ -17,12 +17,12 @@ export function nextItemStylesheet(startOffset: vscode.Position, endOffset: vsco } // Full property is selected, so select full property value next if (currentNode.type === 'property' && startOffset.isEqual(currentNode.start) && endOffset.isEqual(currentNode.end)) { - return getSelectionFromProperty(currentNode, editor.document, startOffset, endOffset, true, 'next'); + return getSelectionFromProperty(currentNode, startOffset, endOffset, true, 'next'); } // Part or whole of propertyValue is selected, so select the next word in the propertyValue if (currentNode.type === 'property' && startOffset.isAfterOrEqual((currentNode).valueToken.start) && endOffset.isBeforeOrEqual((currentNode).valueToken.end)) { - let singlePropertyValue = getSelectionFromProperty(currentNode, editor.document, startOffset, endOffset, false, 'next'); + let singlePropertyValue = getSelectionFromProperty(currentNode, startOffset, endOffset, false, 'next'); if (singlePropertyValue) { return singlePropertyValue; } @@ -31,7 +31,7 @@ export function nextItemStylesheet(startOffset: vscode.Position, endOffset: vsco // Cursor is in the selector or in a property if ((currentNode.type === 'rule' && endOffset.isBefore((currentNode).selectorToken.end)) || (currentNode.type === 'property' && endOffset.isBefore((currentNode).valueToken.end))) { - return getSelectionFromNode(currentNode, editor.document); + return getSelectionFromNode(currentNode); } // Get the first child of current node which is right after the cursor @@ -46,11 +46,11 @@ export function nextItemStylesheet(startOffset: vscode.Position, endOffset: vsco currentNode = currentNode.parent; } - return getSelectionFromNode(nextNode, editor.document); + return getSelectionFromNode(nextNode); } -export function prevItemStylesheet(startOffset: vscode.Position, endOffset: vscode.Position, editor: vscode.TextEditor, rootNode: CssNode): vscode.Selection | undefined { +export function prevItemStylesheet(startOffset: vscode.Position, endOffset: vscode.Position, rootNode: CssNode): vscode.Selection | undefined { let currentNode = getNode(rootNode, startOffset, false); if (!currentNode) { currentNode = rootNode; @@ -61,19 +61,19 @@ export function prevItemStylesheet(startOffset: vscode.Position, endOffset: vsco // Full property value is selected, so select the whole property next if (currentNode.type === 'property' && startOffset.isEqual((currentNode).valueToken.start) && endOffset.isEqual((currentNode).valueToken.end)) { - return getSelectionFromNode(currentNode, editor.document); + return getSelectionFromNode(currentNode); } // Part of propertyValue is selected, so select the prev word in the propertyValue if (currentNode.type === 'property' && startOffset.isAfterOrEqual((currentNode).valueToken.start) && endOffset.isBeforeOrEqual((currentNode).valueToken.end)) { - let singlePropertyValue = getSelectionFromProperty(currentNode, editor.document, startOffset, endOffset, false, 'prev'); + let singlePropertyValue = getSelectionFromProperty(currentNode, startOffset, endOffset, false, 'prev'); if (singlePropertyValue) { return singlePropertyValue; } } if (currentNode.type === 'property' || !currentNode.firstChild || (currentNode.type === 'rule' && startOffset.isBeforeOrEqual(currentNode.firstChild.start))) { - return getSelectionFromNode(currentNode, editor.document); + return getSelectionFromNode(currentNode); } // Select the child that appears just before the cursor @@ -83,12 +83,12 @@ export function prevItemStylesheet(startOffset: vscode.Position, endOffset: vsco } prevNode = getDeepestNode(prevNode); - return getSelectionFromProperty(prevNode, editor.document, startOffset, endOffset, false, 'prev'); + return getSelectionFromProperty(prevNode, startOffset, endOffset, false, 'prev'); } -function getSelectionFromNode(node: Node, document: vscode.TextDocument): vscode.Selection | undefined { +function getSelectionFromNode(node: Node): vscode.Selection | undefined { if (!node) { return; } @@ -98,7 +98,7 @@ function getSelectionFromNode(node: Node, document: vscode.TextDocument): vscode } -function getSelectionFromProperty(node: Node, document: vscode.TextDocument, selectionStart: vscode.Position, selectionEnd: vscode.Position, selectFullValue: boolean, direction: string): vscode.Selection | undefined { +function getSelectionFromProperty(node: Node, selectionStart: vscode.Position, selectionEnd: vscode.Position, selectFullValue: boolean, direction: string): vscode.Selection | undefined { if (!node || node.type !== 'property') { return; } diff --git a/extensions/emmet/src/test/cssAbbreviationAction.test.ts b/extensions/emmet/src/test/cssAbbreviationAction.test.ts index 014e66d046a..de5874f900f 100644 --- a/extensions/emmet/src/test/cssAbbreviationAction.test.ts +++ b/extensions/emmet/src/test/cssAbbreviationAction.test.ts @@ -56,7 +56,7 @@ suite('Tests for Expand Abbreviations (CSS)', () => { teardown(closeAllEditors); test('Expand abbreviation (CSS)', () => { - return withRandomFileEditor(cssContents, 'css', (editor, doc) => { + return withRandomFileEditor(cssContents, 'css', (editor, _) => { editor.selections = [new Selection(3, 1, 3, 6), new Selection(5, 1, 5, 6)]; return expandEmmetAbbreviation(null).then(() => { assert.equal(editor.document.getText(), cssContents.replace(/pos:f/g, 'position: fixed;')); @@ -75,7 +75,7 @@ suite('Tests for Expand Abbreviations (CSS)', () => { } `; - return withRandomFileEditor(testContent, 'css', (editor, doc) => { + return withRandomFileEditor(testContent, 'css', (editor, _) => { editor.selection = new Selection(3, 4, 3, 4); return expandEmmetAbbreviation(null).then(() => { assert.equal(editor.document.getText(), testContent); @@ -98,7 +98,7 @@ suite('Tests for Expand Abbreviations (CSS)', () => { nav# `; - return withRandomFileEditor(testContent, 'css', (editor, doc) => { + return withRandomFileEditor(testContent, 'css', (editor, _) => { editor.selection = new Selection(5, 4, 5, 4); return expandEmmetAbbreviation(null).then(() => { assert.equal(editor.document.getText(), testContent); @@ -120,7 +120,7 @@ nav# } `; - return withRandomFileEditor(testContent, 'css', (editor, doc) => { + return withRandomFileEditor(testContent, 'css', (editor, _) => { editor.selection = new Selection(2, 10, 2, 10); return expandEmmetAbbreviation(null).then(() => { assert.equal(editor.document.getText(), testContent); @@ -137,7 +137,7 @@ nav# test('Skip when typing the last property value in single line rules (CSS)', () => { const testContent = `.foo {padding: 10px; margin: a}`; - return withRandomFileEditor(testContent, 'css', (editor, doc) => { + return withRandomFileEditor(testContent, 'css', (editor, _) => { editor.selection = new Selection(0, 30, 0, 30); return expandEmmetAbbreviation(null).then(() => { assert.equal(editor.document.getText(), testContent); @@ -159,7 +159,7 @@ nav# } `; - return withRandomFileEditor(testContent, 'css', (editor, doc) => { + return withRandomFileEditor(testContent, 'css', (editor, _) => { const cancelSrc = new CancellationTokenSource(); const completionPromise1 = completionProvider.provideCompletionItems(editor.document, new Position(2, 12), cancelSrc.token, { triggerKind: CompletionTriggerKind.Invoke }); const completionPromise2 = completionProvider.provideCompletionItems(editor.document, new Position(2, 14), cancelSrc.token, { triggerKind: CompletionTriggerKind.Invoke }); @@ -198,7 +198,7 @@ nav# } `; - return withRandomFileEditor(testContent, 'css', (editor, doc) => { + return withRandomFileEditor(testContent, 'css', (editor, _) => { editor.selection = new Selection(3, 10, 3, 10); return expandEmmetAbbreviation(null).then(() => { assert.equal(editor.document.getText(), testContent); @@ -220,7 +220,7 @@ nav# } `; - return withRandomFileEditor(testContent, 'css', (editor, doc) => { + return withRandomFileEditor(testContent, 'css', (editor, _) => { const cancelSrc = new CancellationTokenSource(); const completionPromise1 = completionProvider.provideCompletionItems(editor.document, new Position(3, 12), cancelSrc.token, { triggerKind: CompletionTriggerKind.Invoke }); const completionPromise2 = completionProvider.provideCompletionItems(editor.document, new Position(3, 14), cancelSrc.token, { triggerKind: CompletionTriggerKind.Invoke }); @@ -258,7 +258,7 @@ nav# } `; - return withRandomFileEditor(testContent, 'css', (editor, doc) => { + return withRandomFileEditor(testContent, 'css', (editor, _) => { editor.selection = new Selection(2, 10, 2, 10); return expandEmmetAbbreviation(null).then(() => { assert.equal(editor.document.getText(), testContent); @@ -279,7 +279,7 @@ nav# } `; - return withRandomFileEditor(testContent, 'css', (editor, doc) => { + return withRandomFileEditor(testContent, 'css', (editor, _) => { const cancelSrc = new CancellationTokenSource(); const completionPromise1 = completionProvider.provideCompletionItems(editor.document, new Position(2, 12), cancelSrc.token, { triggerKind: CompletionTriggerKind.Invoke }); const completionPromise2 = completionProvider.provideCompletionItems(editor.document, new Position(2, 14), cancelSrc.token, { triggerKind: CompletionTriggerKind.Invoke }); @@ -317,7 +317,7 @@ nav# } `; - return withRandomFileEditor(testContent, 'css', (editor, doc) => { + return withRandomFileEditor(testContent, 'css', (editor, _) => { editor.selection = new Selection(2, 2, 2, 2); return expandEmmetAbbreviation(null).then(() => { assert.equal(editor.document.getText(), testContent); @@ -336,7 +336,7 @@ nav# const abbreviation = 'pos:f'; const expandedText = 'position: fixed;'; - return withRandomFileEditor(cssContents, 'css', (editor, doc) => { + return withRandomFileEditor(cssContents, 'css', (editor, _) => { editor.selection = new Selection(3, 1, 3, 6); const cancelSrc = new CancellationTokenSource(); const completionPromise1 = completionProvider.provideCompletionItems(editor.document, new Position(3, 6), cancelSrc.token, { triggerKind: CompletionTriggerKind.Invoke }); @@ -366,7 +366,7 @@ nav# }); test('Expand abbreviation (SCSS)', () => { - return withRandomFileEditor(scssContents, 'scss', (editor, doc) => { + return withRandomFileEditor(scssContents, 'scss', (editor, _) => { editor.selections = [ new Selection(3, 4, 3, 4), new Selection(5, 5, 5, 5), @@ -382,7 +382,7 @@ nav# test('Expand abbreviation in completion list (SCSS)', () => { - return withRandomFileEditor(scssContents, 'scss', (editor, doc) => { + return withRandomFileEditor(scssContents, 'scss', (editor, _) => { editor.selection = new Selection(3, 4, 3, 4); const cancelSrc = new CancellationTokenSource(); const completionPromise1 = completionProvider.provideCompletionItems(editor.document, new Position(3, 4), cancelSrc.token, { triggerKind: CompletionTriggerKind.Invoke }); @@ -439,7 +439,7 @@ m10 } `; - return withRandomFileEditor(scssContentsNoExpand, 'scss', (editor, doc) => { + return withRandomFileEditor(scssContentsNoExpand, 'scss', (editor, _) => { editor.selections = [ new Selection(1, 3, 1, 3), // outside rule new Selection(5, 15, 5, 15) // in the value part of property value @@ -462,7 +462,7 @@ m10 } `; - return withRandomFileEditor(scssContentsNoExpand, 'scss', (editor, doc) => { + return withRandomFileEditor(scssContentsNoExpand, 'scss', (editor, _) => { editor.selection = new Selection(1, 3, 1, 3); // outside rule const cancelSrc = new CancellationTokenSource(); let completionPromise = completionProvider.provideCompletionItems(editor.document, editor.selection.active, cancelSrc.token, { triggerKind: CompletionTriggerKind.Invoke }); @@ -487,7 +487,7 @@ m10 }); test('Skip when typing property values when there is a nested rule in the next line (SCSS)', () => { - return withRandomFileEditor(scssContents, 'scss', (editor, doc) => { + return withRandomFileEditor(scssContents, 'scss', (editor, _) => { editor.selection = new Selection(19, 10, 19, 10); return expandEmmetAbbreviation(null).then(() => { assert.equal(editor.document.getText(), scssContents); diff --git a/extensions/emmet/src/test/editPointSelectItemBalance.test.ts b/extensions/emmet/src/test/editPointSelectItemBalance.test.ts index 0c47043e867..e5960d5fe56 100644 --- a/extensions/emmet/src/test/editPointSelectItemBalance.test.ts +++ b/extensions/emmet/src/test/editPointSelectItemBalance.test.ts @@ -59,7 +59,7 @@ suite('Tests for Next/Previous Select/Edit point and Balance actions', () => { `; test('Emmet Next/Prev Edit point in html file', function (): any { - return withRandomFileEditor(htmlContents, '.html', (editor, doc) => { + return withRandomFileEditor(htmlContents, '.html', (editor, _) => { editor.selections = [new Selection(1, 5, 1, 5)]; let expectedNextEditPoints: [number, number][] = [[4, 16], [6, 8], [10, 2], [20, 0]]; @@ -79,7 +79,7 @@ suite('Tests for Next/Previous Select/Edit point and Balance actions', () => { }); test('Emmet Select Next/Prev Item in html file', function (): any { - return withRandomFileEditor(htmlContents, '.html', (editor, doc) => { + return withRandomFileEditor(htmlContents, '.html', (editor, _) => { editor.selections = [new Selection(2, 2, 2, 2)]; let expectedNextItemPoints: [number, number, number][] = [ @@ -114,7 +114,7 @@ suite('Tests for Next/Previous Select/Edit point and Balance actions', () => { }); test('Emmet Select Next/Prev item at boundary', function(): any { - return withRandomFileEditor(htmlContents, '.html', (editor, doc) => { + return withRandomFileEditor(htmlContents, '.html', (editor, _) => { editor.selections = [new Selection(4, 1, 4, 1)]; fetchSelectItem('next'); @@ -138,7 +138,7 @@ suite('Tests for Next/Previous Select/Edit point and Balance actions', () => { `; - return withRandomFileEditor(templateContents, '.html', (editor, doc) => { + return withRandomFileEditor(templateContents, '.html', (editor, _) => { editor.selections = [new Selection(2, 2, 2, 2)]; let expectedNextItemPoints: [number, number, number][] = [ @@ -167,7 +167,7 @@ suite('Tests for Next/Previous Select/Edit point and Balance actions', () => { }); test('Emmet Select Next/Prev Item in css file', function (): any { - return withRandomFileEditor(cssContents, '.css', (editor, doc) => { + return withRandomFileEditor(cssContents, '.css', (editor, _) => { editor.selections = [new Selection(0, 0, 0, 0)]; let expectedNextItemPoints: [number, number, number][] = [ @@ -198,7 +198,7 @@ suite('Tests for Next/Previous Select/Edit point and Balance actions', () => { }); test('Emmet Select Next/Prev Item in scss file with nested rules', function (): any { - return withRandomFileEditor(scssContents, '.scss', (editor, doc) => { + return withRandomFileEditor(scssContents, '.scss', (editor, _) => { editor.selections = [new Selection(0, 0, 0, 0)]; let expectedNextItemPoints: [number, number, number][] = [ @@ -229,7 +229,7 @@ suite('Tests for Next/Previous Select/Edit point and Balance actions', () => { }); test('Emmet Balance Out in html file', function (): any { - return withRandomFileEditor(htmlContents, 'html', (editor, doc) => { + return withRandomFileEditor(htmlContents, 'html', (editor, _) => { editor.selections = [new Selection(14, 6, 14, 10)]; let expectedBalanceOutRanges: [number, number, number, number][] = [ @@ -266,7 +266,7 @@ suite('Tests for Next/Previous Select/Edit point and Balance actions', () => { }); test('Emmet Balance In using the same stack as Balance out in html file', function (): any { - return withRandomFileEditor(htmlContents, 'html', (editor, doc) => { + return withRandomFileEditor(htmlContents, 'html', (editor, _) => { editor.selections = [new Selection(15, 6, 15, 10)]; let expectedBalanceOutRanges: [number, number, number, number][] = [ @@ -295,7 +295,7 @@ suite('Tests for Next/Previous Select/Edit point and Balance actions', () => { }); test('Emmet Balance In when selection doesnt span entire node or its inner contents', function (): any { - return withRandomFileEditor(htmlContents, 'html', (editor, doc) => { + return withRandomFileEditor(htmlContents, 'html', (editor, _) => { editor.selection = new Selection(13, 7, 13, 10); // Inside the open tag of `; - return withRandomFileEditor(contents, 'html', (editor, doc) => { + return withRandomFileEditor(contents, 'html', (editor, _) => { editor.selections = [new Selection(2, 0, 2, 0)]; const promise = wrapWithAbbreviation({ abbreviation: 'li.hello|c' }); if (!promise) { @@ -155,7 +155,7 @@ suite('Tests for Wrap with Abbreviations', () => { `; - return withRandomFileEditor(contents, 'html', (editor, doc) => { + return withRandomFileEditor(contents, 'html', (editor, _) => { editor.selections = [new Selection(1, 1, 1, 1)]; const promise = wrapWithAbbreviation({ abbreviation: 'div' }); if (!promise) { @@ -183,7 +183,7 @@ suite('Tests for Wrap with Abbreviations', () => { `; - return withRandomFileEditor(contents, 'html', (editor, doc) => { + return withRandomFileEditor(contents, 'html', (editor, _) => { editor.selections = [new Selection(3, 1, 3, 1)]; const promise = wrapWithAbbreviation({ abbreviation: 'div' }); if (!promise) { @@ -208,7 +208,7 @@ suite('Tests for Wrap with Abbreviations', () => { `; - return withRandomFileEditor(contents, 'html', (editor, doc) => { + return withRandomFileEditor(contents, 'html', (editor, _) => { editor.selections = [new Selection(1, 2, 1, 2)]; const promise = wrapWithAbbreviation({ abbreviation: 'ul>li>a' }); if (!promise) { @@ -237,7 +237,7 @@ suite('Tests for Wrap with Abbreviations', () => { `; - return withRandomFileEditor(contents, 'html', (editor, doc) => { + return withRandomFileEditor(contents, 'html', (editor, _) => { editor.selections = [new Selection(2, 2, 3, 33)]; const promise = wrapIndividualLinesWithAbbreviation({ abbreviation: 'ul>li.hello$*' }); if (!promise) { @@ -266,7 +266,7 @@ suite('Tests for Wrap with Abbreviations', () => { `; - return withRandomFileEditor(contents, 'html', (editor, doc) => { + return withRandomFileEditor(contents, 'html', (editor, _) => { editor.selections = [new Selection(2, 1, 4, 0)]; const promise = wrapIndividualLinesWithAbbreviation({ abbreviation: 'ul>li.hello$*' }); if (!promise) { @@ -297,7 +297,7 @@ suite('Tests for Wrap with Abbreviations', () => { `; - return withRandomFileEditor(contents, 'html', (editor, doc) => { + return withRandomFileEditor(contents, 'html', (editor, _) => { editor.selections = [new Selection(2, 2, 3, 33)]; const promise = wrapIndividualLinesWithAbbreviation({ abbreviation: 'ul>li.hello*|c' }); if (!promise) { @@ -326,7 +326,7 @@ suite('Tests for Wrap with Abbreviations', () => { `; - return withRandomFileEditor(contents, 'html', (editor, doc) => { + return withRandomFileEditor(contents, 'html', (editor, _) => { editor.selections = [new Selection(2, 3, 3, 16)]; const promise = wrapIndividualLinesWithAbbreviation({ abbreviation: 'ul>li.hello$*|t' }); if (!promise) { @@ -344,7 +344,7 @@ suite('Tests for Wrap with Abbreviations', () => { function testWrapWithAbbreviation(selections: Selection[], abbreviation: string, expectedContents: string): Thenable { - return withRandomFileEditor(htmlContentsForWrapTests, 'html', (editor, doc) => { + return withRandomFileEditor(htmlContentsForWrapTests, 'html', (editor, _) => { editor.selections = selections; const promise = wrapWithAbbreviation({ abbreviation }); if (!promise) { diff --git a/extensions/emmet/src/toggleComment.ts b/extensions/emmet/src/toggleComment.ts index e6d66d9acfe..fa8cdc5e68c 100644 --- a/extensions/emmet/src/toggleComment.ts +++ b/extensions/emmet/src/toggleComment.ts @@ -19,14 +19,6 @@ export function toggleComment(): Thenable | undefined { return; } const editor = vscode.window.activeTextEditor; - let toggleCommentInternal: (document: vscode.TextDocument, selection: vscode.Selection, rootNode: Node) => vscode.TextEdit[]; - - if (isStyleSheet(editor.document.languageId)) { - toggleCommentInternal = toggleCommentStylesheet; - } else { - toggleCommentInternal = toggleCommentHTML; - } - let rootNode = parseDocument(editor.document); if (!rootNode) { return; @@ -35,7 +27,7 @@ export function toggleComment(): Thenable | undefined { return editor.edit(editBuilder => { let allEdits: vscode.TextEdit[][] = []; editor.selections.reverse().forEach(selection => { - let edits = toggleCommentInternal(editor.document, selection, rootNode!); + let edits = isStyleSheet(editor.document.languageId) ? toggleCommentStylesheet(selection, rootNode) : toggleCommentHTML(editor.document, selection, rootNode!); if (edits.length > 0) { allEdits.push(edits); } @@ -76,7 +68,7 @@ function toggleCommentHTML(document: vscode.TextDocument, selection: vscode.Sele let buffer = new DocumentStreamReader(document, startNode.open.end, new vscode.Range(startNode.open.end, startNode.close.start)); let cssRootNode = parseStylesheet(buffer); - return toggleCommentStylesheet(document, selection, cssRootNode); + return toggleCommentStylesheet(selection, cssRootNode); } let allNodes: Node[] = getNodesInBetween(startNode, endNode); @@ -117,7 +109,7 @@ function getRangesToUnCommentHTML(node: Node, document: vscode.TextDocument): vs return unCommentTextEdits; } -function toggleCommentStylesheet(document: vscode.TextDocument, selection: vscode.Selection, rootNode: Stylesheet): vscode.TextEdit[] { +function toggleCommentStylesheet(selection: vscode.Selection, rootNode: Stylesheet): vscode.TextEdit[] { let selectionStart = selection.isReversed ? selection.active : selection.anchor; let selectionEnd = selection.isReversed ? selection.anchor : selection.active; diff --git a/extensions/emmet/src/updateImageSize.ts b/extensions/emmet/src/updateImageSize.ts index b7425edb314..3ac3f15a1be 100644 --- a/extensions/emmet/src/updateImageSize.ts +++ b/extensions/emmet/src/updateImageSize.ts @@ -35,7 +35,7 @@ export function updateImageSize() { return Promise.all(allUpdatesPromise).then((updates) => { return editor.edit(builder => { updates.forEach(update => { - update!.forEach((textEdit: TextEdit) => { + update.forEach((textEdit: TextEdit) => { builder.replace(textEdit.range, textEdit.newText); }); }); @@ -46,7 +46,7 @@ export function updateImageSize() { /** * Updates image size of context tag of HTML model */ -function updateImageSizeHTML(editor: TextEditor, position: Position): Promise { +function updateImageSizeHTML(editor: TextEditor, position: Position): Promise { const imageNode = getImageHTMLNode(editor, position); const src = imageNode && getImageSrcHTML(imageNode); @@ -64,11 +64,12 @@ function updateImageSizeHTML(editor: TextEditor, position: Position): Promise { console.warn('Error while updating image size:', err); return []; }); } -function updateImageSizeStyleTag(editor: TextEditor, position: Position): Promise { +function updateImageSizeStyleTag(editor: TextEditor, position: Position): Promise { const getPropertyInsiderStyleTag = (editor: TextEditor): Property | null => { const rootNode = parseDocument(editor.document); const currentNode = getNode(rootNode, position, true); @@ -86,14 +87,14 @@ function updateImageSizeStyleTag(editor: TextEditor, position: Position): Promis return updateImageSizeCSS(editor, position, getPropertyInsiderStyleTag); } -function updateImageSizeCSSFile(editor: TextEditor, position: Position): Promise { +function updateImageSizeCSSFile(editor: TextEditor, position: Position): Promise { return updateImageSizeCSS(editor, position, getImageCSSNode); } /** * Updates image size of context rule of stylesheet model */ -function updateImageSizeCSS(editor: TextEditor, position: Position, fetchNode: (editor: TextEditor, position: Position) => Property | null): Promise { +function updateImageSizeCSS(editor: TextEditor, position: Position, fetchNode: (editor: TextEditor, position: Position) => Property | null): Promise { const node = fetchNode(editor, position); const src = node && getImageSrcCSS(node, position); @@ -103,13 +104,14 @@ function updateImageSizeCSS(editor: TextEditor, position: Position, fetchNode: ( return locateFile(path.dirname(editor.document.fileName), src) .then(getImageSize) - .then((size: any): TextEdit[] | undefined => { + .then((size: any): TextEdit[] => { // since this action is asynchronous, we have to ensure that editor wasn’t // changed and user didn’t moved caret outside node const prop = fetchNode(editor, position); if (prop && getImageSrcCSS(prop, position) === src) { return updateCSSNode(editor, prop, size.width, size.height); } + return []; }) .catch(err => { console.warn('Error while updating image size:', err); return []; }); } @@ -261,12 +263,14 @@ function findUrlToken(node: Property, pos: Position): CssToken | undefined { url = token; return false; } + return true; }); if (url) { return url; } } + return; } /** diff --git a/extensions/emmet/src/util.ts b/extensions/emmet/src/util.ts index 589759c4300..c74fb6511e3 100644 --- a/extensions/emmet/src/util.ts +++ b/extensions/emmet/src/util.ts @@ -114,6 +114,7 @@ export function getEmmetMode(language: string, excludedLanguages: string[]): str if (emmetModes.indexOf(language) > -1) { return language; } + return; } /** @@ -259,7 +260,7 @@ export function parsePartialStylesheet(document: vscode.TextDocument, position: try { return parseStylesheet(new DocumentStreamReader(document, startPosition, new vscode.Range(startPosition, endPosition))); } catch (e) { - + return; } } @@ -351,7 +352,7 @@ export function getDeepestNode(node: Node | undefined): Node | undefined { return undefined; } -export function findNextWord(propertyValue: string, pos: number): [number, number] { +export function findNextWord(propertyValue: string, pos: number): [number | undefined, number | undefined] { let foundSpace = pos === -1; let foundStart = false; @@ -389,7 +390,7 @@ export function findNextWord(propertyValue: string, pos: number): [number, numbe return [newSelectionStart, newSelectionEnd]; } -export function findPrevWord(propertyValue: string, pos: number): [number, number] { +export function findPrevWord(propertyValue: string, pos: number): [number | undefined, number | undefined] { let foundSpace = pos === propertyValue.length; let foundStart = false; @@ -511,12 +512,13 @@ export function getEmmetConfiguration(syntax: string) { * Itereates by each child, as well as nested child's children, in their order * and invokes `fn` for each. If `fn` function returns `false`, iteration stops */ -export function iterateCSSToken(token: CssToken, fn: (x: any) => any) { +export function iterateCSSToken(token: CssToken, fn: (x: any) => any): boolean { for (let i = 0, il = token.size; i < il; i++) { if (fn(token.item(i)) === false || iterateCSSToken(token.item(i), fn) === false) { return false; } } + return true; } /** @@ -530,7 +532,7 @@ export function getCssPropertyFromRule(rule: Rule, name: string): Property | und * Returns css property under caret in given editor or `null` if such node cannot * be found */ -export function getCssPropertyFromDocument(editor: vscode.TextEditor, position: vscode.Position): Property | null | undefined { +export function getCssPropertyFromDocument(editor: vscode.TextEditor, position: vscode.Position): Property | null { const rootNode = parseDocument(editor.document); const node = getNode(rootNode, position, true); @@ -548,6 +550,8 @@ export function getCssPropertyFromDocument(editor: vscode.TextEditor, position: const node = getNode(rootNode, position, true); return (node && node.type === 'property') ? node : null; } + + return null; } @@ -569,6 +573,7 @@ export function getEmbeddedCssNodeIfAny(document: vscode.TextDocument, currentNo } } } + return; } export function isStyleAttribute(currentNode: Node | null, position: vscode.Position): boolean { diff --git a/extensions/emmet/tsconfig.json b/extensions/emmet/tsconfig.json index b035483b959..9db42025d86 100644 --- a/extensions/emmet/tsconfig.json +++ b/extensions/emmet/tsconfig.json @@ -1,12 +1,15 @@ { "compilerOptions": { - "target": "es6", - "lib": [ - "es2016" - ], + "target": "es2017", "module": "commonjs", + "lib": [ + "es6", + "es2015.promise" + ], "outDir": "./out", + "noImplicitReturns": true, "noUnusedLocals": true, + "noUnusedParameters": true, "strictNullChecks": true }, "exclude": [