Skip to content

feat: handle shellcheck's shell directive #1081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Bash Language Server

## 5.1.2

- Use shellcheck's shell directive for selecting the dialect https://github.com/bash-lsp/bash-language-server/pull/1081

## 5.1.1

- Add --help fallback for documentation https://github.com/bash-lsp/bash-language-server/pull/1052
Expand Down
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A language server for Bash",
"author": "Mads Hartmann",
"license": "MIT",
"version": "5.1.1",
"version": "5.1.2",
"main": "./out/server.js",
"typings": "./out/server.d.ts",
"bin": {
Expand Down
2 changes: 2 additions & 0 deletions server/src/__tests__/__snapshots__/server.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ exports[`server onRenameRequest Workspace-wide rename returns correct WorkspaceE
},
},
],
"file://__REPO_ROOT_FOLDER__/testing/fixtures/shellcheck/shell-directive.bash": [],
"file://__REPO_ROOT_FOLDER__/testing/fixtures/shellcheck/source.sh": [],
"file://__REPO_ROOT_FOLDER__/testing/fixtures/shellcheck/sourced.sh": [],
"file://__REPO_ROOT_FOLDER__/testing/fixtures/sourcing.sh": [],
Expand Down Expand Up @@ -997,6 +998,7 @@ exports[`server onRenameRequest Workspace-wide rename returns correct WorkspaceE
},
},
],
"file://__REPO_ROOT_FOLDER__/testing/fixtures/shellcheck/shell-directive.bash": [],
"file://__REPO_ROOT_FOLDER__/testing/fixtures/shellcheck/source.sh": [],
"file://__REPO_ROOT_FOLDER__/testing/fixtures/shellcheck/sourced.sh": [],
"file://__REPO_ROOT_FOLDER__/testing/fixtures/sourcing.sh": [],
Expand Down
2 changes: 1 addition & 1 deletion server/src/__tests__/analyzer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Logger } from '../util/logger'
const CURRENT_URI = 'dummy-uri.sh'

// if you add a .sh file to testing/fixtures, update this value
const FIXTURE_FILES_MATCHING_GLOB = 17
const FIXTURE_FILES_MATCHING_GLOB = 18

const defaultConfig = getDefaultConfiguration()

Expand Down
25 changes: 25 additions & 0 deletions server/src/util/__tests__/shebang.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FIXTURE_DOCUMENT } from '../../../../testing/fixtures'
import { analyzeShebang } from '../shebang'

describe('analyzeShebang', () => {
Expand Down Expand Up @@ -35,4 +36,28 @@ describe('analyzeShebang', () => {
expect(analyzeShebang(command).shellDialect).toBe(expectedDialect)
expect(analyzeShebang(`${command} `).shellDialect).toBe(expectedDialect)
})

it('returns shell dialect from shell directive', () => {
expect(analyzeShebang('# shellcheck shell=dash')).toEqual({
shellDialect: 'dash',
shebang: null,
})
})

it('returns shell dialect when multiple directives are passed', () => {
expect(
analyzeShebang(
'# shellcheck enable=require-variable-braces shell=dash disable=SC1000',
),
).toEqual({
shellDialect: 'dash',
shebang: null,
})
})

it('shell directive overrides file extension and shebang', () => {
expect(
analyzeShebang(FIXTURE_DOCUMENT.SHELLCHECK_SHELL_DIRECTIVE.getText()),
).toHaveProperty('shellDialect', 'sh')
})
})
27 changes: 26 additions & 1 deletion server/src/util/shebang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const SHELL_REGEXP = /bin[/](?:env )?(\w+)/
const BASH_DIALECTS = ['sh', 'bash', 'dash', 'ksh', 'zsh', 'csh', 'ash'] as const
type BashDialect = (typeof BASH_DIALECTS)[number]

const SHELL_DIRECTIVE_REGEXP = new RegExp(
`^\\s*#\\s*shellcheck.*shell=(${BASH_DIALECTS.join('|')}).*$|^\\s*#.*$|^\\s*$`,
)

export function getShebang(fileContent: string): string | null {
const match = SHEBANG_REGEXP.exec(fileContent)
if (!match || !match[1]) {
Expand All @@ -26,13 +30,34 @@ export function getShellDialect(shebang: string): BashDialect | null {
return null
}

export function getShellDialectFromShellDirective(
fileContent: string,
): BashDialect | null {
const contentLines = fileContent.split('\n')
for (const line of contentLines) {
const match = SHELL_DIRECTIVE_REGEXP.exec(line)
if (match === null) {
break
}
if (match[1]) {
const bashDialect = match[1].trim() as any
if (BASH_DIALECTS.includes(bashDialect)) {
return bashDialect
}
}
}
return null
}

export function analyzeShebang(fileContent: string): {
shellDialect: BashDialect | null
shebang: string | null
} {
const shebang = getShebang(fileContent)
return {
shebang,
shellDialect: shebang ? getShellDialect(shebang) : null,
shellDialect:
getShellDialectFromShellDirective(fileContent) ??
(shebang ? getShellDialect(shebang) : null),
}
}
5 changes: 5 additions & 0 deletions testing/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export const FIXTURE_URI = {
PARSE_PROBLEMS: `file://${path.join(FIXTURE_FOLDER, 'parse-problems.sh')}`,
SCOPE: `file://${path.join(FIXTURE_FOLDER, 'scope.sh')}`,
SHELLCHECK_SOURCE: `file://${path.join(FIXTURE_FOLDER, 'shellcheck', 'source.sh')}`,
SHELLCHECK_SHELL_DIRECTIVE: `file://${path.join(
FIXTURE_FOLDER,
'shellcheck',
'shell-directive.bash',
)}`,
SOURCING: `file://${path.join(FIXTURE_FOLDER, 'sourcing.sh')}`,
SOURCING2: `file://${path.join(FIXTURE_FOLDER, 'sourcing2.sh')}`,
RENAMING: `file://${path.join(FIXTURE_FOLDER, 'renaming.sh')}`,
Expand Down
12 changes: 12 additions & 0 deletions testing/fixtures/shellcheck/shell-directive.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/env ksh
# this shebang must be overriden by shell directive
# this line must be ignored

# shellcheck disable=SC1072 shell=sh enable=require-variable-braces


if [[ -n "$TERM" ]]; then
echo "$TERM"
fi

# shellcheck shell=dash