Skip to content

Commit 3535d7b

Browse files
stevenzeckGlavin001
authored andcommitted
Add feature to specify path to beautifier config file (#223)
* Add feature to specify path to beautifier config file * Change to resolve config based on passed file path * Minor refactor * Make suggested change * Use prefer_beautifier_config option intsead * Fix typo, unibeautify issue
1 parent 123b835 commit 3535d7b

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/beautifier.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,13 @@ export class Unibeautify {
397397
beautifierOptions.prefer_beautifier_config &&
398398
beautifier.resolveConfig
399399
) {
400+
const resolveConfigPath: string | undefined =
401+
typeof beautifierOptions.prefer_beautifier_config === "string"
402+
? beautifierOptions.prefer_beautifier_config
403+
: filePath;
400404
return beautifier.resolveConfig({
401405
dependencies: dependencyManager,
402-
filePath,
406+
filePath: resolveConfigPath,
403407
projectPath,
404408
});
405409
}

test/beautifier/config.spec.ts

+57
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,60 @@ describe("prefer beautifier config enabled", () => {
170170
).resolves.toBe(beautifierResult);
171171
});
172172
});
173+
174+
describe("prefer beautifier config enabled", () => {
175+
test("should use path to beautifier config file", () => {
176+
expect.assertions(3);
177+
const unibeautify = new Unibeautify();
178+
const lang: Language = {
179+
atomGrammars: [],
180+
extensions: ["test"],
181+
name: "TestLang",
182+
namespace: "test",
183+
since: "0.1.0",
184+
sublimeSyntaxes: [],
185+
vscodeLanguages: [],
186+
};
187+
unibeautify.loadLanguage(lang);
188+
189+
const beautifierResult = "Testing Result";
190+
const configPath = "C:\\path1\\path2";
191+
const dependency: DependencyDefinition = {
192+
name: "Node",
193+
program: "node",
194+
type: DependencyType.Executable,
195+
};
196+
const beautifier: Beautifier = {
197+
beautify: ({ dependencies, beautifierConfig }) => {
198+
expect(() => dependencies.get(dependency.name)).not.toThrowError();
199+
return Promise.resolve(beautifierConfig && beautifierConfig.config);
200+
},
201+
dependencies: [dependency],
202+
name: "TestBeautify",
203+
options: {
204+
TestLang: false,
205+
},
206+
resolveConfig: ({ dependencies, filePath, projectPath }) => {
207+
expect(() => dependencies.get(dependency.name)).not.toThrowError();
208+
return Promise.resolve({
209+
config: beautifierResult,
210+
filePath: configPath,
211+
});
212+
},
213+
};
214+
unibeautify.loadBeautifier(beautifier);
215+
return expect(
216+
unibeautify.beautify({
217+
languageName: lang.name,
218+
options: {
219+
[lang.name]: {
220+
[beautifier.name]: {
221+
prefer_beautifier_config: configPath,
222+
},
223+
},
224+
},
225+
text: "test",
226+
})
227+
).resolves.toBe(beautifierResult);
228+
});
229+
});

0 commit comments

Comments
 (0)