-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
102 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import walkSync from "walk-sync"; | ||
|
||
export default function getFiles(cwd) { | ||
let ignoreFile; | ||
|
||
try { | ||
ignoreFile = readFileSync(join(cwd, '.gitignore'), 'utf8') | ||
.split('\n') | ||
.filter((line) => line.length) | ||
.filter((line) => !line.startsWith('#')) | ||
// walkSync can't handle these | ||
.filter((line) => !line.startsWith('!')) | ||
.map((line) => line.replace(/^\//, '')) | ||
.map((line) => line.replace(/\/$/, '/*')); | ||
} catch (e) { | ||
// noop | ||
} | ||
|
||
return walkSync(cwd, { | ||
globs: ['**/*.hbs'], | ||
ignore: ignoreFile || ['**/node_modules/*'], | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { writeFileSync } from 'fs'; | ||
|
||
export default function ignoreError(errorInput, file, filePath) { | ||
let errors = errorInput.results ?? errorInput; | ||
|
||
const ruleIds = errors | ||
.filter(error => error.severity === 2) | ||
.map(error => error.rule); | ||
|
||
let uniqueIds = [...new Set(ruleIds)]; | ||
|
||
if (!uniqueIds.length) { | ||
// no errors to ignore | ||
return; | ||
} | ||
|
||
const firstLine = file.split('\n')[0]; | ||
|
||
if (firstLine.includes('template-lint-disable')) { | ||
const matched = firstLine.match(/template-lint-disable(.*)(--)?\}\}/); | ||
const existing = matched[1].split(' ') | ||
.map(item => item.trim()) | ||
.filter(item => item.length); | ||
|
||
uniqueIds = [...new Set([...ruleIds, ...existing])]; | ||
uniqueIds.sort((a, b) => a.localeCompare(b)); | ||
|
||
writeFileSync(filePath, file.replace(/^.*\n/, `{{! template-lint-disable ${uniqueIds.join(' ')} }}\n`)); | ||
} else { | ||
uniqueIds.sort((a, b) => a.localeCompare(b)); | ||
writeFileSync(filePath, `{{! template-lint-disable ${uniqueIds.join(' ')} }}\n${file}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { createRequire } from 'module'; | ||
import { readFileSync } from "fs"; | ||
import { join } from "path"; | ||
|
||
import getFiles from "./get-files.mjs"; | ||
import ignoreError from './ignore-error.mjs'; | ||
|
||
const require = createRequire(import.meta.url); | ||
|
||
// only passing the cwd in for testing purposes | ||
export default async function ignoreAll(cwd = process.cwd()) { | ||
const files = getFiles(cwd); | ||
|
||
let TemplateLinter; | ||
|
||
try { | ||
TemplateLinter = await import(require.resolve('ember-template-lint', { paths: [cwd]})); | ||
} catch (err) { | ||
console.error({err}); | ||
} | ||
|
||
const linter = new TemplateLinter.default(); | ||
|
||
for (const fileName of files) { | ||
const template = readFileSync(join(cwd, fileName), { | ||
encoding: 'utf8', | ||
}); | ||
|
||
let results = linter.verify({ | ||
source: template, | ||
filePath: fileName, | ||
// workaround for /~https://github.com/ember-template-lint/ember-template-lint/issues/2128 | ||
moduleId: fileName.replace(/\.[^/\\.]+$/, '') | ||
}); | ||
|
||
|
||
// support ember-template-lint 2.x and 3.x | ||
if (results.then) { | ||
results = await results; | ||
} | ||
|
||
ignoreError(results, template, join(cwd, fileName)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters