Skip to content

Commit

Permalink
fix: directives with trailing comments (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjake authored Feb 20, 2024
1 parent dad69c6 commit 4fec097
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/ssh-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export default class SSHConfig extends Array<Line> {
}
}
}

if (canonicalDomains.length > 0 && canonicalizeHostName) {
for (const domain of canonicalDomains) {
const host = `${line.value}.${domain}`
Expand Down Expand Up @@ -490,6 +490,9 @@ export function parse(text: string): SSHConfig {
else if (chr === '\\') {
escaped = true
}
else if (chr === '#' && !quoted) {
break
}
else {
val += chr
}
Expand Down Expand Up @@ -545,6 +548,9 @@ export function parse(text: string): SSHConfig {
}
// otherwise ignore the space
}
else if (chr === '#') {
break
}
else {
val += chr
}
Expand Down
22 changes: 22 additions & 0 deletions test/unit/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,26 @@ describe('parse', function() {
final: [],
})
})

it('.parse match with comments', function() {
const config = parse(`
# CLOUDFLARE SETUP https://URL_REDACTED
Match all # CLOUDFLARE SETUP
Include /Users/mtovino/.ssh/cloudflare/config # CLOUDFLARE SETUP
`)
const match = config.find(line => line.type === DIRECTIVE && line.param === 'Match')
assert.ok(match)
assert.ok('criteria' in match)
assert.deepEqual(match.criteria, {
all: [],
})
assert.deepEqual(match.config.find(entry => 'param' in entry && entry.param === 'Include'), {
after: '',
before: ' ',
param: 'Include',
separator: ' ',
type: 1,
value: '/Users/mtovino/.ssh/cloudflare/config',
})
})
})

0 comments on commit 4fec097

Please sign in to comment.