Skip to content

Commit

Permalink
move ignoreAll into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
mansona committed Nov 12, 2024
1 parent 60bf915 commit fd72bc2
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 94 deletions.
23 changes: 23 additions & 0 deletions lib/get-files.mjs
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/*'],
});
}
33 changes: 33 additions & 0 deletions lib/ignore-error.mjs
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}`);
}
}
44 changes: 44 additions & 0 deletions lib/ignore.mjs
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));
}
}
96 changes: 2 additions & 94 deletions main.mjs
Original file line number Diff line number Diff line change
@@ -1,100 +1,8 @@
import { readFileSync, writeFileSync } from 'fs';
import importCwd from 'import-cwd';
import walkSync from 'walk-sync';
import { join } from 'path';
import getFiles from './lib/get-files.mjs';

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

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/*'],
});
}

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}`);
}
}

// only passing the cwd in for testing purposes
export 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));
}
}
export { default as ignoreAll } from './lib/ignore.mjs';

export function list(directory) {
// this is only used for internal testing, lint-to-the-future never passes a
Expand Down

0 comments on commit fd72bc2

Please sign in to comment.