diff --git a/.eslintrc.js b/.eslintrc.js index ea9afbf..2cdef08 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -8,6 +8,7 @@ module.exports = { extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"], rules: { "no-empty": 0, + "no-prototype-builtins": 0, "@typescript-eslint/ban-ts-comment": 0, "@typescript-eslint/no-namespace": 0, diff --git a/src/extension.ts b/src/extension.ts index 86862c7..ae380fe 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -20,7 +20,6 @@ import { config, configActivate, configDeactivate } from './plugin/lib/config' import { PropDefinitionProvider } from './plugin/PropDefinitionProvider' export function activate(context: ExtensionContext): void { - // console.log('minapp-vscode is active!') configActivate() if (!config.disableAutoConfig) { diff --git a/src/plugin/WxmlFormatter.ts b/src/plugin/WxmlFormatter.ts index fa95c90..e11a75b 100644 --- a/src/plugin/WxmlFormatter.ts +++ b/src/plugin/WxmlFormatter.ts @@ -17,7 +17,7 @@ type PrettierType = typeof Prettier export default class implements DocumentFormattingEditProvider, DocumentRangeFormattingEditProvider { constructor(public config: Config) {} - async format(doc: TextDocument, range: Range, options: FormattingOptions, prefix = '') { + async format(doc: TextDocument, range: Range, options: FormattingOptions, prefix = ''): Promise { const code = doc.getText(range) let content: string = code const resolveOptions = (prettier?: PrettierType) => diff --git a/src/plugin/lib/ScriptFile.ts b/src/plugin/lib/ScriptFile.ts index 3af35f9..5368063 100644 --- a/src/plugin/lib/ScriptFile.ts +++ b/src/plugin/lib/ScriptFile.ts @@ -25,8 +25,8 @@ const resultCache = new Map() const reservedWords = ['if', 'switch', 'catch', 'while', 'for', 'constructor'] function parseScriptFile(file: string, type: string, prop: string) { - let content = getFileContent(file) - let locs: PropInfo[] = [] + const content = getFileContent(file) + const locs: PropInfo[] = [] let reg: RegExp | null = null /** @@ -99,14 +99,29 @@ function parseScriptFile(file: string, type: string, prop: string) { }) .forEach(mat => { const property = mat[2] || mat[3] || prop - let pos = getPositionFromIndex(content, mat.index + mat[0].indexOf(property)) - let endPos = new Position(pos.line, pos.character + property.length) + const pos = getPositionFromIndex(content, mat.index + mat[0].indexOf(property)) + const endPos = new Position(pos.line, pos.character + property.length) locs.push({ loc: new Location(Uri.file(file), new Range(pos, endPos)), name: property, detail: mat[1] || mat[0], }) }) + + /** + * 没有匹配到任何有效的定义就直接字符搜索 + * 取第一个作为返回 + */ + if (locs.length === 0 && content && content.indexOf(prop) !== -1) { + const pos = getPositionFromIndex(content, content.indexOf(prop)) + const endPos = new Position(pos.line, pos.character + prop.length) + locs.push({ + loc: new Location(Uri.file(file), new Range(pos, endPos)), + name: prop, + detail: prop, + }) + } + return locs } @@ -153,7 +168,7 @@ function getVersion(file: string): number { * @param type * @param prop */ -export function getProp(wxmlFile: string, type: string, prop: string) { +export function getProp(wxmlFile: string, type: string, prop: string): PropInfo[] { const scriptFile = getScriptFile(wxmlFile) if (!scriptFile) return []