Skip to content

Commit c35a0e2

Browse files
refactor: diagnostics per editor buffer.
1 parent 04bef8f commit c35a0e2

3 files changed

Lines changed: 70 additions & 37 deletions

File tree

src/app.js

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2450,7 +2450,7 @@ const syncLintPendingState = () => {
24502450
setLintDiagnosticsPending(componentLintPending || stylesLintPending)
24512451
}
24522452

2453-
const runComponentLint = async ({ userInitiated = false } = {}) => {
2453+
const runComponentLint = ({ userInitiated = false, source = undefined } = {}) => {
24542454
activeComponentLintAbortController?.abort()
24552455
const controller = new AbortController()
24562456
activeComponentLintAbortController = controller
@@ -2460,24 +2460,28 @@ const runComponentLint = async ({ userInitiated = false } = {}) => {
24602460

24612461
setLintButtonLoading({ button: lintComponentButton, isLoading: true })
24622462

2463-
try {
2464-
const result = await lintDiagnostics.lintComponent({
2463+
return lintDiagnostics
2464+
.lintComponent({
24652465
signal: controller.signal,
24662466
userInitiated,
2467+
source,
2468+
})
2469+
.then(result => {
2470+
if (result) {
2471+
lastComponentLintIssueCount = result.issueCount
2472+
}
2473+
return result
2474+
})
2475+
.finally(() => {
2476+
decrementLintDiagnosticsRuns()
2477+
if (activeComponentLintAbortController === controller) {
2478+
activeComponentLintAbortController = null
2479+
setLintButtonLoading({ button: lintComponentButton, isLoading: false })
2480+
}
24672481
})
2468-
if (result) {
2469-
lastComponentLintIssueCount = result.issueCount
2470-
}
2471-
} finally {
2472-
decrementLintDiagnosticsRuns()
2473-
if (activeComponentLintAbortController === controller) {
2474-
activeComponentLintAbortController = null
2475-
setLintButtonLoading({ button: lintComponentButton, isLoading: false })
2476-
}
2477-
}
24782482
}
24792483

2480-
const runStylesLint = async ({ userInitiated = false } = {}) => {
2484+
const runStylesLint = ({ userInitiated = false, source = undefined } = {}) => {
24812485
activeStylesLintAbortController?.abort()
24822486
const controller = new AbortController()
24832487
activeStylesLintAbortController = controller
@@ -2487,21 +2491,25 @@ const runStylesLint = async ({ userInitiated = false } = {}) => {
24872491

24882492
setLintButtonLoading({ button: lintStylesButton, isLoading: true })
24892493

2490-
try {
2491-
const result = await lintDiagnostics.lintStyles({
2494+
return lintDiagnostics
2495+
.lintStyles({
24922496
signal: controller.signal,
24932497
userInitiated,
2498+
source,
2499+
})
2500+
.then(result => {
2501+
if (result) {
2502+
lastStylesLintIssueCount = result.issueCount
2503+
}
2504+
return result
2505+
})
2506+
.finally(() => {
2507+
decrementLintDiagnosticsRuns()
2508+
if (activeStylesLintAbortController === controller) {
2509+
activeStylesLintAbortController = null
2510+
setLintButtonLoading({ button: lintStylesButton, isLoading: false })
2511+
}
24942512
})
2495-
if (result) {
2496-
lastStylesLintIssueCount = result.issueCount
2497-
}
2498-
} finally {
2499-
decrementLintDiagnosticsRuns()
2500-
if (activeStylesLintAbortController === controller) {
2501-
activeStylesLintAbortController = null
2502-
setLintButtonLoading({ button: lintStylesButton, isLoading: false })
2503-
}
2504-
}
25052513
}
25062514

25072515
const markTypeDiagnosticsStale = () => {
@@ -2888,17 +2896,26 @@ if (diagnosticsClearAll) {
28882896
}
28892897
if (typecheckButton) {
28902898
typecheckButton.addEventListener('click', () => {
2891-
typeDiagnostics.triggerTypeDiagnostics({ userInitiated: true })
2899+
typeDiagnostics.triggerTypeDiagnostics({
2900+
userInitiated: true,
2901+
source: getJsxSource(),
2902+
})
28922903
})
28932904
}
28942905
if (lintComponentButton) {
28952906
lintComponentButton.addEventListener('click', () => {
2896-
void runComponentLint({ userInitiated: true })
2907+
void runComponentLint({
2908+
userInitiated: true,
2909+
source: getJsxSource(),
2910+
})
28972911
})
28982912
}
28992913
if (lintStylesButton) {
29002914
lintStylesButton.addEventListener('click', () => {
2901-
void runStylesLint({ userInitiated: true })
2915+
void runStylesLint({
2916+
userInitiated: true,
2917+
source: getCssSource(),
2918+
})
29022919
})
29032920
}
29042921
renderButton.addEventListener('click', renderPreview)

src/modules/lint-diagnostics.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,11 @@ export const createLintDiagnosticsController = ({
297297
}
298298
}
299299

300-
const lintComponent = async ({ signal, userInitiated = false } = {}) => {
300+
const lintComponent = async ({
301+
signal,
302+
userInitiated = false,
303+
source = undefined,
304+
} = {}) => {
301305
componentLintRunId += 1
302306
const runId = componentLintRunId
303307

@@ -310,7 +314,7 @@ export const createLintDiagnosticsController = ({
310314

311315
try {
312316
const diagnostics = await runLintDiagnostics({
313-
source: getComponentSource(),
317+
source: typeof source === 'string' ? source : getComponentSource(),
314318
path: lintPathByScope.component,
315319
signal,
316320
})
@@ -366,7 +370,11 @@ export const createLintDiagnosticsController = ({
366370
}
367371
}
368372

369-
const lintStyles = async ({ signal, userInitiated = false } = {}) => {
373+
const lintStyles = async ({
374+
signal,
375+
userInitiated = false,
376+
source = undefined,
377+
} = {}) => {
370378
stylesLintRunId += 1
371379
const runId = stylesLintRunId
372380

@@ -391,7 +399,7 @@ export const createLintDiagnosticsController = ({
391399
: lintPathByScope.styles
392400

393401
const diagnostics = await runLintDiagnostics({
394-
source: getStylesSource(),
402+
source: typeof source === 'string' ? source : getStylesSource(),
395403
path,
396404
signal,
397405
})

src/modules/type-diagnostics.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,10 @@ export const createTypeDiagnosticsController = ({
859859
.filter(diagnostic => !shouldIgnoreTypeDiagnostic(diagnostic))
860860
}
861861

862-
const runTypeDiagnostics = async (runId, { userInitiated = false } = {}) => {
862+
const runTypeDiagnostics = async (
863+
runId,
864+
{ userInitiated = false, sourceOverride = undefined } = {},
865+
) => {
863866
incrementTypeDiagnosticsRuns()
864867
setTypeDiagnosticsPending(false)
865868
setTypecheckButtonLoading(true)
@@ -876,7 +879,9 @@ export const createTypeDiagnosticsController = ({
876879
return
877880
}
878881

879-
const diagnostics = await collectTypeDiagnostics(compiler, getJsxSource())
882+
const sourceForRun =
883+
typeof sourceOverride === 'string' ? sourceOverride : getJsxSource()
884+
const diagnostics = await collectTypeDiagnostics(compiler, sourceForRun)
880885
const errorCategory = compiler.DiagnosticCategory?.Error
881886
const errors = diagnostics.filter(
882887
diagnostic => diagnostic.category === errorCategory,
@@ -938,9 +943,12 @@ export const createTypeDiagnosticsController = ({
938943
}
939944
}
940945

941-
const triggerTypeDiagnostics = ({ userInitiated = false } = {}) => {
946+
const triggerTypeDiagnostics = ({ userInitiated = false, source = undefined } = {}) => {
942947
typeCheckRunId += 1
943-
void runTypeDiagnostics(typeCheckRunId, { userInitiated })
948+
void runTypeDiagnostics(typeCheckRunId, {
949+
userInitiated,
950+
sourceOverride: source,
951+
})
944952
}
945953

946954
const scheduleTypeRecheck = () => {

0 commit comments

Comments
 (0)