Skip to content

Commit

Permalink
feat: add parsing of isFlag property by Examples
Browse files Browse the repository at this point in the history
- Add parsing of argument property isFlag by Examples section

Closes #67
  • Loading branch information
ifedchankau committed Aug 16, 2018
1 parent a124184 commit c9505b3
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 18 deletions.
17 changes: 16 additions & 1 deletion src/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ const isValueBoolean = (string) => {
* @return {string} argumentType - type of argument
*/
const getPropertyType = (string, argument, context) => {
if (argument.isFlag) return null;
const typesDictionary = context.get.template.typesDictionary();
const argumentAddition = removeExtraArgumentNames(string, argument);
const result = Object.keys(typesDictionary)
Expand Down Expand Up @@ -135,6 +134,21 @@ const removeExtraEnumValues = (enumArray, regexp) => {
return result.length !== 0 ? result : null;
};

/**
* Set argument property isFlag by examples section
* @param {string} section - examples section
* @param {object} context - internal config
*/
const checkFlagsByExamples = (section, context) => {
const regularExp = new RegExp(context.regexp.examplesArgument, 'gim');
while (match = regularExp.exec(section)) {
let option = context.options.find((option) => {
return match[1] === (option.longName || option.shortName);
});
if (option) option.isFlag = false;
}
};

/**
* Remove extra coma from string
* @param {string} string - any string
Expand Down Expand Up @@ -225,4 +239,5 @@ exports = module.exports = {
getArgumentObject,
getDelimiterValue,
getValueByRegexp,
checkFlagsByExamples,
};
1 change: 1 addition & 0 deletions src/sections.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const examples = (section, context) => {
const delimiter = arguments.getDelimiterValue(
section, context.regexp.delimiter);
context.delimiter = delimiter ? delimiter : context.delimiter;
arguments.checkFlagsByExamples(section, context);
};

/**
Expand Down
12 changes: 3 additions & 9 deletions src/template/configDefault.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"Options"
],
"postfix": [
":",
"\n",
":\n"
":"
],
"parseWhole": true
},
Expand All @@ -16,19 +14,15 @@
"Usage"
],
"postfix": [
":",
"\n",
":\n"
":"
]
},
"examples": {
"names": [
"Examples"
],
"postfix": [
":",
"\n",
":\n"
":"
]
}
},
Expand Down
1 change: 1 addition & 0 deletions src/template/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const context = {
short: '(\\s|^)-[^-]*?(\\s|=|$)',
long: '--.+?(\\s|=|$)',
},
examplesArgument: '\\s+(-[\\S]+)(\\s|=)[^-\\s]+',
},
options: [],
delimiter: '',
Expand Down
2 changes: 1 addition & 1 deletion src/templatizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const templatizer = (context, config) => {
default:
const prefix = !option.isFlag ? `${nonFlagPrefix}:` : '';
optionSchema.id = prefix + argumentName;
optionSchema.type = option.type;
optionSchema.type = option.isFlag ? null : option.type;
}
optionSchema.description = option.description;
if (option.defaultValue) optionSchema.default = option.defaultValue;
Expand Down
4 changes: 1 addition & 3 deletions test/parsing/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
"postfix": [
":",
"s",
"s:",
"s\n",
"s:\n"
"s:"
]
}
}
Expand Down
9 changes: 7 additions & 2 deletions test/parsing/files/full.example.valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"type": "object",
"properties": {
"--extension": {
"id": "--extension",
"type": null,
"id": "args:--extension",
"type": "string",
"description": "Additional extension to lint [Can be set multiple times]"
},
"--no-esnext": {
Expand Down Expand Up @@ -154,6 +154,11 @@
"type": "object",
"description": "Specify parser options"
},
"--spaces": {
"id": "args:--spaces",
"type": "string",
"description": "Spaces"
},
"--ignore": {
"id": "--ignore",
"type": null,
Expand Down
5 changes: 3 additions & 2 deletions test/parsing/files/full.example.valid.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
-f, --error-filter <path> a module to filter errors
--max-warnings [integer] Number of warnings to trigger nonzero exit code
--parser-options Object Specify parser options
--spaces Spaces.


Examples:
Expand All @@ -45,9 +46,9 @@
Examples:
$ xo index.js
$ xo *.js !foo.js
$ xo --space
$ xo --spaces=1
$ xo --env=node --env=mocha
$ xo --init --space
$ xo --ignore --i
$ xo --plugin=react
$ xo --plugin=html --extension=html
$ echo 'const x=true' | xo --stdin --fix

0 comments on commit c9505b3

Please sign in to comment.