Skip to content

Commit

Permalink
dev: eslint-plugin check comments, strings, and identifiers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Mar 8, 2022
1 parent 0fcf365 commit f5a6d18
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ export const LITERAL_COOKED = 'cafe\u0301';
export const UNDEFINED = undefined;
export const NULL = null;
export const BIG_INT = 1n;
export const BADD_NAME = true;

function mapDir(dir) {
return `type: ${dir.isFile ? 'F' : ' '}${dir.isDirectory() ? 'D' :' '} name: ${dir.name}`;
}

/**
* this is a functionn comment.
*/
export async function listFiles() {
const dirs = await fs.readdir('.', { withFileTypes: true });
const entries = dirs.map(mapDir);
Expand Down
11 changes: 9 additions & 2 deletions packages/cspell-eslint-plugin/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ ruleTester.run('cspell', rule.rules.cspell, {
readSample('sampleESM.mjs'),
// readSample('sample.json'),
],
// cspell:ignore Guuide Gallaxy
invalid: [readInvalid('with-errors/sampleESM.mjs', ['Unknown word: "Guuide"', 'Unknown word: "Gallaxy"'])],
// cspell:ignore Guuide Gallaxy BADD functionn
invalid: [
readInvalid('with-errors/sampleESM.mjs', [
'Unknown word: "Guuide"',
'Unknown word: "Gallaxy"',
'Unknown word: "BADD"',
'Unknown word: "functionn"',
]),
],
});

function resolveFromMonoRepo(file: string): string {
Expand Down
21 changes: 12 additions & 9 deletions packages/cspell-eslint-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,40 @@ scope: ${context.getScope().type}
`);

function checkLiteral(node: Literal & Rule.NodeParentExtension) {
debugNode(node, node.value);
if (typeof node.value === 'string') {
checkNodeText(node, node.value);
}
debugNode(node, node.value);
}

function checkTemplateElement(node: TemplateElement & Rule.NodeParentExtension) {
debugNode(node, node.value);
checkNodeText(node, node.value.cooked || node.value.raw);
}

function checkIdentifier(node: Identifier & Rule.NodeParentExtension) {
debugNode(node, node.name);
checkNodeText(node, node.name);
}

function checkComment(node: Comment) {
checkNodeText(node, node.value);
const val = format('%o', node);
log(`Comment: ${val}`);
}

function checkNodeText(node: Node, text: string) {
if (!node.range || !node.loc) return;
function checkNodeText(node: Node | Comment, text: string) {
if (!node.range) return;

const scope = inheritance(node);
const result = validator.checkText(node.range, text, scope);
if (result.length) {
console.error('%o', result);
}
result.forEach((issue) => reportIssue(node, issue));
result.forEach((issue) => reportIssue(issue));
}

function reportIssue(node: Node, issue: ValidationIssue) {
function reportIssue(issue: ValidationIssue) {
// const messageId = issue.isFlagged ? 'cspell-forbidden-word' : 'cspell-unknown-word';
const messageType = issue.isFlagged ? 'Forbidden' : 'Unknown';
const message = `${messageType} word: "${issue.text}"`;
Expand Down Expand Up @@ -97,7 +100,7 @@ scope: ${context.getScope().type}
Identifier: checkIdentifier,
};

function mapNode(node: Node | TSESTree.Node, index: number, nodes: Node[]): string {
function mapNode(node: Node | TSESTree.Node | Comment, index: number, nodes: (Node | Comment)[]): string {
const child = nodes[index + 1];
if (node.type === 'ImportSpecifier') {
const extra = node.imported === child ? '.imported' : node.local === child ? '.local' : '';
Expand Down Expand Up @@ -133,16 +136,16 @@ scope: ${context.getScope().type}
return node.type;
}

function inheritance(node: Node) {
function inheritance(node: Node | Comment) {
const a = [...context.getAncestors(), node];
return a.map(mapNode);
}

function inheritanceSummary(node: Node) {
function inheritanceSummary(node: Node | Comment) {
return inheritance(node).join(' ');
}

function debugNode(node: Node, value: unknown) {
function debugNode(node: Node | Comment, value: unknown) {
if (!isDebugMode) return;
const val = format('%o', value);
log(`${inheritanceSummary(node)}: ${val}`);
Expand Down

0 comments on commit f5a6d18

Please sign in to comment.