Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ignoreAll function into its own file #27

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/get-files.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import walkSync from "walk-sync";
import { readFileSync } from 'fs';
import { join } from 'path';

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 {
// 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));
}
}
97 changes: 3 additions & 94 deletions main.mjs
Original file line number Diff line number Diff line change
@@ -1,99 +1,8 @@
import { readFileSync, writeFileSync } from 'fs';
import walkSync from 'walk-sync';
import { readFileSync } from 'fs';
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 {
// 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