Skip to content

Commit 9a3a6f5

Browse files
committed
Some syntactical changes to make things look nicer overall with seperating to more functions and declaring types
1 parent f5af65a commit 9a3a6f5

File tree

2 files changed

+65
-42
lines changed

2 files changed

+65
-42
lines changed

server/src/linter.ts

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ type IncludeObj = {
3535
match: RegExpMatchArray
3636
}
3737

38+
type ErrorMatch = {
39+
type: DiagnosticSeverity,
40+
file: string,
41+
line: number,
42+
msg: string,
43+
}
44+
45+
type LinesProcessingInfo = {
46+
total: number,
47+
comment: Comment.State,
48+
parStack: string[],
49+
count: number[],
50+
}
51+
3852
export const ext = new Map([
3953
['.fsh', 'frag'],
4054
['.gsh', 'geom'],
@@ -113,14 +127,6 @@ function processIncludes(lines: string[], incStack: string[], allIncludes: Inclu
113127
}
114128
}
115129

116-
type LinesProcessingInfo = {
117-
total: number,
118-
comment: Comment.State,
119-
parStack: string[],
120-
count: number[],
121-
}
122-
123-
// TODO can surely be reworked
124130
export function getIncludes(uri: string, lines: string[]) {
125131
// the numbers start at -1 because we increment them as soon as we enter the loop so that we
126132
// dont have to put an incrememnt at each return
@@ -131,9 +137,10 @@ export function getIncludes(uri: string, lines: string[]) {
131137
count: [-1],
132138
}
133139

134-
return lines.reduce<IncludeObj[]>((out, line, i, l): IncludeObj[] => processLine(out, line, lines, i, lineInfo), [])
140+
return lines.reduce<IncludeObj[]>((out, line, i): IncludeObj[] => processLine(out, line, lines, i, lineInfo), [])
135141
}
136142

143+
// TODO can surely be reworked
137144
function processLine(includes: IncludeObj[], line: string, lines: string[], i: number, linesInfo: LinesProcessingInfo): IncludeObj[] {
138145
const updated = Comment.update(line, linesInfo.comment)
139146
linesInfo.comment = updated[0]
@@ -172,21 +179,23 @@ function processLine(includes: IncludeObj[], line: string, lines: string[], i: n
172179

173180
function ifInvalidFile(inc: IncludeObj, lines: string[], incStack: string[], diagnostics: Map<string, Diagnostic[]>) {
174181
const file = incStack[incStack.length - 1]
175-
diagnostics.set(
176-
inc.parent,
177-
[
178-
...(diagnostics.get(inc.parent) || []),
179-
{
180-
severity: DiagnosticSeverity.Error,
181-
range: calcRange(inc.lineNum - (win ? 1 : 0), file),
182-
message: `${inc.path.replace(conf.shaderpacksPath, '')} is missing or an invalid file.`,
183-
source: 'mc-glsl'
184-
}
185-
]
186-
)
187-
lines[inc.lineNumTopLevel] = ''
188-
// TODO fill in the actual data
189-
propogateDiagnostic(file, 'ERROR', inc.lineNum.toString(), `${inc.path.replace(conf.shaderpacksPath, '')} is missing or an invalid file.`, diagnostics, null)
182+
const diag: Diagnostic = {
183+
severity: DiagnosticSeverity.Error,
184+
range: calcRange(inc.lineNum - (win ? 1 : 0), file),
185+
message: `${inc.path.replace(conf.shaderpacksPath, '')} is missing or an invalid file.`,
186+
source: 'mc-glsl'
187+
}
188+
189+
diagnostics.set(inc.parent, [...(diagnostics.get(inc.parent) || []), diag])
190+
lines[inc.lineNumTopLevel] = lines[inc.lineNumTopLevel].replace(reInclude, '')
191+
192+
const error: ErrorMatch = {
193+
type: DiagnosticSeverity.Error,
194+
line: inc.lineNum,
195+
msg: `${inc.path.replace(conf.shaderpacksPath, '')} is missing or an invalid file.`,
196+
file,
197+
}
198+
propogateDiagnostic(error, diagnostics)
190199
}
191200

192201
function mergeInclude(inc: IncludeObj, lines: string[], incStack: string[], diagnostics: Map<string, Diagnostic[]>, hasDirective: boolean) {
@@ -236,43 +245,54 @@ function lint(docURI: string, lines: string[], includes: Map<string, IncludeObj>
236245
if (!diagnostics.has(obj.path)) diagnostics.set(obj.path, [])
237246
})
238247

239-
filterMatches(out).forEach((match) => {
240-
const [whole, type, file, line, msg] = match
248+
processErrors(out, docURI, diagnostics)
249+
250+
daigsArray(diagnostics).forEach(d => {
251+
if (win) d.uri = d.uri.replace('file://C:', 'file:///c%3A')
252+
connection.sendDiagnostics({uri: d.uri, diagnostics: d.diag})
253+
})
254+
}
255+
256+
function processErrors(out: string, docURI: string, diagnostics: Map<string, Diagnostic[]>) {
257+
filterMatches(out).forEach(match => {
258+
const error: ErrorMatch = {
259+
type: errorType(match[1]),
260+
file: match[2],
261+
line: parseInt(match[3]),
262+
msg: match[4]
263+
}
264+
241265
const diag: Diagnostic = {
242-
severity: errorType(type),
266+
severity: error.type,
243267
// had to do - 2 here instead of - 1, windows only perhaps?
244-
range: calcRange(parseInt(line) - (win ? 2 : 1), file.length - 1 ? file : docURI),
245-
message: `Line ${line} ${replaceWords(msg)}`,
268+
range: calcRange(error.line - (win ? 2 : 1), error.file.length - 1 ? error.file : docURI),
269+
message: `Line ${error.line} ${replaceWords(error.msg)}`,
246270
source: 'mc-glsl'
247271
}
248272

249-
diagnostics.get(file.length - 1 ? file : docURI).push(diag)
273+
diagnostics.get(error.file.length - 1 ? error.file : docURI).push(diag)
250274

251275
// if is an include, highlight an error in the parents line of inclusion
252-
propogateDiagnostic(file, type, line, msg, diagnostics, includes)
253-
})
254-
255-
daigsArray(diagnostics).forEach(d => {
256-
if (win) d.uri = d.uri.replace('file://C:', 'file:///c%3A')
257-
connection.sendDiagnostics({uri: d.uri, diagnostics: d.diag})
276+
propogateDiagnostic(error, diagnostics)
258277
})
259278
}
260279

261-
function propogateDiagnostic(errorFile: string, type: string, line: string, msg: string, diagnostics: Map<string, Diagnostic[]>, includes: Map<string, IncludeObj>, parentURI?: string) {
262-
includeGraph.get(parentURI || errorFile).parents.forEach((pair, parURI) => {
263-
console.log('parent', parURI, 'child', errorFile)
280+
//errorFile: string, type: DiagnosticSeverity, line: number, msg: string
281+
function propogateDiagnostic(error: ErrorMatch, diagnostics: Map<string, Diagnostic[]>, parentURI?: string) {
282+
includeGraph.get(parentURI || error.file).parents.forEach((pair, parURI) => {
283+
console.log('parent', parURI, 'child', error.file)
264284
const diag: Diagnostic = {
265-
severity: errorType(type),
285+
severity: error.type,
266286
range: calcRange(pair.first, parURI),
267-
message: `Line ${line} ${errorFile.replace(conf.shaderpacksPath, '')} ${replaceWords(msg)}`,
287+
message: `Line ${error.line} ${error.file.replace(conf.shaderpacksPath, '')} ${replaceWords(error.msg)}`,
268288
source: 'mc-glsl'
269289
}
270290

271291
if (!diagnostics.has(parURI)) diagnostics.set(parURI, [])
272292
diagnostics.get(parURI).push(diag)
273293

274294
if (pair.second.parents.size > 0) {
275-
propogateDiagnostic(errorFile, type, line, msg, diagnostics, includes, parURI)
295+
propogateDiagnostic(error, diagnostics, parURI)
276296
}
277297
})
278298
}

tslint.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
"max-classes-per-file": false,
1818
"no-console": false,
1919
"object-literal-key-quotes": false,
20+
"object-literal-shorthand": {
21+
"severity": "warning"
22+
},
2023
"eofline": false,
2124
"member-ordering": false,
2225
"interface-over-type-literal": false,

0 commit comments

Comments
 (0)