-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve included method detection (#554)
- Loading branch information
1 parent
2317a5b
commit 578d852
Showing
9 changed files
with
190 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule ahk2
updated
from c05e25 to 22d94d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { suite, test } from 'mocha'; | ||
import assert from 'assert'; | ||
import { getIncludedPath, resolveIncludedPath } from './defProvider.utils'; | ||
|
||
suite(getIncludedPath.name, () => { | ||
const tests: [ | ||
name: string, | ||
args: Parameters<typeof getIncludedPath>, | ||
expected: ReturnType<typeof getIncludedPath>, | ||
][] = [ | ||
['comma', ['#include , a b.ahk'], 'a b.ahk'], | ||
['no hash', ['include , a b.ahk'], undefined], | ||
[ | ||
'no comma nor extra space', | ||
['#include path/to/file.ahk'], | ||
'path/to/file.ahk', | ||
], | ||
['ah1', ['#include a.ah1'], 'a.ah1'], | ||
['ahk1', ['#include a.ahk1'], 'a.ahk1'], | ||
['ext', ['#include a.ext'], 'a.ext'], | ||
['preceding whitespace', [' #include a.ahk'], 'a.ahk'], | ||
['directory', ['#include a'], 'a'], | ||
['non-whitespace preceding char', [';#include a'], undefined], | ||
['escaped `;` with whitespace', ['#include a `;b.ahk'], 'a `;b.ahk'], | ||
['escaped `;` without whitespace', ['#include a`;b.ahk'], 'a`;b.ahk'], | ||
['unescaped `;` without whitespace', ['#include a;b.ahk'], 'a;b.ahk'], | ||
['unescaped `;` with whitespace', ['#include a ;b.ahk'], 'a'], | ||
['unescaped valid `%`', ['#include %A_ScriptDir%'], '%A_ScriptDir%'], | ||
['unescaped `<` and `>`', ['#include <foo>'], '<foo>'], | ||
]; | ||
tests.forEach(([name, args, expected]) => | ||
test(name, () => | ||
assert.strictEqual(getIncludedPath(...args), expected), | ||
), | ||
); | ||
}); | ||
|
||
suite(resolveIncludedPath.name, () => { | ||
const tests: [ | ||
name: string, | ||
args: Parameters<typeof resolveIncludedPath>, | ||
expected: ReturnType<typeof resolveIncludedPath>, | ||
][] = [ | ||
['relative file', ['/c:/main.ahk', 'a.ahk'], 'c:\\a.ahk'], | ||
['absolute file', ['/c:/users/main.ahk', 'd:/b.ahk'], 'd:\\b.ahk'], | ||
['with single dot', ['/c:/main.ahk', './c.ahk'], 'c:\\c.ahk'], | ||
['with double dot', ['/c:/users/main.ahk', '../d.ahk'], 'c:\\d.ahk'], | ||
]; | ||
tests.forEach(([name, args, expected]) => | ||
test(name, () => | ||
assert.strictEqual( | ||
resolveIncludedPath(args[0], `#include ${args[1]}`), | ||
expected, | ||
), | ||
), | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//* Utilities not requiring the vscode API | ||
|
||
import { isAbsolute, join, normalize } from 'path'; | ||
|
||
/** | ||
** Returns the string representing the included path after the `#include`. | ||
** Only works for actual `#include` directives, not comments or strings containing `#include`. | ||
** Does not resolve or normalize the included path. | ||
* @example | ||
* getIncludedPath('#include , a b.ahk') === 'a b.ahk' | ||
* getIncludedPath(' #include path/to/file.ahk') === 'path/to/file.ahk' | ||
* getIncludedPath('include , a b.ahk') === undefined // no `#` | ||
* getIncludedPath('; #include , a b.ahk') === undefined | ||
* getIncludedPath('x := % "#include , a b.ahk"') === undefined | ||
* getIncludedPath('#include a') === 'a' | ||
* getIncludedPath('#include %A_ScriptDir%') === '%A_ScriptDir%' | ||
* getIncludedPath('#include <myLib>') === '<myLib>' | ||
* getIncludedPath('#include semi-colon ;and-more.ahk') === 'semi-colon' | ||
* getIncludedPath('#include semi-colon`;and-more.ahk') === 'semi-colon`;and-more.ahk' | ||
*/ | ||
export const getIncludedPath = (ahkLine: string): string | undefined => | ||
ahkLine.match(/^\s*#include\s*,?\s*(.+?)( ;.*)?$/i)?.[1]; | ||
|
||
const normalizeIncludedPath = ( | ||
includedPath: string, | ||
basePath: string, | ||
parentGoodPath: string, | ||
): string => | ||
normalize( | ||
includedPath | ||
.trim() | ||
.replace(/`;/g, ';') // only semi-colons are escaped | ||
.replace(/(%A_ScriptDir%|%A_WorkingDir%)/, parentGoodPath) | ||
.replace(/(%A_LineFile%)/, basePath), | ||
); | ||
|
||
/** | ||
* Returns the absolute, normalized path included by a #include directive. | ||
* Does not check if that path is a to a folder, or if that path exists. | ||
* | ||
* @param basePath | ||
* The path to include from, usually the script's path. | ||
* | ||
* This may be a different path if the including script has a preceding `#include dir` | ||
* | ||
* @param includedPath The path that's included in the `#include` directive | ||
*/ | ||
export const resolveIncludedPath = ( | ||
/** | ||
* The path of the current script, namely `vscode.document.uri.path`: | ||
* @example '/c:/path/to/file.ahk' | ||
*/ | ||
basePath: string, | ||
/** Line of text from the including script. */ | ||
ahkLine: string, | ||
): string | undefined => { | ||
const includedPath = getIncludedPath(ahkLine); | ||
/** @example 'c:/path/to' */ | ||
const parentGoodPath = basePath.substring(1, basePath.lastIndexOf('/')); | ||
const normalizedPath = normalizeIncludedPath( | ||
includedPath, | ||
basePath, | ||
parentGoodPath, | ||
); | ||
const absolutePath = isAbsolute(includedPath) | ||
? normalize(includedPath) | ||
: join(parentGoodPath, normalizedPath); | ||
return absolutePath; | ||
}; |