Skip to content

Commit 0e90526

Browse files
authored
Merge pull request #686 from bash-lsp/remove-parsing-errors
Move infrequent and rather useless parsing error to logger
2 parents 7e07b96 + ea3e955 commit 0e90526

File tree

7 files changed

+32
-114
lines changed

7 files changed

+32
-114
lines changed

server/src/__tests__/analyzer.test.ts

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -42,60 +42,29 @@ describe('analyze', () => {
4242
document: FIXTURE_DOCUMENT.INSTALL,
4343
})
4444
expect(diagnostics).toEqual([])
45+
expect(loggerWarn).not.toHaveBeenCalled()
4546
})
4647

47-
it('returns a list of diagnostics for a file with a missing node', () => {
48+
it('parses files with a missing node', () => {
4849
const diagnostics = analyzer.analyze({
4950
uri: CURRENT_URI,
5051
document: FIXTURE_DOCUMENT.MISSING_NODE,
5152
})
52-
expect(diagnostics).not.toEqual([])
53-
expect(diagnostics).toMatchInlineSnapshot(`
54-
Array [
55-
Object {
56-
"message": "Syntax error: expected \\"fi\\" somewhere in the file",
57-
"range": Object {
58-
"end": Object {
59-
"character": 0,
60-
"line": 12,
61-
},
62-
"start": Object {
63-
"character": 0,
64-
"line": 12,
65-
},
66-
},
67-
"severity": 2,
68-
"source": "bash-language-server",
69-
},
70-
]
71-
`)
53+
expect(diagnostics).toEqual([])
54+
expect(loggerWarn).toHaveBeenCalledWith(
55+
'Error while parsing dummy-uri.sh: syntax error',
56+
)
7257
})
7358

74-
it('returns a list of diagnostics for a file with parsing errors', () => {
59+
it('parses a file with parsing errors', () => {
7560
const diagnostics = analyzer.analyze({
7661
uri: CURRENT_URI,
7762
document: FIXTURE_DOCUMENT.PARSE_PROBLEMS,
7863
})
79-
expect(diagnostics).not.toEqual([])
80-
expect(diagnostics).toMatchInlineSnapshot(`
81-
Array [
82-
Object {
83-
"message": "Failed to parse",
84-
"range": Object {
85-
"end": Object {
86-
"character": 1,
87-
"line": 9,
88-
},
89-
"start": Object {
90-
"character": 0,
91-
"line": 2,
92-
},
93-
},
94-
"severity": 1,
95-
"source": "bash-language-server",
96-
},
97-
]
98-
`)
64+
expect(diagnostics).toEqual([])
65+
expect(loggerWarn).toHaveBeenCalledWith(
66+
'Error while parsing dummy-uri.sh: syntax error',
67+
)
9968
})
10069

10170
it('returns a list of diagnostics for a file with sourcing issues', async () => {
@@ -802,7 +771,12 @@ describe('initiateBackgroundAnalysis', () => {
802771
globPattern: defaultConfig.globPattern,
803772
})
804773

805-
expect(loggerWarn).not.toHaveBeenCalled()
774+
expect(loggerWarn).toHaveBeenCalled()
775+
expect(loggerWarn.mock.calls).toEqual([
776+
[expect.stringContaining('missing-node.sh: syntax error')],
777+
[expect.stringContaining('not-a-shell-script.sh: syntax error')],
778+
[expect.stringContaining('parse-problems.sh: syntax error')],
779+
])
806780

807781
// Intro, stats on glob, one file skipped due to shebang, and outro
808782
expect(filesParsed).toEqual(FIXTURE_FILES_MATCHING_GLOB)

server/src/__tests__/config.test.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ describe('ConfigSchema', () => {
88
"backgroundAnalysisMaxFiles": 500,
99
"explainshellEndpoint": "",
1010
"globPattern": "**/*@(.sh|.inc|.bash|.command)",
11-
"highlightParsingErrors": false,
1211
"includeAllWorkspaceSymbols": false,
1312
"logLevel": "info",
1413
"shellcheckArguments": Array [],
@@ -22,7 +21,6 @@ describe('ConfigSchema', () => {
2221
backgroundAnalysisMaxFiles: 1,
2322
explainshellEndpoint: 'localhost:8080',
2423
globPattern: '**/*@(.sh)',
25-
highlightParsingErrors: true,
2624
includeAllWorkspaceSymbols: true,
2725
shellcheckArguments: ' -e SC2001 -e SC2002 ',
2826
shellcheckPath: '',
@@ -32,7 +30,6 @@ describe('ConfigSchema', () => {
3230
"backgroundAnalysisMaxFiles": 1,
3331
"explainshellEndpoint": "localhost:8080",
3432
"globPattern": "**/*@(.sh)",
35-
"highlightParsingErrors": true,
3633
"includeAllWorkspaceSymbols": true,
3734
"logLevel": "info",
3835
"shellcheckArguments": Array [
@@ -63,7 +60,6 @@ describe('getConfigFromEnvironmentVariables', () => {
6360
"backgroundAnalysisMaxFiles": 500,
6461
"explainshellEndpoint": "",
6562
"globPattern": "**/*@(.sh|.inc|.bash|.command)",
66-
"highlightParsingErrors": false,
6763
"includeAllWorkspaceSymbols": false,
6864
"logLevel": "info",
6965
"shellcheckArguments": Array [],
@@ -82,7 +78,6 @@ describe('getConfigFromEnvironmentVariables', () => {
8278
"backgroundAnalysisMaxFiles": 500,
8379
"explainshellEndpoint": "",
8480
"globPattern": "**/*@(.sh|.inc|.bash|.command)",
85-
"highlightParsingErrors": false,
8681
"includeAllWorkspaceSymbols": false,
8782
"logLevel": "info",
8883
"shellcheckArguments": Array [],
@@ -106,7 +101,6 @@ describe('getConfigFromEnvironmentVariables', () => {
106101
"backgroundAnalysisMaxFiles": 1,
107102
"explainshellEndpoint": "localhost:8080",
108103
"globPattern": "*.*",
109-
"highlightParsingErrors": false,
110104
"includeAllWorkspaceSymbols": false,
111105
"logLevel": "error",
112106
"shellcheckArguments": Array [
@@ -127,27 +121,27 @@ describe('getConfigFromEnvironmentVariables', () => {
127121
})
128122
it('parses boolean environment variables', () => {
129123
process.env = {
130-
HIGHLIGHT_PARSING_ERRORS: 'true',
124+
INCLUDE_ALL_WORKSPACE_SYMBOLS: 'true',
131125
}
132-
let result = getConfigFromEnvironmentVariables().config.highlightParsingErrors
126+
let result = getConfigFromEnvironmentVariables().config.includeAllWorkspaceSymbols
133127
expect(result).toEqual(true)
134128

135129
process.env = {
136-
HIGHLIGHT_PARSING_ERRORS: '1',
130+
INCLUDE_ALL_WORKSPACE_SYMBOLS: '1',
137131
}
138-
result = getConfigFromEnvironmentVariables().config.highlightParsingErrors
132+
result = getConfigFromEnvironmentVariables().config.includeAllWorkspaceSymbols
139133
expect(result).toEqual(true)
140134

141135
process.env = {
142-
HIGHLIGHT_PARSING_ERRORS: '0',
136+
INCLUDE_ALL_WORKSPACE_SYMBOLS: '0',
143137
}
144-
result = getConfigFromEnvironmentVariables().config.highlightParsingErrors
138+
result = getConfigFromEnvironmentVariables().config.includeAllWorkspaceSymbols
145139
expect(result).toEqual(false)
146140

147141
process.env = {
148-
HIGHLIGHT_PARSING_ERRORS: 'false',
142+
INCLUDE_ALL_WORKSPACE_SYMBOLS: 'false',
149143
}
150-
result = getConfigFromEnvironmentVariables().config.highlightParsingErrors
144+
result = getConfigFromEnvironmentVariables().config.includeAllWorkspaceSymbols
151145
expect(result).toEqual(false)
152146
})
153147
})

server/src/analyser.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ export default class Analyzer {
5757
/**
5858
* Analyze the given document, cache the tree-sitter AST, and iterate over the
5959
* tree to find declarations.
60-
*
61-
* Returns all, if any, syntax errors that occurred while parsing the file.
62-
*
6360
*/
6461
public analyze({
6562
document,
@@ -68,11 +65,12 @@ export default class Analyzer {
6865
document: TextDocument
6966
uri: string
7067
}): LSP.Diagnostic[] {
68+
const diagnostics: LSP.Diagnostic[] = []
7169
const fileContent = document.getText()
7270

7371
const tree = this.parser.parse(fileContent)
7472

75-
const { diagnostics, globalDeclarations } = getGlobalDeclarations({ tree, uri })
73+
const globalDeclarations = getGlobalDeclarations({ tree, uri })
7674

7775
const sourceCommands = sourcing.getSourceCommands({
7876
fileUri: uri,
@@ -115,24 +113,10 @@ export default class Analyzer {
115113
})
116114
}
117115

118-
function findMissingNodes(node: Parser.SyntaxNode) {
119-
if (node.isMissing()) {
120-
diagnostics.push(
121-
LSP.Diagnostic.create(
122-
TreeSitterUtil.range(node),
123-
`Syntax error: expected "${node.type}" somewhere in the file`,
124-
LSP.DiagnosticSeverity.Warning,
125-
undefined,
126-
'bash-language-server',
127-
),
128-
)
129-
} else if (node.hasError()) {
130-
node.children.forEach(findMissingNodes)
131-
}
116+
if (tree.rootNode.hasError()) {
117+
logger.warn(`Error while parsing ${uri}: syntax error`)
132118
}
133119

134-
findMissingNodes(tree.rootNode)
135-
136120
return diagnostics
137121
}
138122

server/src/config.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ export const ConfigSchema = z.object({
1616
// Log level for the server. To set the right log level from the start please also use the environment variable 'BASH_IDE_LOG_LEVEL'.
1717
logLevel: z.enum(LOG_LEVELS).default(DEFAULT_LOG_LEVEL),
1818

19-
// Controls if Treesitter parsing errors will be highlighted as problems.
20-
highlightParsingErrors: z.boolean().default(false),
21-
2219
// Controls how symbols (e.g. variables and functions) are included and used for completion and documentation.
2320
// If false, then we only include symbols from sourced files (i.e. using non dynamic statements like 'source file.sh' or '. file.sh').
2421
// If true, then all symbols from the workspace are included.
@@ -52,7 +49,6 @@ export function getConfigFromEnvironmentVariables(): {
5249
backgroundAnalysisMaxFiles: toNumber(process.env.BACKGROUND_ANALYSIS_MAX_FILES),
5350
explainshellEndpoint: process.env.EXPLAINSHELL_ENDPOINT,
5451
globPattern: process.env.GLOB_PATTERN,
55-
highlightParsingErrors: toBoolean(process.env.HIGHLIGHT_PARSING_ERRORS),
5652
includeAllWorkspaceSymbols: toBoolean(process.env.INCLUDE_ALL_WORKSPACE_SYMBOLS),
5753
logLevel: process.env[LOG_LEVEL_ENV_VAR],
5854
shellcheckArguments: process.env.SHELLCHECK_ARGUMENTS,

server/src/server.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,8 @@ export default class BashServer {
289289
public async analyzeAndLintDocument(document: TextDocument) {
290290
const { uri } = document
291291

292-
let diagnostics: LSP.Diagnostic[] = []
293-
294292
// Load the tree for the modified contents into the analyzer:
295-
const analyzeDiagnostics = this.analyzer.analyze({ uri, document })
296-
// Treesitter's diagnostics can be a bit inaccurate, so we only merge the
297-
// analyzer's diagnostics if the setting is enabled:
298-
if (this.config.highlightParsingErrors) {
299-
diagnostics = diagnostics.concat(analyzeDiagnostics)
300-
}
293+
let diagnostics = this.analyzer.analyze({ uri, document })
301294

302295
// Run ShellCheck diagnostics:
303296
if (this.linter) {

server/src/util/declarations.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,20 @@ export function getGlobalDeclarations({
3232
}: {
3333
tree: Parser.Tree
3434
uri: string
35-
}): {
36-
diagnostics: LSP.Diagnostic[]
37-
globalDeclarations: GlobalDeclarations
38-
} {
39-
const diagnostics: LSP.Diagnostic[] = []
35+
}): GlobalDeclarations {
4036
const globalDeclarations: GlobalDeclarations = {}
4137

4238
tree.rootNode.children.forEach((node) => {
43-
if (node.type === 'ERROR') {
44-
diagnostics.push(
45-
LSP.Diagnostic.create(
46-
TreeSitterUtil.range(node),
47-
'Failed to parse',
48-
LSP.DiagnosticSeverity.Error,
49-
undefined,
50-
'bash-language-server',
51-
),
52-
)
53-
return
54-
}
55-
5639
if (TreeSitterUtil.isDefinition(node)) {
5740
const symbol = nodeToSymbolInformation({ node, uri })
58-
5941
if (symbol) {
6042
const word = symbol.name
6143
globalDeclarations[word] = symbol
6244
}
6345
}
6446
})
6547

66-
return { diagnostics, globalDeclarations }
48+
return globalDeclarations
6749
}
6850

6951
/**

vscode-client/package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@
4747
"default": "**/*@(.sh|.inc|.bash|.command)",
4848
"description": "Glob pattern for finding and parsing shell script files in the workspace. Used by the background analysis features across files."
4949
},
50-
"bashIde.highlightParsingErrors": {
51-
"type": "boolean",
52-
"default": false,
53-
"description": "Controls if Treesitter parsing errors will be highlighted as problems."
54-
},
5550
"bashIde.includeAllWorkspaceSymbols": {
5651
"type": "boolean",
5752
"default": false,

0 commit comments

Comments
 (0)