Skip to content

Commit 23fdfbb

Browse files
fix(configuration-location): fix filepath when configuration is not explicitely set
1 parent 43d7d9c commit 23fdfbb

File tree

6 files changed

+45
-7
lines changed

6 files changed

+45
-7
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
},
4141
"ava": {
4242
"files": [
43-
"source/test/**/*"
43+
"source/test/**/*",
44+
"!source/test/helpers/**/*"
4445
],
4546
"typescript": {
4647
"rewritePaths": {

source/entities/language-definition.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export interface LanguageDefinition {
2424
*/
2525
grammar?: string | DefinitionLocation;
2626
/**
27-
* The name or location of the language configuration file. If not provided, the `base` will be the `id`
28-
* of the language definition and the file will be `language-configuration.json`.
27+
* The name or location of the language configuration file. If not provided, the `base` will be the
28+
* base of the `grammar` and the file will be `language-configuration.json`.
2929
*/
3030
configuration?: string | DefinitionLocation;
3131
/**

source/test/configuration-location.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import test from 'ava';
2-
import { getLanguageConfigurationLocation } from '..';
2+
import { languages, getLanguageConfigurationLocation } from '..';
3+
import { fileExistsMacro } from './helpers/file-exists';
34

45
test('should throw if grammar property is invalid', (t) => {
56
t.throws(
@@ -78,3 +79,12 @@ test('should return location if configuration is a string', (t) => {
7879
'jsx-tags/tags-language-configuration.json'
7980
);
8081
});
82+
83+
// Iterate over all languages and test that the configuration file exists
84+
for (const language of languages) {
85+
test(
86+
`language configuration should exist for language ${language.language}`,
87+
fileExistsMacro,
88+
getLanguageConfigurationLocation(language)
89+
);
90+
}

source/test/grammar-location.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import test from 'ava';
2-
import { getGrammarLocation } from '..';
2+
import { languages, grammars, getGrammarLocation } from '..';
3+
import { fileExistsMacro } from './helpers/file-exists';
34

45
test('should throw if grammar property is invalid', (t) => {
56
t.throws(() => getGrammarLocation({ language: 'javascript', extensions: ['.js'], grammar: 1 } as any), {
@@ -104,3 +105,12 @@ test('should return grammar location if definition is a `LanguageDefinition` wit
104105
'scss/syntaxes/scss.tmLanguage.json'
105106
);
106107
});
108+
109+
// Iterate over all languages and grammars and test that the grammar file exists
110+
for (const language of languages) {
111+
test(`grammar should exist for language ${language.language}`, fileExistsMacro, getGrammarLocation(language));
112+
}
113+
114+
for (const grammar of grammars) {
115+
test(`grammar should exist for scope ${grammar.scopeName}`, fileExistsMacro, getGrammarLocation(grammar));
116+
}

source/test/helpers/file-exists.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-disable ava/use-test */
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
import { ExecutionContext, Macro } from 'ava';
5+
6+
export const fileExistsMacro: Macro<[string | undefined]> = (t: ExecutionContext, filePath: string | undefined) => {
7+
if (!filePath) {
8+
t.pass();
9+
10+
return;
11+
}
12+
13+
t.true(fs.existsSync(path.join(__dirname, '../../../grammars', filePath)));
14+
};

source/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,17 @@ export const getLanguageConfigurationLocation = (definition: LanguageDefinition)
5656
throw new TypeError('Expected `definition` to be `LanguageDefinition` or `GrammarDefinition`');
5757
}
5858

59+
// If the base is not set for the configuration file, use the base from the grammar or the language identifier
60+
const base = typeof definition.grammar === 'object' ? definition.grammar.base : definition.language;
61+
5962
if (!definition.configuration) {
6063
// The default configuration name is `language-configuration.json`.
61-
return path.join(definition.language, 'language-configuration.json');
64+
return path.join(base, 'language-configuration.json');
6265
}
6366

6467
if (typeof definition.configuration === 'string') {
6568
// If the configuration name is of type `string`, concatenate with the `language`.
66-
return path.join(definition.language, definition.configuration);
69+
return path.join(base || definition.language, definition.configuration);
6770
}
6871

6972
// If the `configuration` is defined and not a `string`, it has to be a definition location object.

0 commit comments

Comments
 (0)