Skip to content

Commit

Permalink
Merge 5f0e27e into b7a88ec
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Mar 9, 2022
2 parents b7a88ec + 5f0e27e commit fad5d9f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/cspell-eslint-plugin/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"skipFiles": [
"<node_internals>/**"
],
"stopOnEntry": true,
"stopOnEntry": false,
"type": "pwa-node"
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* This is a sample with spelling errors in template strings.
*/

const now = new Date();

export const messageToday = `
Todayy is ${now.toLocaleDateString(undefined, { weekday: 'long' })}.
Day ${now.toLocaleDateString(undefined, { day: 'numeric' })} of the Montj of ${now.toLocaleDateString(undefined, { month: 'long' })}
in the Yaar ${now.toLocaleDateString(undefined, { year: 'numeric' })}.
`

export const months = 'January\u00eb, Februarry, March, Aprill, May, June';

export const templateWithUnicodeEscapeSequences = `
Lets gooo to a caf\u00e9 or a cafe\u0301? ${now.getDay() === 0 ? 'Yes' : 'No'}
Only if Today is ${now.toLocaleDateString(undefined, { weekday: 'long' })} it is the start of the weeek.
`;
1 change: 0 additions & 1 deletion packages/cspell-eslint-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "@cspell/eslint-plugin-cspell",
"private": true,
"publishConfig": {
"access": "public"
},
Expand Down
25 changes: 12 additions & 13 deletions packages/cspell-eslint-plugin/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,25 @@ const ruleTester = new RuleTester({
});

ruleTester.run('cspell', rule.rules.cspell, {
valid: [
// "import * as jest from 'jest'",
// "import { jestFn as jestFunc} from 'jest'",
// 'const mocha = require("mocha");',
// 'async function* values(iter) { yield* iter; }',
// 'var foo = true',
// 'const x = `It is now time to add everything up: \\` ${y} + ${x}`',
readSample('sample.js'),
readSample('sample.ts'),
readSample('sampleESM.mjs'),
// readSample('sample.json'),
],
// cspell:ignore Guuide Gallaxy BADD functionn
valid: [readSample('sample.js'), readSample('sample.ts'), readSample('sampleESM.mjs')],
invalid: [
// cspell:ignore Guuide Gallaxy BADD functionn
readInvalid('with-errors/sampleESM.mjs', [
'Unknown word: "Guuide"',
'Unknown word: "Gallaxy"',
'Unknown word: "BADD"',
'Unknown word: "functionn"',
]),
// cspell:ignore Montj Todayy Yaar Aprill Februarry gooo weeek
readInvalid('with-errors/sampleTemplateString.mjs', [
{ message: 'Unknown word: "Todayy"' },
'Unknown word: "Montj"',
'Unknown word: "Yaar"',
'Unknown word: "Februarry"',
'Unknown word: "Aprill"',
'Unknown word: "gooo"',
'Unknown word: "weeek"',
]),
],
});

Expand Down
37 changes: 27 additions & 10 deletions packages/cspell-eslint-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Rule } from 'eslint';
// eslint-disable-next-line node/no-missing-import
import type { Comment, Identifier, Literal, Node, TemplateElement } from 'estree';
import { format } from 'util';
import { createTextDocument, DocumentValidator, ValidationIssue } from 'cspell-lib';
import { createTextDocument, DocumentValidator, ValidationIssue, CSpellSettings } from 'cspell-lib';

interface PluginRules {
['cspell']: Rule.RuleModule;
Expand All @@ -17,13 +17,26 @@ const meta: Rule.RuleMetaData = {
},
};

type ASTNode = Node | Comment;

const defaultSettings: CSpellSettings = {
patterns: [
// @todo: be able to use cooked / transformed strings.
// {
// // Do not block unicode escape sequences.
// name: 'js-unicode-escape',
// pattern: /$^/g,
// },
],
};

const isDebugMode = false;

const log: typeof console.log = isDebugMode ? console.log : () => undefined;

function create(context: Rule.RuleContext): Rule.RuleListener {
const doc = createTextDocument({ uri: context.getFilename(), content: context.getSourceCode().getText() });
const validator = new DocumentValidator(doc, {}, {});
const validator = new DocumentValidator(doc, {}, defaultSettings);
validator.prepareSync();

log('Source code: \n ************************ \n\n');
Expand All @@ -46,6 +59,7 @@ scope: ${context.getScope().type}

function checkTemplateElement(node: TemplateElement & Rule.NodeParentExtension) {
debugNode(node, node.value);
// console.log('Template: %o', node.value);
checkNodeText(node, node.value.cooked || node.value.raw);
}

Expand All @@ -60,13 +74,16 @@ scope: ${context.getScope().type}
log(`Comment: ${val}`);
}

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

const adj = node.type === 'Literal' ? 1 : 0;
const range = [node.range[0] + adj, node.range[1] - adj] as const;

const scope = inheritance(node);
const result = validator.checkText(node.range, text, scope);
const result = validator.checkText(range, text, scope);
if (result.length) {
console.error('%o', result);
log('%o', result);
}
result.forEach((issue) => reportIssue(issue));
}
Expand Down Expand Up @@ -100,7 +117,7 @@ scope: ${context.getScope().type}
Identifier: checkIdentifier,
};

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

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

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

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

function tagLiteral(node: Node | TSESTree.Node): string {
function tagLiteral(node: ASTNode | TSESTree.Node): string {
assert(node.type === 'Literal');
const kind = typeof node.value;
const extra =
Expand Down

0 comments on commit fad5d9f

Please sign in to comment.