1
0
mirror of https://github.com/sasjs/lint.git synced 2026-01-07 12:40:05 +00:00

chore(*): add PR template, add TSDoc comments

This commit is contained in:
Krishna Acondy
2021-03-22 20:36:19 +00:00
parent bf23963127
commit da528bb031
12 changed files with 77 additions and 1 deletions

20
PULL_REQUEST_TEMPLATE.md Normal file
View File

@@ -0,0 +1,20 @@
## Issue
Link any related issue(s) in this section.
## Intent
What this PR intends to achieve.
## Implementation
What code changes have been made to achieve the intent.
## Checks
- [ ] Code is formatted correctly (`npm run lint:fix`).
- [ ] Any new functionality has been unit tested.
- [ ] All unit tests are passing (`npm test`).
- [ ] All CI checks are green.
- [ ] JSDoc comments have been added or updated.
- [ ] Reviewer is assigned.

View File

@@ -1,5 +1,9 @@
import { lint } from './lint' import { lint } from './lint'
/**
* Example which tests a piece of text with all known violations.
*/
const text = `/** const text = `/**
@file @file
@brief Returns an unused libref @brief Returns an unused libref

View File

@@ -2,11 +2,21 @@ import { Diagnostic } from './types/Diagnostic'
import { LintConfig } from './types/LintConfig' import { LintConfig } from './types/LintConfig'
import { getLintConfig } from './utils/getLintConfig' import { getLintConfig } from './utils/getLintConfig'
/**
* Analyses and produces a set of diagnostics for the given text content.
* @param {string} text - the text content to be linted.
* @returns {Diagnostic[]} array of diagnostic objects, each containing a warning, line number and column number.
*/
export const lint = async (text: string) => { export const lint = async (text: string) => {
const config = await getLintConfig() const config = await getLintConfig()
return processText(text, config) return processText(text, config)
} }
/**
* Splits the given content into a list of lines, regardless of CRLF or LF line endings.
* @param {string} text - the text content to be split into lines.
* @returns {string[]} an array of lines from the given text
*/
export const splitText = (text: string): string[] => { export const splitText = (text: string): string[] => {
if (!text) return [] if (!text) return []
return text.replace(/\r\n/g, '\n').split('\n') return text.replace(/\r\n/g, '\n').split('\n')

View File

@@ -15,6 +15,9 @@ const test = (value: string) => {
} }
} }
/**
* Lint rule that checks for the presence of a Doxygen header in a given file.
*/
export const hasDoxygenHeader: FileLintRule = { export const hasDoxygenHeader: FileLintRule = {
type: LintRuleType.File, type: LintRuleType.File,
name, name,

View File

@@ -15,6 +15,9 @@ const test = (value: string, lineNumber: number) => {
})) }))
} }
/**
* Lint rule that checks for the presence of encoded password(s) in a given line of text.
*/
export const noEncodedPasswords: LineLintRule = { export const noEncodedPasswords: LineLintRule = {
type: LintRuleType.Line, type: LintRuleType.Line,
name, name,

View File

@@ -9,6 +9,9 @@ const test = (value: string, lineNumber: number) =>
? [] ? []
: [{ warning, lineNumber, columnNumber: value.trimEnd().length + 1 }] : [{ warning, lineNumber, columnNumber: value.trimEnd().length + 1 }]
/**
* Lint rule that checks for the presence of trailing space(s) in a given line of text.
*/
export const noTrailingSpaces: LineLintRule = { export const noTrailingSpaces: LineLintRule = {
type: LintRuleType.Line, type: LintRuleType.Line,
name, name,

View File

@@ -1,3 +1,6 @@
/**
* A diagnostic is produced by the execution of a lint rule against a file or line of text.
*/
export interface Diagnostic { export interface Diagnostic {
lineNumber: number lineNumber: number
columnNumber: number columnNumber: number

View File

@@ -3,6 +3,13 @@ import { noEncodedPasswords } from '../rules/noEncodedPasswords'
import { noTrailingSpaces } from '../rules/noTrailingSpaces' import { noTrailingSpaces } from '../rules/noTrailingSpaces'
import { FileLintRule, LineLintRule } from './LintRule' import { FileLintRule, LineLintRule } from './LintRule'
/**
* LintConfig is the logical representation of the .sasjslint file.
* It exposes two sets of rules - one to be run against each line in a file,
* and one to be run once per file.
*
* More types of rules, when available, will be added here.
*/
export class LintConfig { export class LintConfig {
readonly lineLintRules: LineLintRule[] = [] readonly lineLintRules: LineLintRule[] = []
readonly fileLintRules: FileLintRule[] = [] readonly fileLintRules: FileLintRule[] = []

View File

@@ -1,6 +1,10 @@
import { Diagnostic } from './Diagnostic' import { Diagnostic } from './Diagnostic'
import { LintRuleType } from './LintRuleType' import { LintRuleType } from './LintRuleType'
/**
* A lint rule is defined by a type, name, description, warning text and a test function.
* The test function produces a set of diagnostics when executed.
*/
export interface LintRule { export interface LintRule {
type: LintRuleType type: LintRuleType
name: string name: string
@@ -9,10 +13,16 @@ export interface LintRule {
test: (value: string, lineNumber: number) => Diagnostic[] test: (value: string, lineNumber: number) => Diagnostic[]
} }
/**
* A LineLintRule is run once per line of text.
*/
export interface LineLintRule extends LintRule { export interface LineLintRule extends LintRule {
type: LintRuleType.Line type: LintRuleType.Line
} }
/**
* A FileLintRule is run once per file.
*/
export interface FileLintRule extends LintRule { export interface FileLintRule extends LintRule {
type: LintRuleType.File type: LintRuleType.File
test: (value: string) => Diagnostic[] test: (value: string) => Diagnostic[]

View File

@@ -1,3 +1,7 @@
/**
* The types of lint rules available.
* This might be expanded to include lint rules for entire folders and projects.
*/
export enum LintRuleType { export enum LintRuleType {
Line, Line,
File File

View File

@@ -3,6 +3,10 @@ import { LintConfig } from '../types/LintConfig'
import { readFile } from '@sasjs/utils/file' import { readFile } from '@sasjs/utils/file'
import { getProjectRoot } from './getProjectRoot' import { getProjectRoot } from './getProjectRoot'
/**
* Fetches the config from the .sasjslint file and creates a LintConfig object.
* @returns {Promise<LintConfig>} resolves with an object representing the current lint configuration.
*/
export async function getLintConfig(): Promise<LintConfig> { export async function getLintConfig(): Promise<LintConfig> {
const projectRoot = await getProjectRoot() const projectRoot = await getProjectRoot()
const configuration = await readFile(path.join(projectRoot, '.sasjslint')) const configuration = await readFile(path.join(projectRoot, '.sasjslint'))

View File

@@ -1,7 +1,12 @@
import path from 'path' import path from 'path'
import { fileExists } from '@sasjs/utils/file' import { fileExists } from '@sasjs/utils/file'
export async function getProjectRoot() { /**
* Returns the absolute path to the location of the .sasjslint file.
* Traverses the folder tree until the .sasjslint file is found.
* @returns {Promise<string>} the path to the folder containing the lint config.
*/
export async function getProjectRoot(): Promise<string> {
let root = '' let root = ''
let rootFound = false let rootFound = false
let i = 1 let i = 1