Skip to content

Commit 37c3cd1

Browse files
authored
Start moving emmet extension to strict mode (microsoft#37740)
* Start moving emmet to strict mode First part of moving the emmet extension to strict mode TypeScript. This change focuses on adding annotations when things can be undefined and removing jsdoc type comments * Fix a few more errors * Fix compile errors * Tiny updates
1 parent 1d3f3bc commit 37c3cd1

14 files changed

Lines changed: 129 additions & 187 deletions

extensions/emmet/src/abbreviationActions.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ interface ExpandAbbreviationInput {
1717
filter?: string;
1818
}
1919

20-
export function wrapWithAbbreviation(args) {
21-
if (!validate(false)) {
20+
export function wrapWithAbbreviation(args: any) {
21+
if (!validate(false) || !vscode.window.activeTextEditor) {
2222
return;
2323
}
2424

@@ -50,8 +50,8 @@ export function wrapWithAbbreviation(args) {
5050
});
5151
}
5252

53-
export function wrapIndividualLinesWithAbbreviation(args) {
54-
if (!validate(false)) {
53+
export function wrapIndividualLinesWithAbbreviation(args: any) {
54+
if (!validate(false) || !vscode.window.activeTextEditor) {
5555
return;
5656
}
5757

@@ -88,7 +88,7 @@ export function wrapIndividualLinesWithAbbreviation(args) {
8888

8989
}
9090

91-
export function expandEmmetAbbreviation(args): Thenable<boolean> {
91+
export function expandEmmetAbbreviation(args: any): Thenable<boolean> {
9292
const syntax = getSyntaxFromArgs(args);
9393
if (!syntax || !validate()) {
9494
return fallbackTab();
@@ -179,7 +179,7 @@ export function expandEmmetAbbreviation(args): Thenable<boolean> {
179179
});
180180
}
181181

182-
function fallbackTab(): Thenable<boolean> {
182+
function fallbackTab(): Thenable<boolean | undefined> | undefined {
183183
if (vscode.workspace.getConfiguration('emmet')['triggerExpansionOnTab'] === true) {
184184
return vscode.commands.executeCommand('tab');
185185
}
@@ -226,7 +226,8 @@ export function isValidLocationForEmmetAbbreviation(currentNode: Node, syntax: s
226226

227227
const currentHtmlNode = <HtmlNode>currentNode;
228228
if (currentHtmlNode.close) {
229-
return getInnerRange(currentHtmlNode).contains(position);
229+
const innerRange = getInnerRange(currentHtmlNode);
230+
return !!innerRange && innerRange.contains(position);
230231
}
231232

232233
return false;
@@ -247,7 +248,7 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex
247248
// Snippet to replace at multiple cursors are not the same
248249
// `editor.insertSnippet` will have to be called for each instance separately
249250
// We will not be able to maintain multiple cursors after snippet insertion
250-
let insertPromises = [];
251+
let insertPromises: Thenable<boolean>[] = [];
251252
if (!insertSameSnippet) {
252253
expandAbbrList.forEach((expandAbbrInput: ExpandAbbreviationInput) => {
253254
let expandedText = expandAbbr(expandAbbrInput);
@@ -278,7 +279,7 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex
278279
/**
279280
* Expands abbreviation as detailed in given input.
280281
*/
281-
function expandAbbr(input: ExpandAbbreviationInput): string {
282+
function expandAbbr(input: ExpandAbbreviationInput): string | undefined {
282283
const helper = getEmmetHelper();
283284
const expandOptions = helper.getExpandOptions(input.syntax, getEmmetConfiguration(input.syntax), input.filter);
284285

@@ -322,7 +323,7 @@ function expandAbbr(input: ExpandAbbreviationInput): string {
322323

323324
}
324325

325-
function getSyntaxFromArgs(args: any): string {
326+
function getSyntaxFromArgs(args: any): string | undefined {
326327
let editor = vscode.window.activeTextEditor;
327328
if (!editor) {
328329
vscode.window.showInformationMessage('No editor is active.');

extensions/emmet/src/balance.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ export function balanceIn() {
1616
}
1717

1818
function balance(out: boolean) {
19-
let editor = vscode.window.activeTextEditor;
20-
if (!validate(false)) {
19+
if (!validate(false) || !vscode.window.activeTextEditor) {
2120
return;
2221
}
23-
22+
const editor = vscode.window.activeTextEditor;
2423
let rootNode = <HtmlNode>parseDocument(editor.document);
2524
if (!rootNode) {
2625
return;
@@ -30,7 +29,7 @@ function balance(out: boolean) {
3029
let newSelections: vscode.Selection[] = [];
3130
editor.selections.forEach(selection => {
3231
let range = getRangeFunction(editor.document, selection, rootNode);
33-
newSelections.push(range ? range : selection);
32+
newSelections.push(range);
3433
});
3534

3635
editor.selection = newSelections[0];
@@ -40,7 +39,7 @@ function balance(out: boolean) {
4039
function getRangeToBalanceOut(document: vscode.TextDocument, selection: vscode.Selection, rootNode: HtmlNode): vscode.Selection {
4140
let nodeToBalance = <HtmlNode>getNode(rootNode, selection.start);
4241
if (!nodeToBalance) {
43-
return;
42+
return selection;
4443
}
4544
if (!nodeToBalance.close) {
4645
return new vscode.Selection(nodeToBalance.start, nodeToBalance.end);
@@ -55,13 +54,13 @@ function getRangeToBalanceOut(document: vscode.TextDocument, selection: vscode.S
5554
if (outerSelection.contains(selection) && !outerSelection.isEqual(selection)) {
5655
return outerSelection;
5756
}
58-
return;
57+
return selection;
5958
}
6059

6160
function getRangeToBalanceIn(document: vscode.TextDocument, selection: vscode.Selection, rootNode: HtmlNode): vscode.Selection {
6261
let nodeToBalance = <HtmlNode>getNode(rootNode, selection.start, true);
6362
if (!nodeToBalance) {
64-
return;
63+
return selection;
6564
}
6665

6766
if (selection.start.isEqual(nodeToBalance.start)
@@ -71,7 +70,7 @@ function getRangeToBalanceIn(document: vscode.TextDocument, selection: vscode.Se
7170
}
7271

7372
if (!nodeToBalance.firstChild) {
74-
return;
73+
return selection;
7574
}
7675

7776
if (selection.start.isEqual(nodeToBalance.firstChild.start)

extensions/emmet/src/bufferStream.ts

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,16 @@ export class DocumentStreamReader {
4343

4444
/**
4545
* Creates a new stream instance which is limited to given range for given document
46-
* @param {Position} start
47-
* @param {Position} end
48-
* @return {DocumentStreamReader}
4946
*/
50-
limit(start, end) {
47+
limit(start: Position, end: Position): DocumentStreamReader {
5148
return new DocumentStreamReader(this.document, start, new Range(start, end));
5249
}
5350

5451
/**
5552
* Returns the next character code in the stream without advancing it.
5653
* Will return NaN at the end of the file.
57-
* @returns {Number}
5854
*/
59-
peek() {
55+
peek(): number {
6056
if (this.eof()) {
6157
return NaN;
6258
}
@@ -67,9 +63,8 @@ export class DocumentStreamReader {
6763
/**
6864
* Returns the next character in the stream and advances it.
6965
* Also returns NaN when no more characters are available.
70-
* @returns {Number}
7166
*/
72-
next() {
67+
next(): number {
7368
if (this.eof()) {
7469
return NaN;
7570
}
@@ -95,9 +90,8 @@ export class DocumentStreamReader {
9590
/**
9691
* Backs up the stream n characters. Backing it up further than the
9792
* start of the current token will cause things to break, so be careful.
98-
* @param {Number} n
9993
*/
100-
backUp(n) {
94+
backUp(n: number) {
10195
let row = this.pos.line;
10296
let column = this.pos.character;
10397
column -= (n || 1);
@@ -117,39 +111,31 @@ export class DocumentStreamReader {
117111
/**
118112
* Get the string between the start of the current token and the
119113
* current stream position.
120-
* @returns {String}
121114
*/
122-
current() {
115+
current(): string {
123116
return this.substring(this.start, this.pos);
124117
}
125118

126119
/**
127120
* Returns contents for given range
128-
* @param {Position} from
129-
* @param {Position} to
130-
* @return {String}
131121
*/
132-
substring(from, to) {
122+
substring(from: Position, to: Position): string {
133123
return this.document.getText(new Range(from, to));
134124
}
135125

136126
/**
137127
* Creates error object with current stream state
138-
* @param {String} message
139-
* @return {Error}
140128
*/
141-
error(message) {
129+
error(message: string): Error {
142130
const err = new Error(`${message} at row ${this.pos.line}, column ${this.pos.character}`);
143131

144132
return err;
145133
}
146134

147135
/**
148136
* Returns line length of given row, including line ending
149-
* @param {Number} row
150-
* @return {Number}
151137
*/
152-
_lineLength(row) {
138+
_lineLength(row: number): number {
153139
if (row === this.document.lineCount - 1) {
154140
return this.document.lineAt(row).text.length;
155141
}
@@ -161,10 +147,8 @@ export class DocumentStreamReader {
161147
* and returns a boolean. If the next character in the stream 'matches'
162148
* the given argument, it is consumed and returned.
163149
* Otherwise, `false` is returned.
164-
* @param {Number|Function} match
165-
* @returns {Boolean}
166150
*/
167-
eat(match) {
151+
eat(match: number | Function): boolean {
168152
const ch = this.peek();
169153
const ok = typeof match === 'function' ? match(ch) : ch === match;
170154

@@ -178,10 +162,8 @@ export class DocumentStreamReader {
178162
/**
179163
* Repeatedly calls <code>eat</code> with the given argument, until it
180164
* fails. Returns <code>true</code> if any characters were eaten.
181-
* @param {Object} match
182-
* @returns {Boolean}
183165
*/
184-
eatWhile(match) {
166+
eatWhile(match: number | Function): boolean {
185167
const start = this.pos;
186168
while (!this.eof() && this.eat(match)) { }
187169
return !this.pos.isEqual(start);

extensions/emmet/src/defaultCompletionProvider.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const allowedMimeTypesInScriptTag = ['text/html', 'text/plain', 'text/x-template
1212

1313
export class DefaultCompletionItemProvider implements vscode.CompletionItemProvider {
1414

15-
public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.CompletionList> {
15+
public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.CompletionList | undefined> | undefined {
1616
const mappedLanguages = getMappingForIncludedLanguages();
1717
const emmetConfig = vscode.workspace.getConfiguration('emmet');
1818

@@ -45,8 +45,8 @@ export class DefaultCompletionItemProvider implements vscode.CompletionItemProvi
4545
if (abbreviation.startsWith('this.')) {
4646
noiseCheckPromise = Promise.resolve(true);
4747
} else {
48-
noiseCheckPromise = vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider', document.uri).then((symbols: vscode.SymbolInformation[]) => {
49-
return symbols.find(x => abbreviation === x.name || (abbreviation.startsWith(x.name + '.') && !/>|\*|\+/.test(abbreviation)));
48+
noiseCheckPromise = vscode.commands.executeCommand<vscode.SymbolInformation[]>('vscode.executeDocumentSymbolProvider', document.uri).then((symbols: vscode.SymbolInformation[] | undefined) => {
49+
return symbols && symbols.find(x => abbreviation === x.name || (abbreviation.startsWith(x.name + '.') && !/>|\*|\+/.test(abbreviation)));
5050
});
5151
}
5252
}
@@ -88,7 +88,7 @@ export class DefaultCompletionItemProvider implements vscode.CompletionItemProvi
8888
* @param document vscode.Textdocument
8989
* @param position vscode.Position position of the abbreviation that needs to be expanded
9090
*/
91-
private syntaxHelper(syntax: string, document: vscode.TextDocument, position: vscode.Position): string {
91+
private syntaxHelper(syntax: string | undefined, document: vscode.TextDocument, position: vscode.Position): string | undefined {
9292
if (!syntax) {
9393
return syntax;
9494
}

extensions/emmet/src/editPoint.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,42 @@ import * as vscode from 'vscode';
77
import { validate } from './util';
88

99
export function fetchEditPoint(direction: string): void {
10-
let editor = vscode.window.activeTextEditor;
11-
if (!validate()) {
10+
if (!validate() || !vscode.window.activeTextEditor) {
1211
return;
1312
}
13+
const editor = vscode.window.activeTextEditor;
1414

1515
let newSelections: vscode.Selection[] = [];
1616
editor.selections.forEach(selection => {
17-
let updatedSelection = direction === 'next' ? nextEditPoint(selection.anchor, editor) : prevEditPoint(selection.anchor, editor);
18-
newSelections.push(updatedSelection ? updatedSelection : selection);
17+
let updatedSelection = direction === 'next' ? nextEditPoint(selection, editor) : prevEditPoint(selection, editor);
18+
newSelections.push(updatedSelection);
1919
});
2020
editor.selections = newSelections;
2121
editor.revealRange(editor.selections[editor.selections.length - 1]);
2222
}
2323

24-
function nextEditPoint(position: vscode.Position, editor: vscode.TextEditor): vscode.Selection {
25-
for (let lineNum = position.line; lineNum < editor.document.lineCount; lineNum++) {
26-
let updatedSelection = findEditPoint(lineNum, editor, position, 'next');
24+
function nextEditPoint(selection: vscode.Selection, editor: vscode.TextEditor): vscode.Selection {
25+
for (let lineNum = selection.anchor.line; lineNum < editor.document.lineCount; lineNum++) {
26+
let updatedSelection = findEditPoint(lineNum, editor, selection.anchor, 'next');
2727
if (updatedSelection) {
2828
return updatedSelection;
2929
}
3030
}
31+
return selection;
3132
}
3233

33-
function prevEditPoint(position: vscode.Position, editor: vscode.TextEditor): vscode.Selection {
34-
for (let lineNum = position.line; lineNum >= 0; lineNum--) {
35-
let updatedSelection = findEditPoint(lineNum, editor, position, 'prev');
34+
function prevEditPoint(selection: vscode.Selection, editor: vscode.TextEditor): vscode.Selection {
35+
for (let lineNum = selection.anchor.line; lineNum >= 0; lineNum--) {
36+
let updatedSelection = findEditPoint(lineNum, editor, selection.anchor, 'prev');
3637
if (updatedSelection) {
3738
return updatedSelection;
3839
}
3940
}
41+
return selection;
4042
}
4143

4244

43-
function findEditPoint(lineNum: number, editor: vscode.TextEditor, position: vscode.Position, direction: string): vscode.Selection {
45+
function findEditPoint(lineNum: number, editor: vscode.TextEditor, position: vscode.Position, direction: string): vscode.Selection | undefined {
4446
let line = editor.document.lineAt(lineNum);
4547
let lineContent = line.text;
4648

extensions/emmet/src/evaluateMathExpression.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import evaluate from '@emmetio/math-expression';
1010
import { DocumentStreamReader } from './bufferStream';
1111

1212
export function evaluateMathExpression() {
13-
let editor = vscode.window.activeTextEditor;
14-
if (!editor) {
13+
if (!vscode.window.activeTextEditor) {
1514
vscode.window.showInformationMessage('No editor is active');
1615
return;
1716
}
17+
const editor = vscode.window.activeTextEditor;
1818
const stream = new DocumentStreamReader(editor.document);
1919
editor.edit(editBuilder => {
2020
editor.selections.forEach(selection => {

0 commit comments

Comments
 (0)