Skip to content

Commit

Permalink
chore: Adding handlebars analyzer (#1006)
Browse files Browse the repository at this point in the history
  • Loading branch information
scalvert authored May 20, 2021
1 parent 05273bb commit ac1d0f5
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Task, BaseTask, LintResult, trimCwd, AstAnalyzer, sarifBuilder } from '@checkup/core';
import { promises } from 'fs';
import {
Task,
BaseTask,
LintResult,
trimCwd,
sarifBuilder,
HandlebarsAnalyzer,
} from '@checkup/core';
import { Result } from 'sarif';

const fs = require('fs');
const { parse, traverse } = require('ember-template-recast');

const TEMPLATE_LINT_DISABLE = 'template-lint-disable';

export default class EmberTemplateLintDisableTask extends BaseTask implements Task {
Expand Down Expand Up @@ -59,9 +64,9 @@ async function getTemplateLintDisables(filePaths: string[], cwd: string) {

await Promise.all(
filePaths.map((filePath) => {
return fs.promises.readFile(filePath, 'utf8').then((fileContents: string) => {
return promises.readFile(filePath, 'utf8').then((source: string) => {
let accumulator = new TemplateLintDisableAccumulator(filePath);
let analyzer = new AstAnalyzer(fileContents, parse, traverse);
let analyzer = new HandlebarsAnalyzer(source);

analyzer.analyze(accumulator.visitors);
data.push(...accumulator.data);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { promises } from 'fs';
import { Task, BaseTask, trimCwd, LintResult, AstAnalyzer, sarifBuilder } from '@checkup/core';

import * as t from '@babel/types';
import { parse, visit } from 'recast';
import { Visitor } from 'ast-types';
import { Result } from 'sarif';

const fs = require('fs');

const ESLINT_DISABLE_REGEX = /^eslint-disable(?:-next-line|-line)*/gi;

export default class EslintDisableTask extends BaseTask implements Task {
Expand Down Expand Up @@ -59,7 +58,7 @@ async function getEslintDisables(filePaths: string[], cwd: string) {

await Promise.all(
filePaths.map((filePath) => {
return fs.promises.readFile(filePath, 'utf8').then((fileContents: string) => {
return promises.readFile(filePath, 'utf8').then((fileContents: string) => {
let accumulator = new ESLintDisableAccumulator(filePath);
let analyzer = new AstAnalyzer<t.File, Visitor<any>, typeof parse, typeof visit>(
fileContents,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"bugs": "https://github.com/checkupjs/checkup/issues",
"dependencies": {
"@babel/parser": "^7.14.2",
"@glimmer/syntax": "^0.79.1",
"@types/micromatch": "^4.0.1",
"@types/sarif": "^2.1.3",
"ajv": "^6.12.6",
Expand All @@ -17,6 +18,7 @@
"debug": "^4.3.1",
"deepmerge": "^4.2.2",
"ember-template-lint": "^3.2.0",
"ember-template-recast": "^5.0.3",
"eslint": "^7.16.0",
"fs-extra": "^9.1.0",
"globby": "^11.0.1",
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/analyzers/ast-analyzer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
type ParserWithOptions<TAst> = (source: string, parserOptions?: any) => TAst;
type Parser<TAst> = (source: string) => TAst;

export default class AstAnalyzer<
TAst,
TVisitors,
TParse extends (source: string, parserOptions?: any) => TAst,
TParse extends Parser<TAst> | ParserWithOptions<TAst>,
TTraverse extends (ast: TAst, visitors: TVisitors) => any
> {
ast: TAst;
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/analyzers/handlebars-analyzer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { AST, NodeVisitor, parse, traverse } from 'ember-template-recast';
import AstAnalyzer from './ast-analyzer';

export default class HandlebarsAnalyzer extends AstAnalyzer<
AST.Template,
NodeVisitor,
typeof parse,
typeof traverse
> {
constructor(source: string) {
super(source, parse, traverse);
}
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export { default as AstAnalyzer } from './analyzers/ast-analyzer';
export { default as JavaScriptAnalyzer } from './analyzers/javascript-analyzer';
export { default as TypeScriptAnalyzer } from './analyzers/typescript-analyzer';
export { default as JsonAnalyzer } from './analyzers/json-analyzer';
export { default as HandlebarsAnalyzer } from './analyzers/handlebars-analyzer';
export { default as ESLintAnalyzer } from './analyzers/eslint-analyzer';
export { default as EmberTemplateLintAnalyzer } from './analyzers/ember-template-lint-analyzer';
export { default as DependencyAnalyzer } from './analyzers/dependency-analyzer';
Expand Down
7 changes: 1 addition & 6 deletions packages/plugin/src/generate-docs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { join, extname, parse } from 'path';
import * as recast from 'recast';
import { ensureDir, readdir, readFile, existsSync, writeFile } from 'fs-extra';
import * as t from '@babel/types';
import traverse, { TraverseOptions } from '@babel/traverse';
import { getPluginName, AstAnalyzer, getShorthandName, TypeScriptAnalyzer } from '@checkup/core';

type RecastParse = typeof recast.parse;
type BabelTraverse = typeof traverse;
import { getPluginName, getShorthandName, TypeScriptAnalyzer } from '@checkup/core';

function getDocsFile(docsFilePath: string) {
if (existsSync(docsFilePath)) {
Expand Down
60 changes: 59 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,13 @@
dependencies:
"@simple-dom/interface" "^1.4.0"

"@glimmer/interfaces@0.79.1":
version "0.79.1"
resolved "https://registry.yarnpkg.com/@glimmer/interfaces/-/interfaces-0.79.1.tgz#c25bb029450e8666d7a4e09da8669437eee237d1"
integrity sha512-Mz7118uYtHAodvuNPQFzJLwCleBsh1bx/7H5LH6liCE720OOz1PD2hhamMntpetuvgkpX8OnVDp99BB7bH3/Hg==
dependencies:
"@simple-dom/interface" "^1.4.0"

"@glimmer/reference@^0.65.0":
version "0.65.1"
resolved "https://registry.yarnpkg.com/@glimmer/reference/-/reference-0.65.1.tgz#44515e7bef58c9ce4ea65c2eb3403caec11a2060"
Expand All @@ -595,6 +602,16 @@
"@handlebars/parser" "^1.1.0"
simple-html-tokenizer "^0.5.10"

"@glimmer/syntax@^0.79.1":
version "0.79.1"
resolved "https://registry.yarnpkg.com/@glimmer/syntax/-/syntax-0.79.1.tgz#845f19424705e27f4ef8ab1358057c77da8b4fbc"
integrity sha512-dbdK4un1u2GB2i8n7PUOC+nKS6HQuMxFmpiy/GNL8hIvf6i33nqtdAZ7S4UJx65V7weOqLMwhrzhEN3+901I8g==
dependencies:
"@glimmer/interfaces" "0.79.1"
"@glimmer/util" "0.79.1"
"@handlebars/parser" "~2.0.0"
simple-html-tokenizer "^0.5.10"

"@glimmer/util@0.65.1":
version "0.65.1"
resolved "https://registry.yarnpkg.com/@glimmer/util/-/util-0.65.1.tgz#c85ebc13344739c123e0ec6551ae740cf5fd62bf"
Expand All @@ -604,6 +621,15 @@
"@glimmer/interfaces" "0.65.1"
"@simple-dom/interface" "^1.4.0"

"@glimmer/util@0.79.1":
version "0.79.1"
resolved "https://registry.yarnpkg.com/@glimmer/util/-/util-0.79.1.tgz#35b74f58712bbfcde973947449aac9b08fa38067"
integrity sha512-NNeTAh9NsqZLiG8to8RJzEDrUkf8h3eVBxVtRC63xOb4Pq5fQFv3B5E51ibv83yPvCyD+y/bMnInB1ep5gneYQ==
dependencies:
"@glimmer/env" "0.1.7"
"@glimmer/interfaces" "0.79.1"
"@simple-dom/interface" "^1.4.0"

"@glimmer/validator@0.65.1", "@glimmer/validator@^0.65.0":
version "0.65.1"
resolved "https://registry.yarnpkg.com/@glimmer/validator/-/validator-0.65.1.tgz#07ebd13952660d341fea8e5b606b512fe1dbc176"
Expand All @@ -617,6 +643,11 @@
resolved "https://registry.yarnpkg.com/@handlebars/parser/-/parser-1.1.0.tgz#d6dbc7574774b238114582410e8fee0dc3532bdf"
integrity sha512-rR7tJoSwJ2eooOpYGxGGW95sLq6GXUaS1UtWvN7pei6n2/okYvCGld9vsUTvkl2migxbkszsycwtMf/GEc1k1A==

"@handlebars/parser@~2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@handlebars/parser/-/parser-2.0.0.tgz#5e8b7298f31ff8f7b260e6b7363c7e9ceed7d9c5"
integrity sha512-EP9uEDZv/L5Qh9IWuMUGJRfwhXJ4h1dqKTT4/3+tY0eu7sPis7xh23j61SYUnNF4vqCQvvUXpDo9Bh/+q1zASA==

"@iarna/toml@2.2.5":
version "2.2.5"
resolved "https://registry.yarnpkg.com/@iarna/toml/-/toml-2.2.5.tgz#b32366c89b43c6f8cefbdefac778b9c828e3ba8c"
Expand Down Expand Up @@ -2777,6 +2808,11 @@ commander@^6.2.0:
resolved "https://registry.yarnpkg.com/commander/-/commander-6.2.0.tgz#b990bfb8ac030aedc6d11bc04d1488ffef56db75"
integrity sha512-zP4jEKbe8SHzKJYQmq8Y9gYjtO/POJLgIdKgV7B9qNmABVFVc+ctqSX6iXh4mCpJfRBOabiZ2YKPg8ciDw6C+Q==

commander@^6.2.1:
version "6.2.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-6.2.1.tgz#0792eb682dfbc325999bb2b84fddddba110ac73c"
integrity sha512-U7VdrJFnJgo4xjrHpTzu0yrHPGImdsmD95ZlgYSEajAn2JKzDhDTPG9kBTefmObL2w/ngeZnilk+OV9CG3d7UA==

commander@~2.20.3:
version "2.20.3"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
Expand Down Expand Up @@ -3427,6 +3463,23 @@ ember-template-recast@^5.0.1:
tmp "^0.2.1"
workerpool "^6.0.3"

ember-template-recast@^5.0.3:
version "5.0.3"
resolved "https://registry.yarnpkg.com/ember-template-recast/-/ember-template-recast-5.0.3.tgz#79df27a70bdce7be17f14db13886afde1e9d02d6"
integrity sha512-qsJYQhf29Dk6QMfviXhUPE+byMOs6iRQxUDHgkj8yqjeppvjHaFG96hZi/NAXJTm/M7o3PpfF5YlmeaKtI9UeQ==
dependencies:
"@glimmer/reference" "^0.65.0"
"@glimmer/syntax" "^0.65.0"
"@glimmer/validator" "^0.65.0"
async-promise-queue "^1.0.5"
colors "^1.4.0"
commander "^6.2.1"
globby "^11.0.3"
ora "^5.4.0"
slash "^3.0.0"
tmp "^0.2.1"
workerpool "^6.1.4"

emittery@^0.7.1:
version "0.7.2"
resolved "https://registry.yarnpkg.com/emittery/-/emittery-0.7.2.tgz#25595908e13af0f5674ab419396e2fb394cdfa82"
Expand Down Expand Up @@ -7327,7 +7380,7 @@ optionator@^0.9.1:
type-check "^0.4.0"
word-wrap "^1.2.3"

ora@5.4.0, ora@^5.1.0, ora@^5.3.0:
ora@5.4.0, ora@^5.1.0, ora@^5.3.0, ora@^5.4.0:
version "5.4.0"
resolved "https://registry.yarnpkg.com/ora/-/ora-5.4.0.tgz#42eda4855835b9cd14d33864c97a3c95a3f56bf4"
integrity sha512-1StwyXQGoU6gdjYkyVcqOLnVlbKj+6yPNNOxJVgpt9t4eksKjiriiHuxktLYkgllwk+D6MbC4ihH84L1udRXPg==
Expand Down Expand Up @@ -10069,6 +10122,11 @@ workerpool@^6.0.3:
resolved "https://registry.yarnpkg.com/workerpool/-/workerpool-6.0.3.tgz#3f80ba4d914fe7bb8d933628c26e5d7ff820c703"
integrity sha512-meU8ZzO+ipcx/njxtKUcbu2K95085q5WYDo8fR6PMW3hCY4driteIsNsEowYV7dzOtvq0HotUKsReJkK8gKXgg==

workerpool@^6.1.4:
version "6.1.4"
resolved "https://registry.yarnpkg.com/workerpool/-/workerpool-6.1.4.tgz#6a972b6df82e38d50248ee2820aa98e2d0ad3090"
integrity sha512-jGWPzsUqzkow8HoAvqaPWTUPCrlPJaJ5tY8Iz7n1uCz3tTp6s3CDG0FF1NsX42WNlkRSW6Mr+CDZGnNoSsKa7g==

wrap-ansi@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-5.1.0.tgz#1fd1f67235d5b6d0fee781056001bfb694c03b09"
Expand Down

0 comments on commit ac1d0f5

Please sign in to comment.