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

feat: Support suggestions #1812

Merged
merged 2 commits into from
Jul 1, 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ jobs:
# Default: warning
inline: warning

# Generate Spelling suggestions.
suggestions: false

# Determines if the action should be failed if any spelling issues are found.
# Allowed values are: true, false
# Default: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import:
- "../../cspell.json"
files:
- "*.ts"
# cspell:disable
flagWords:
- blacklist->denylist
- blacklists->denylists
- whitelist->allowlist
- whitelists->allowlists
suggestWords:
- colour->color
- colours->colors
# cspell:enable
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
export function myFuncton(countt: number): number {
return 43;
}

// We need to blacklist the following functions and colours.
8 changes: 8 additions & 0 deletions action-src/src/ActionParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export interface ActionParams {
* @default 'false'
*/
strict: TrueFalse;
/**
* Should generate spelling suggestions.
* Note: Preferred suggestions will always be shown if available.
* @default 'false'
*/
suggestions: TrueFalse;
/**
* Increases the amount of information logged during the action.
* true - show progress
Expand Down Expand Up @@ -63,6 +69,7 @@ const defaultActionParams: ActionParams = {
verbose: 'false',
check_dot_files: 'explicit',
use_cspell_files: 'false',
suggestions: 'false',
};

type ValidationFunction = (params: ActionParamsInput) => string | undefined;
Expand Down Expand Up @@ -118,6 +125,7 @@ export function validateActionParams(
validateTrueFalse('incremental_files_only'),
validateTrueFalse('verbose'),
validateTrueFalse('use_cspell_files'),
validateTrueFalse('suggestions'),
validateOptions('check_dot_files', ['true', 'false', 'explicit']),
];
const success = validations
Expand Down
426 changes: 321 additions & 105 deletions action-src/src/__snapshots__/action.test.ts.snap

Large diffs are not rendered by default.

22 changes: 12 additions & 10 deletions action-src/src/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,18 @@ describe('Validate Action', () => {
});

test.each`
files | incremental | dot | contextFile | expected
${'fixtures/sampleCode/**'} | ${false} | ${'explicit'} | ${'pull_request_with_files.json'} | ${true}
${'fixtures/sampleCode/**'} | ${true} | ${'explicit'} | ${'bad_params/bad_unsupported_event.json'} | ${true}
${''} | ${true} | ${'explicit'} | ${'bad_params/bad_unsupported_event.json'} | ${false}
${'**'} | ${true} | ${'explicit'} | ${'bad_params/bad_unsupported_event.json'} | ${false}
${''} | ${false} | ${'explicit'} | ${'pull_request_with_files.json'} | ${false}
${''} | ${false} | ${''} | ${'pull_request_with_files.json'} | ${false}
${''} | ${false} | ${'true'} | ${'pull_request_with_files.json'} | ${false}
files | incremental | suggestions | dot | contextFile | expected
${'sampleCode/**'} | ${false} | ${false} | ${'explicit'} | ${'pull_request_with_files.json'} | ${false}
${'sampleCode/**'} | ${false} | ${true} | ${'explicit'} | ${'pull_request_with_files.json'} | ${false}
${'sampleCode/**'} | ${true} | ${false} | ${'explicit'} | ${'bad_params/bad_unsupported_event.json'} | ${false}
${''} | ${true} | ${false} | ${'explicit'} | ${'bad_params/bad_unsupported_event.json'} | ${false}
${'**'} | ${true} | ${false} | ${'explicit'} | ${'bad_params/bad_unsupported_event.json'} | ${false}
${''} | ${false} | ${false} | ${'explicit'} | ${'pull_request_with_files.json'} | ${false}
${''} | ${false} | ${false} | ${''} | ${'pull_request_with_files.json'} | ${false}
${''} | ${false} | ${false} | ${'true'} | ${'pull_request_with_files.json'} | ${false}
`(
'check files "$files" incremental: $incremental $contextFile, dot: "$dot"',
async ({ files, incremental, contextFile, dot, expected }) => {
'check files "$files" incremental: $incremental sugs: $suggestions $contextFile, dot: "$dot"',
async ({ files, incremental, suggestions, contextFile, dot, expected }) => {
const warnings: string[] = [];
spyWarn.mockImplementation((msg: string) => warnings.push(msg));
const params = {
Expand All @@ -100,6 +101,7 @@ describe('Validate Action', () => {
INPUT_CHECK_DOT_FILES: dot,
INPUT_ROOT: path.resolve(sourceDir, 'fixtures'),
INPUT_CONFIG: path.resolve(sourceDir, 'fixtures/cspell.json'),
INPUT_SUGGESTIONS: suggestions ? 'true' : 'false',
};
const context = createContextFromFile(contextFile, params);
await expect(action(context)).resolves.toBe(expected);
Expand Down
1 change: 1 addition & 0 deletions action-src/src/checkSpelling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ async function checkSpelling(
config: params.config || undefined,
checkDotFiles: checkDotMap[params.check_dot_files],
files,
showSuggestions: params.suggestions === 'true',
};

const reporterOptions = {
Expand Down
1 change: 1 addition & 0 deletions action-src/src/getActionParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function getActionParams(): ActionParamsInput {
verbose: tf(getInput('verbose')),
check_dot_files: tf(getInput('check_dot_files')),
use_cspell_files: tf(getInput('use_cspell_files')),
suggestions: tf(getInput('suggestions')),
});
}

Expand Down
10 changes: 8 additions & 2 deletions action-src/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ ${error.stack}
const cwd = process.cwd();

this.issues.forEach((item) => {
const hasPreferred = item.suggestionsEx?.some((s) => s.isPreferred) || false;
const msgPrefix = item.isFlagged ? 'Forbidden word' : hasPreferred ? 'Misspelled word' : 'Unknown word';
const suggestions = item.suggestionsEx?.map((s) => s.word + (s.isPreferred ? '*' : '')).join(', ') || '';
const sugMsg = suggestions ? ` Suggestions: (${suggestions})` : '';
const message = `${msgPrefix} (${item.text})${sugMsg}`;

// format: ::warning file={name},line={line},col={col}::{message}
issueCommand(
command,
Expand All @@ -122,9 +128,9 @@ ${error.stack}
line: item.row,
col: item.col,
},
`Unknown word (${item.text})`,
message,
);
console.warn(`${relative(cwd, item.uri || '')}:${item.row}:${item.col} Unknown word (${item.text})`);
console.warn(`${relative(cwd, item.uri || '')}:${item.row}:${item.col} ${message}`);
});
}

Expand Down
5 changes: 4 additions & 1 deletion action-src/src/spell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface LintOptions {
*/
checkDotFiles: boolean | undefined;
files?: string[] | undefined;
showSuggestions?: boolean;
}

/**
Expand All @@ -21,7 +22,7 @@ export interface LintOptions {
* @param reporter - reporter to use.
*/
export async function lint(globs: string[], lintOptions: LintOptions, reporter: CSpellReporter): Promise<void> {
const { root, config, checkDotFiles, files } = lintOptions;
const { root, config, checkDotFiles, files, showSuggestions } = lintOptions;
// It is expected that `files` in the configuration will be used to filter the files.
const mustFindFiles = !files;
const options: CSpellApplicationOptions = {
Expand All @@ -30,7 +31,9 @@ export async function lint(globs: string[], lintOptions: LintOptions, reporter:
files,
// filterFiles: files ? false : undefined,
mustFindFiles,
showSuggestions,
};

if (checkDotFiles) {
options.dot = true;
} else if (checkDotFiles === false) {
Expand Down
5 changes: 5 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ inputs:
Allowed values are: true, false
default: "true"
required: false
suggestions:
description: >
Generate Spelling suggestions.
default: "false"
required: false
verbose:
description: |
Increase the log output to include progress.
Expand Down
24 changes: 17 additions & 7 deletions action/lib/main_root.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -41202,7 +41202,8 @@ var defaultActionParams = {
strict: "true",
verbose: "false",
check_dot_files: "explicit",
use_cspell_files: "false"
use_cspell_files: "false",
suggestions: "false"
};
function applyDefaults(params) {
const results = { ...defaultActionParams, ...params };
Expand Down Expand Up @@ -41241,6 +41242,7 @@ function validateActionParams(params, logError2) {
validateTrueFalse("incremental_files_only"),
validateTrueFalse("verbose"),
validateTrueFalse("use_cspell_files"),
validateTrueFalse("suggestions"),
validateOptions("check_dot_files", ["true", "false", "explicit"])
];
const success = validations.map((fn) => fn(params)).map((msg) => !msg || (logError2(msg), false)).reduce((a, b) => a && b, true);
Expand Down Expand Up @@ -41737,16 +41739,21 @@ ${error4.stack}
}
const cwd = process.cwd();
this.issues.forEach((item) => {
const hasPreferred = item.suggestionsEx?.some((s) => s.isPreferred) || false;
const msgPrefix = item.isFlagged ? "Forbidden word" : hasPreferred ? "Misspelled word" : "Unknown word";
const suggestions2 = item.suggestionsEx?.map((s) => s.word + (s.isPreferred ? "*" : "")).join(", ") || "";
const sugMsg = suggestions2 ? ` Suggestions: (${suggestions2})` : "";
const message = `${msgPrefix} (${item.text})${sugMsg}`;
(0, import_command.issueCommand)(
command,
{
file: relative2(cwd, item.uri || ""),
line: item.row,
col: item.col
},
`Unknown word (${item.text})`
message
);
console.warn(`${relative2(cwd, item.uri || "")}:${item.row}:${item.col} Unknown word (${item.text})`);
console.warn(`${relative2(cwd, item.uri || "")}:${item.row}:${item.col} ${message}`);
});
}
reporter = {
Expand Down Expand Up @@ -64306,14 +64313,15 @@ __reExport(esm_exports2, dist_exports);

// src/spell.ts
async function lint2(globs, lintOptions, reporter) {
const { root, config, checkDotFiles, files } = lintOptions;
const { root, config, checkDotFiles, files, showSuggestions } = lintOptions;
const mustFindFiles = !files;
const options = {
root,
config,
files,
// filterFiles: files ? false : undefined,
mustFindFiles
mustFindFiles,
showSuggestions
};
if (checkDotFiles) {
options.dot = true;
Expand Down Expand Up @@ -64370,7 +64378,8 @@ async function checkSpelling(params, globs, files) {
root: params.root || process.cwd(),
config: params.config || void 0,
checkDotFiles: checkDotMap[params.check_dot_files],
files
files,
showSuggestions: params.suggestions === "true"
};
const reporterOptions = {
verbose: params.verbose === "true"
Expand All @@ -64393,7 +64402,8 @@ function getActionParams() {
strict: tf((0, import_core3.getInput)("strict")),
verbose: tf((0, import_core3.getInput)("verbose")),
check_dot_files: tf((0, import_core3.getInput)("check_dot_files")),
use_cspell_files: tf((0, import_core3.getInput)("use_cspell_files"))
use_cspell_files: tf((0, import_core3.getInput)("use_cspell_files")),
suggestions: tf((0, import_core3.getInput)("suggestions"))
});
}
function tf(v) {
Expand Down