From 12bc48b59419f0e00b9bd9fbfcb9f16f22cd345e Mon Sep 17 00:00:00 2001 From: Yoav Sabag Date: Sun, 18 Feb 2024 21:45:22 +0200 Subject: [PATCH 1/3] fix: yarn argument value identified as a package --- src/content/registry/shared.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/content/registry/shared.js b/src/content/registry/shared.js index b2562b6..1c7f886 100644 --- a/src/content/registry/shared.js +++ b/src/content/registry/shared.js @@ -6,6 +6,10 @@ const finishAllWords = (argsAndPackagesWords) => { return length; }; +const isNumber = (str) => { + return /^\d+(\.\d+)?$/.test(str); +}; + /** * @param {string} registryName `type` in result element * @param {(line: string) => RegExpMatchArray} getBaseCommandMatch get the base command match for a line (examples: `pip install`, `yarn add`) @@ -41,7 +45,7 @@ export const createParseCommand = continue; } - if (word.startsWith('-')) { + if (word.startsWith('-') || isNumber(word)) { counterIndex += handleArgument(word, argsAndPackagesWords); continue; } From f1e8ff83d17436ce2a1c02e4434cfa7b42fa7811 Mon Sep 17 00:00:00 2001 From: Yoav Sabag Date: Sun, 18 Feb 2024 22:10:20 +0200 Subject: [PATCH 2/3] fix: add tests --- src/content/registry/npm.test.js | 1 + tests/real-examples/real-examples.yaml | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/content/registry/npm.test.js b/src/content/registry/npm.test.js index 97c58ff..5fc202a 100644 --- a/src/content/registry/npm.test.js +++ b/src/content/registry/npm.test.js @@ -20,6 +20,7 @@ describe('npm', () => { 'npm install -g', '`npm install node-sass`', // this is not a valid command because of the ` 'npm create-react-app my-app', + 'yarn add --network-timeout 100000', ])('should return empty array if no packages found', (command) => { expect(parseCommand(command)).toStrictEqual([]); }); diff --git a/tests/real-examples/real-examples.yaml b/tests/real-examples/real-examples.yaml index 296cbd7..7bc4ba5 100644 --- a/tests/real-examples/real-examples.yaml +++ b/tests/real-examples/real-examples.yaml @@ -27,6 +27,10 @@ links: comment: ignore 'npm install -d' & find '/usr/local/bin/npm install jade' post: answer registry: npm + https://stackoverflow.com/questions/51508364: + comment: ignore 'yarn add --network-timeout 100000' + post: answer + registry: npm https://stackoverflow.com/questions/12008719: comment: npm install --save post: answer From 86e69b1f55df36ee133d557981375443ec09f474 Mon Sep 17 00:00:00 2001 From: Yoav Sabag Date: Tue, 20 Feb 2024 22:20:16 +0200 Subject: [PATCH 3/3] fix: review comments --- src/content/registry/npm.js | 24 ++++++++++++++----- src/content/registry/shared.js | 6 +---- .../real-examples/real-examples-results.yaml | 10 ++++++++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/content/registry/npm.js b/src/content/registry/npm.js index f53a15f..2c41170 100644 --- a/src/content/registry/npm.js +++ b/src/content/registry/npm.js @@ -20,12 +20,24 @@ const parsePackageString = (str) => { }; }; -const parseNpmCommand = createParseCommand( - 'npm', - (line) => line.match(npmInstall), - (word) => word.length + 1, - parsePackageString -); +const npmOptionWithArgToIgnore = ['--network-timeout', '--network-concurrency']; + +//npm +const handleArgument = (argument, restCommandWords) => { + let index = 0; + index += argument.length + 1; // +1 for the space removed by split + + if (!npmOptionWithArgToIgnore.includes(argument)) { + return index; + } + + if (argument.includes('=')) return index; + + index += restCommandWords.shift().length + 1; + return index; +}; + +const parseNpmCommand = createParseCommand('npm', (line) => line.match(npmInstall), handleArgument, parsePackageString); // npx const parseOnlyFirstPackage = (str, argsAndPackagesWords) => { diff --git a/src/content/registry/shared.js b/src/content/registry/shared.js index 1c7f886..b2562b6 100644 --- a/src/content/registry/shared.js +++ b/src/content/registry/shared.js @@ -6,10 +6,6 @@ const finishAllWords = (argsAndPackagesWords) => { return length; }; -const isNumber = (str) => { - return /^\d+(\.\d+)?$/.test(str); -}; - /** * @param {string} registryName `type` in result element * @param {(line: string) => RegExpMatchArray} getBaseCommandMatch get the base command match for a line (examples: `pip install`, `yarn add`) @@ -45,7 +41,7 @@ export const createParseCommand = continue; } - if (word.startsWith('-') || isNumber(word)) { + if (word.startsWith('-')) { counterIndex += handleArgument(word, argsAndPackagesWords); continue; } diff --git a/tests/real-examples/real-examples-results.yaml b/tests/real-examples/real-examples-results.yaml index 526373d..2fbed30 100644 --- a/tests/real-examples/real-examples-results.yaml +++ b/tests/real-examples/real-examples-results.yaml @@ -424,6 +424,16 @@ https://stackoverflow.com/questions/3608384: type: npm name: jade +https://stackoverflow.com/questions/51508364: + comment: ignore 'yarn add --network-timeout 100000' + post: answer + registry: npm + results: + - range: |- + npx create-react-app app --use-npm + + type: npm + name: create-react-app https://stackoverflow.com/questions/12008719: comment: npm install --save post: answer