Skip to content

Commit

Permalink
refactor: optimize rule tester implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Sep 27, 2024
1 parent 3bf2dd1 commit a5020a6
Showing 1 changed file with 15 additions and 28 deletions.
43 changes: 15 additions & 28 deletions tests/src/rule-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,22 @@ class FlatCompatRuleTester {
return config;
}

const obj = {
languageOptions: { parserOptions: {} },
const { parser, parserOptions = {}, languageOptions = {}, ...remainingConfig } = config;
const { ecmaVersion, sourceType, ...remainingParserOptions } = parserOptions;
const parserObj = typeof parser === 'string' ? require(parser) : parser;

return {
...remainingConfig,
languageOptions: {
...languageOptions,
...parserObj ? { parser: parserObj } : {},
...ecmaVersion ? { ecmaVersion } : {},
...sourceType ? { sourceType } : {},
parserOptions: {
...remainingParserOptions,
},
},
};

for (const [key, value] of Object.entries(config)) {
if (key === 'parser') {
obj.languageOptions.parser = typeof value === 'string' ? require(value) : value;

continue;
}

if (key === 'parserOptions') {
for (const [option, val] of Object.entries(value)) {
if (option === 'ecmaVersion' || option === 'sourceType') {
obj.languageOptions[option] = val;

continue;
}

obj.languageOptions.parserOptions[option] = val;
}

continue;
}

obj[key] = value;
}

return obj;
}
}

Expand Down

0 comments on commit a5020a6

Please sign in to comment.