Emmet strict mode move, part 2 (#37840)

Continue moving emmet extension to strict mode. This change does the following:

- Remove jsdoc types. These are unused in ts files and can easily get out of date
- Annotate when something can return undefined
- Add null checks for when something can be undefined
- Add explicit types when something can be any
This commit is contained in:
Matt Bierner
2017-11-20 14:08:49 -08:00
committed by GitHub
parent c93e6c60ab
commit 7965160a6d
13 changed files with 85 additions and 103 deletions

View File

@@ -37,7 +37,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);
});
});
@@ -49,7 +49,9 @@ export function updateImageSize() {
* Updates image size of context tag of HTML model
*/
function updateImageSizeHTML(editor: TextEditor, position: Position): Promise<TextEdit[] | undefined> {
const src = getImageSrcHTML(getImageHTMLNode(editor, position));
const imageNode = getImageHTMLNode(editor, position);
const src = imageNode && getImageSrcHTML(imageNode);
if (!src) {
return updateImageSizeStyleTag(editor, position);
@@ -61,15 +63,15 @@ function updateImageSizeHTML(editor: TextEditor, position: Position): Promise<Te
// since this action is asynchronous, we have to ensure that editor wasnt
// changed and user didnt moved caret outside <img> node
const img = getImageHTMLNode(editor, position);
if (getImageSrcHTML(img) === src) {
if (img && getImageSrcHTML(img) === src) {
return updateHTMLTag(editor, img, size.width, size.height);
}
})
.catch(err => { console.warn('Error while updating image size:', err); return []; });
}
function updateImageSizeStyleTag(editor: TextEditor, position: Position): Promise<TextEdit[]> {
const getPropertyInsiderStyleTag = (editor: TextEditor): Property | undefined => {
function updateImageSizeStyleTag(editor: TextEditor, position: Position): Promise<TextEdit[] | undefined> {
const getPropertyInsiderStyleTag = (editor: TextEditor): Property | null => {
const rootNode = parseDocument(editor.document);
const currentNode = <HtmlNode>getNode(rootNode, position);
if (currentNode && currentNode.name === 'style'
@@ -78,23 +80,24 @@ function updateImageSizeStyleTag(editor: TextEditor, position: Position): Promis
let buffer = new DocumentStreamReader(editor.document, currentNode.open.end, new Range(currentNode.open.end, currentNode.close.start));
let rootNode = parseStylesheet(buffer);
const node = getNode(rootNode, position);
return (node && node.type === 'property') ? <Property>node : undefined;
return (node && node.type === 'property') ? <Property>node : null;
}
return null;
};
return updateImageSizeCSS(editor, position, getPropertyInsiderStyleTag);
}
function updateImageSizeCSSFile(editor: TextEditor, position: Position): Promise<TextEdit[]> {
function updateImageSizeCSSFile(editor: TextEditor, position: Position): Promise<TextEdit[] | undefined> {
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 | undefined): Promise<TextEdit[]> {
const src = getImageSrcCSS(fetchNode(editor, position), position);
function updateImageSizeCSS(editor: TextEditor, position: Position, fetchNode: (editor: TextEditor, position: Position) => Property | null): Promise<TextEdit[] | undefined> {
const node = fetchNode(editor, position);
const src = node && getImageSrcCSS(node, position);
if (!src) {
return Promise.reject(new Error('No valid image source'));
@@ -102,11 +105,11 @@ function updateImageSizeCSS(editor: TextEditor, position: Position, fetchNode: (
return locateFile(path.dirname(editor.document.fileName), src)
.then(getImageSize)
.then((size: any) => {
.then((size: any): TextEdit[] | undefined => {
// since this action is asynchronous, we have to ensure that editor wasnt
// changed and user didnt moved caret outside <img> node
const prop = fetchNode(editor, position);
if (getImageSrcCSS(prop, position) === src) {
if (prop && getImageSrcCSS(prop, position) === src) {
return updateCSSNode(editor, prop, size.width, size.height);
}
})
@@ -116,10 +119,8 @@ function updateImageSizeCSS(editor: TextEditor, position: Position, fetchNode: (
/**
* Returns <img> node under caret in given editor or `null` if such node cannot
* be found
* @param {TextEditor} editor
* @return {HtmlNode}
*/
function getImageHTMLNode(editor: TextEditor, position: Position): HtmlNode {
function getImageHTMLNode(editor: TextEditor, position: Position): HtmlNode | null {
const rootNode = parseDocument(editor.document);
const node = <HtmlNode>getNode(rootNode, position, true);
@@ -129,10 +130,8 @@ function getImageHTMLNode(editor: TextEditor, position: Position): HtmlNode {
/**
* Returns css property under caret in given editor or `null` if such node cannot
* be found
* @param {TextEditor} editor
* @return {Property}
*/
function getImageCSSNode(editor: TextEditor, position: Position): Property {
function getImageCSSNode(editor: TextEditor, position: Position): Property | null {
const rootNode = parseDocument(editor.document);
const node = getNode(rootNode, position, true);
return node && node.type === 'property' ? <Property>node : null;
@@ -173,10 +172,6 @@ function getImageSrcCSS(node: Property | undefined, position: Position): string
/**
* Updates size of given HTML node
* @param {TextEditor} editor
* @param {HtmlNode} node
* @param {number} width
* @param {number} height
*/
function updateHTMLTag(editor: TextEditor, node: HtmlNode, width: number, height: number): TextEdit[] {
const srcAttr = getAttribute(node, 'src');
@@ -245,7 +240,7 @@ function updateCSSNode(editor: TextEditor, srcProp: Property, width: number, hei
*/
function getAttribute(node: HtmlNode, attrName: string): Attribute {
attrName = attrName.toLowerCase();
return node && (node.open as any).attributes.find(attr => attr.name.value.toLowerCase() === attrName);
return node && (node.open as any).attributes.find((attr: any) => attr.name.value.toLowerCase() === attrName);
}
/**