mirror of
https://github.com/sasjs/lint.git
synced 2026-01-18 17:50:05 +00:00
chore: Code Refactor
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
import { Diagnostic } from '../../types/Diagnostic'
|
import { Diagnostic, LintConfig, Macro, Severity } from '../../types'
|
||||||
import { LintConfig } from '../../types'
|
|
||||||
import { FileLintRule } from '../../types/LintRule'
|
import { FileLintRule } from '../../types/LintRule'
|
||||||
import { LintRuleType } from '../../types/LintRuleType'
|
import { LintRuleType } from '../../types/LintRuleType'
|
||||||
import { Severity } from '../../types/Severity'
|
|
||||||
import { parseMacros } from '../../utils/parseMacros'
|
import { parseMacros } from '../../utils/parseMacros'
|
||||||
|
|
||||||
const name = 'strictMacroDefinition'
|
const name = 'strictMacroDefinition'
|
||||||
@@ -24,12 +22,11 @@ const validOptions = [
|
|||||||
'STORE'
|
'STORE'
|
||||||
]
|
]
|
||||||
|
|
||||||
const test = (value: string, config?: LintConfig) => {
|
const processParams = (
|
||||||
const diagnostics: Diagnostic[] = []
|
content: string,
|
||||||
|
macro: Macro,
|
||||||
const macros = parseMacros(value, config)
|
diagnostics: Diagnostic[]
|
||||||
|
): string => {
|
||||||
macros.forEach((macro) => {
|
|
||||||
const declaration = macro.declaration
|
const declaration = macro.declaration
|
||||||
|
|
||||||
const regExpParams = new RegExp(/(?<=\().*(?=\))/)
|
const regExpParams = new RegExp(/(?<=\().*(?=\))/)
|
||||||
@@ -39,14 +36,13 @@ const test = (value: string, config?: LintConfig) => {
|
|||||||
if (regExpParamsResult) {
|
if (regExpParamsResult) {
|
||||||
const paramsPresent = regExpParamsResult[0]
|
const paramsPresent = regExpParamsResult[0]
|
||||||
|
|
||||||
const paramsTrimmed = paramsPresent.trim()
|
const params = paramsPresent.trim().split(',')
|
||||||
const params = paramsTrimmed.split(',')
|
|
||||||
params.forEach((param) => {
|
params.forEach((param) => {
|
||||||
const trimedParam = param.split('=')[0].trim()
|
const trimedParam = param.split('=')[0].trim()
|
||||||
|
|
||||||
let paramLineNumber: number = 1,
|
let paramLineNumber: number = 1,
|
||||||
paramStartIndex: number = 1,
|
paramStartIndex: number = 1,
|
||||||
paramEndIndex: number = value.length
|
paramEndIndex: number = content.length
|
||||||
|
|
||||||
if (
|
if (
|
||||||
macro.declarationLines.findIndex(
|
macro.declarationLines.findIndex(
|
||||||
@@ -64,8 +60,7 @@ const test = (value: string, config?: LintConfig) => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
if (declarationLineIndex !== -1) {
|
if (declarationLineIndex !== -1) {
|
||||||
const declarationLine =
|
const declarationLine = macro.declarationLines[declarationLineIndex]
|
||||||
macro.declarationLines[declarationLineIndex]
|
|
||||||
const partFound = regEx.exec(declarationLine)![0]
|
const partFound = regEx.exec(declarationLine)![0]
|
||||||
|
|
||||||
paramLineNumber = macro.startLineNumbers[declarationLineIndex]
|
paramLineNumber = macro.startLineNumbers[declarationLineIndex]
|
||||||
@@ -100,7 +95,14 @@ const test = (value: string, config?: LintConfig) => {
|
|||||||
|
|
||||||
_declaration = declaration.split(`(${paramsPresent})`)[1]
|
_declaration = declaration.split(`(${paramsPresent})`)[1]
|
||||||
}
|
}
|
||||||
|
return _declaration
|
||||||
|
}
|
||||||
|
|
||||||
|
const processOptions = (
|
||||||
|
_declaration: string,
|
||||||
|
macro: Macro,
|
||||||
|
diagnostics: Diagnostic[]
|
||||||
|
): void => {
|
||||||
let optionsPresent = _declaration.split('/')?.[1]?.trim()
|
let optionsPresent = _declaration.split('/')?.[1]?.trim()
|
||||||
|
|
||||||
if (optionsPresent) {
|
if (optionsPresent) {
|
||||||
@@ -108,6 +110,7 @@ const test = (value: string, config?: LintConfig) => {
|
|||||||
|
|
||||||
let result = regex.exec(optionsPresent)
|
let result = regex.exec(optionsPresent)
|
||||||
|
|
||||||
|
// removing Option's `="..."` part, e.g. des="..."
|
||||||
while (result) {
|
while (result) {
|
||||||
optionsPresent =
|
optionsPresent =
|
||||||
optionsPresent.slice(0, result.index) +
|
optionsPresent.slice(0, result.index) +
|
||||||
@@ -138,6 +141,17 @@ const test = (value: string, config?: LintConfig) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const test = (value: string, config?: LintConfig) => {
|
||||||
|
const diagnostics: Diagnostic[] = []
|
||||||
|
|
||||||
|
const macros = parseMacros(value, config)
|
||||||
|
|
||||||
|
macros.forEach((macro) => {
|
||||||
|
const _declaration = processParams(value, macro, diagnostics)
|
||||||
|
|
||||||
|
processOptions(_declaration, macro, diagnostics)
|
||||||
})
|
})
|
||||||
|
|
||||||
return diagnostics
|
return diagnostics
|
||||||
|
|||||||
12
src/types/Macro.ts
Normal file
12
src/types/Macro.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
export interface Macro {
|
||||||
|
name: string
|
||||||
|
startLineNumbers: number[]
|
||||||
|
endLineNumber: number | null
|
||||||
|
declarationLines: string[]
|
||||||
|
terminationLine: string
|
||||||
|
declaration: string
|
||||||
|
termination: string
|
||||||
|
parentMacro: string
|
||||||
|
hasMacroNameInMend: boolean
|
||||||
|
mismatchedMendMacroName: string
|
||||||
|
}
|
||||||
@@ -4,3 +4,4 @@ export * from './LintConfig'
|
|||||||
export * from './LintRule'
|
export * from './LintRule'
|
||||||
export * from './LintRuleType'
|
export * from './LintRuleType'
|
||||||
export * from './Severity'
|
export * from './Severity'
|
||||||
|
export * from './Macro'
|
||||||
|
|||||||
@@ -1,20 +1,7 @@
|
|||||||
import { LintConfig } from '../types/LintConfig'
|
import { LintConfig, Macro } from '../types'
|
||||||
import { LineEndings } from '../types/LineEndings'
|
import { LineEndings } from '../types/LineEndings'
|
||||||
import { trimComments } from './trimComments'
|
import { trimComments } from './trimComments'
|
||||||
|
|
||||||
interface Macro {
|
|
||||||
name: string
|
|
||||||
startLineNumbers: number[]
|
|
||||||
endLineNumber: number | null
|
|
||||||
declarationLines: string[]
|
|
||||||
terminationLine: string
|
|
||||||
declaration: string
|
|
||||||
termination: string
|
|
||||||
parentMacro: string
|
|
||||||
hasMacroNameInMend: boolean
|
|
||||||
mismatchedMendMacroName: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export const parseMacros = (text: string, config?: LintConfig): Macro[] => {
|
export const parseMacros = (text: string, config?: LintConfig): Macro[] => {
|
||||||
const lineEnding = config?.lineEndings === LineEndings.CRLF ? '\r\n' : '\n'
|
const lineEnding = config?.lineEndings === LineEndings.CRLF ? '\r\n' : '\n'
|
||||||
const lines: string[] = text ? text.split(lineEnding) : []
|
const lines: string[] = text ? text.split(lineEnding) : []
|
||||||
@@ -39,6 +26,8 @@ export const parseMacros = (text: string, config?: LintConfig): Macro[] => {
|
|||||||
const statements: string[] = trimmedLine.split(';')
|
const statements: string[] = trimmedLine.split(';')
|
||||||
|
|
||||||
if (isReadingMacroDefinition) {
|
if (isReadingMacroDefinition) {
|
||||||
|
// checking if code is split into statements based on `;` is a part of HTML Encoded Character
|
||||||
|
// if it happened, merges two statements into one
|
||||||
statements.forEach((statement, statementIndex) => {
|
statements.forEach((statement, statementIndex) => {
|
||||||
if (/&[^\s]{1,5}$/.test(statement)) {
|
if (/&[^\s]{1,5}$/.test(statement)) {
|
||||||
const next = statements[statementIndex]
|
const next = statements[statementIndex]
|
||||||
|
|||||||
Reference in New Issue
Block a user