Skip to content

Commit

Permalink
feat: add support for ember-template-lint v4 (#347)
Browse files Browse the repository at this point in the history
* feat: add support for ember-template-lint v4

feat: add support for ember-template-lint v4

* chore: don't log error for broken js/hbs

* chore: remove unused import
  • Loading branch information
lifeart authored Jan 12, 2022
1 parent c2a0518 commit ef13b60
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ jobs:
with:
path: ./debug.*.log

- name: bundle (node)
run: yarn build:bundle:node

- name: bundle (worker)
run: yarn build:bundle:worker

coverage:
name: Coverage
Expand Down
18 changes: 11 additions & 7 deletions src/builtin-addons/core/template-completion-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,17 @@ export default class TemplateCompletionProvider {
return [];
}

return getTemplateBlocks(content).map((blockName: string) => {
return {
label: `:${blockName}`,
kind: CompletionItemKind.Variable,
detail: `Named block (Slot) for <${focusPath.tag}>`,
};
});
try {
return getTemplateBlocks(content).map((blockName: string) => {
return {
label: `:${blockName}`,
kind: CompletionItemKind.Variable,
detail: `Named block (Slot) for <${focusPath.tag}>`,
};
});
} catch (e) {
return [];
}
}
async onComplete(root: string, params: CompletionFunctionParams): Promise<CompletionItem[]> {
logDebugInfo('provideCompletions');
Expand Down
3 changes: 1 addition & 2 deletions src/definition-providers/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Server from './../server';
import { getExtension } from './../utils/file-extension';
import TemplateDefinitionProvider from './template';
import ScriptDefinitionProvider from './script';
import { logError } from '../utils/logger';

export default class DefinitionProvider {
public template!: TemplateDefinitionProvider;
Expand Down Expand Up @@ -34,7 +33,7 @@ export default class DefinitionProvider {
return null;
}
} catch (e) {
logError(e);
// logError(e);

return null;
}
Expand Down
43 changes: 25 additions & 18 deletions src/template-linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { URI } from 'vscode-uri';
import { log, logError, logDebugInfo } from './utils/logger';
import * as findUp from 'find-up';
import * as path from 'path';
import { pathToFileURL } from 'url';

import Server from './server';
import { Project } from './project';
Expand Down Expand Up @@ -212,16 +213,14 @@ export default class TemplateLinter {
return;
}

let nodePath = Files.resolveGlobalNodePath();
if (!getRequireSupport()) {
return;
}

// vs-code-online fix (we don't have global path, but it returned)
if (!nodePath || !(await this.server.fs.exists(nodePath))) {
// easy fix case
nodePath = 'node_modules';
const nodePath = 'node_modules';

if (!(await this.server.fs.exists(path.join(project.root, nodePath)))) {
return;
}
if (!(await this.server.fs.exists(path.join(project.root, nodePath)))) {
return;
}

const linterPath = await (Files.resolveModulePath(project.root, 'ember-template-lint', nodePath, () => {
Expand All @@ -232,21 +231,29 @@ export default class TemplateLinter {
return;
}

if (!getRequireSupport()) {
return;
}
try {
// commonjs behavior

// @ts-expect-error @todo - fix webpack imports
const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require;

// @ts-expect-error @todo - fix webpack imports
const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const linter: typeof Linter = requireFunc(linterPath);

// eslint-disable-next-line @typescript-eslint/no-var-requires
const linter: typeof Linter = requireFunc(linterPath);
this._linterCache.set(project, linter);

this._linterCache.set(project, linter);
return linter;
} catch {
// ember-template-lint v4 support (as esm module)
// using eval here to stop webpack from bundling it
const linter: typeof Linter = (await eval(`import("${pathToFileURL(linterPath)}")`)).default;

return linter;
this._linterCache.set(project, linter);

return linter;
}
} catch (error) {
log('Module ember-template-lint not found.');
log('Module ember-template-lint not found. ' + error.toString());
}
}
}
3 changes: 1 addition & 2 deletions src/utils/usages-api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { extractTokensFromTemplate } from './template-tokens-collector';
import { MatchResultType } from './path-matcher';
import { fsProvider } from '../fs-provider';
import { logError, logDebugInfo } from './logger';
import { logDebugInfo } from './logger';

export interface TemplateTokenMeta {
source: string;
Expand Down Expand Up @@ -150,7 +150,6 @@ async function extractTokens() {
};
}
} catch (e) {
logError(e);
//
} finally {
tokenQueue.shift();
Expand Down

0 comments on commit ef13b60

Please sign in to comment.