Skip to content

Commit 5c17398

Browse files
authored
refactor(ts): init typescript conf + tsify tokenize-arg-string (yargs#272)
1 parent 8931ab0 commit 5c17398

12 files changed

+242
-83
lines changed

.editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 2
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

.eslintrc

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"overrides": [
3+
{
4+
"files": "*.ts",
5+
"parser": "@typescript-eslint/parser",
6+
"rules": {
7+
"no-unused-vars": "off",
8+
"no-useless-constructor": "off",
9+
"@typescript-eslint/no-unused-vars": "error",
10+
"@typescript-eslint/no-useless-constructor": "error"
11+
}
12+
}
13+
],
14+
"parserOptions": {
15+
"ecmaVersion": 2018,
16+
"sourceType": "module"
17+
},
18+
"plugins": [
19+
"@typescript-eslint/eslint-plugin"
20+
]
21+
}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
2+
build/
23
.nyc_output
34
node_modules
45
.DS_Store

.mocharc.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"spec": [
3+
"build/test",
4+
"test"
5+
]
6+
}

.nycrc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"exclude": [
3+
"build/test/**",
4+
"test/**"
5+
],
6+
"reporter": [
7+
"html",
8+
"text"
9+
],
10+
"lines": 100,
11+
"branches": "97",
12+
"statements": "100"
13+
}

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const camelCase = require('camelcase')
22
const decamelize = require('decamelize')
33
const path = require('path')
4-
const tokenizeArgString = require('./lib/tokenize-arg-string')
4+
const { tokenizeArgString } = require('./build/lib/tokenize-arg-string')
55
const util = require('util')
66

77
// See https://github.com/yargs/yargs-parser#supported-nodejs-versions for our

lib/tokenize-arg-string.js lib/tokenize-arg-string.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// take an un-split argv string and tokenize it.
2-
module.exports = function (argString) {
2+
export function tokenizeArgString (argString: string | any[]): string[] {
33
if (Array.isArray(argString)) {
44
return argString.map(e => typeof e !== 'string' ? e + '' : e)
55
}
66

77
argString = argString.trim()
88

99
let i = 0
10-
let prevC = null
11-
let c = null
12-
let opening = null
13-
const args = []
10+
let prevC: string | null = null
11+
let c: string | null = null
12+
let opening: string | null = null
13+
const args: string[] = []
1414

1515
for (let ii = 0; ii < argString.length; ii++) {
1616
prevC = c

package.json

+29-7
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
"description": "the mighty option parser used by yargs",
55
"main": "index.js",
66
"scripts": {
7-
"fix": "standard --fix",
8-
"test": "c8 --reporter=text --reporter=html mocha test/*.js",
9-
"posttest": "standard",
10-
"coverage": "c8 report --check-coverage check-coverage --lines=100 --branches=97 --statements=100"
7+
"fix": "standardx --fix && standardx --fix **/*.ts",
8+
"pretest": "npm run compile -- -p tsconfig.test.json",
9+
"test": "c8 --reporter=text --reporter=html mocha test/*.js",
10+
"posttest": "npm run check",
11+
"coverage": "c8 report --check-coverage",
12+
"check": "standardx && standardx **/*.ts",
13+
"compile": "rimraf build && tsc",
14+
"prepare": "npm run compile"
1115
},
1216
"repository": {
1317
"type": "git",
@@ -27,20 +31,38 @@
2731
"author": "Ben Coe <[email protected]>",
2832
"license": "ISC",
2933
"devDependencies": {
34+
"@types/chai": "^4.2.11",
35+
"@types/mocha": "^7.0.2",
36+
"@types/node": "^10.0.3",
37+
"@typescript-eslint/eslint-plugin": "^2.25.0",
38+
"@typescript-eslint/parser": "^2.25.0",
3039
"c8": "^7.1.2",
3140
"chai": "^4.2.0",
41+
"eslint": "^7.0.0",
42+
"eslint-plugin-import": "^2.20.1",
43+
"eslint-plugin-node": "^11.0.0",
44+
"gts": "^2.0.0-alpha.4",
3245
"mocha": "^7.2.0",
33-
"standard": "^14.3.4"
46+
"rimraf": "^3.0.2",
47+
"standardx": "^5.0.0",
48+
"typescript": "^3.7.0"
3449
},
3550
"dependencies": {
3651
"camelcase": "^6.0.0",
3752
"decamelize": "^4.0.0"
3853
},
3954
"files": [
40-
"lib",
41-
"index.js"
55+
"index.js",
56+
"build",
57+
"lib/**/*.js"
4258
],
4359
"engines": {
4460
"node": ">=10"
61+
},
62+
"standardx": {
63+
"ignore": [
64+
"build",
65+
"example.js"
66+
]
4567
}
4668
}

test/tokenize-arg-string.js

-70
This file was deleted.

test/tokenize-arg-string.ts

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/* global describe, it */
2+
import { expect, should } from 'chai'
3+
import { tokenizeArgString } from '../lib/tokenize-arg-string'
4+
5+
should()
6+
7+
describe('TokenizeArgString', function () {
8+
it('handles unquoted string', function () {
9+
const args = tokenizeArgString('--foo 99')
10+
args[0].should.equal('--foo')
11+
args[1].should.equal('99')
12+
})
13+
14+
it('handles unquoted numbers', function () {
15+
const args = tokenizeArgString(['--foo', 9])
16+
args[0].should.equal('--foo')
17+
args[1].should.equal('9')
18+
})
19+
20+
it('handles quoted string with no spaces', function () {
21+
const args = tokenizeArgString("--foo 'hello'")
22+
args[0].should.equal('--foo')
23+
args[1].should.equal("'hello'")
24+
})
25+
26+
it('handles single quoted string with spaces', function () {
27+
const args = tokenizeArgString("--foo 'hello world' --bar='foo bar'")
28+
args[0].should.equal('--foo')
29+
args[1].should.equal("'hello world'")
30+
args[2].should.equal("--bar='foo bar'")
31+
})
32+
33+
it('handles double quoted string with spaces', function () {
34+
const args = tokenizeArgString('--foo "hello world" --bar="foo bar"')
35+
args[0].should.equal('--foo')
36+
args[1].should.equal('"hello world"')
37+
args[2].should.equal('--bar="foo bar"')
38+
})
39+
40+
it('handles single quoted empty string', function () {
41+
const args = tokenizeArgString('--foo \'\' --bar=\'\'')
42+
args[0].should.equal('--foo')
43+
args[1].should.equal("''")
44+
args[2].should.equal("--bar=''")
45+
})
46+
47+
it('handles double quoted empty string', function () {
48+
const args = tokenizeArgString('--foo "" --bar=""')
49+
args[0].should.equal('--foo')
50+
args[1].should.equal('""')
51+
args[2].should.equal('--bar=""')
52+
})
53+
54+
it('handles quoted string with embedded quotes', function () {
55+
var args = tokenizeArgString('--foo "hello \'world\'" --bar=\'foo "bar"\'')
56+
args[0].should.equal('--foo')
57+
args[1].should.equal('"hello \'world\'"')
58+
args[2].should.equal('--bar=\'foo "bar"\'')
59+
})
60+
61+
// https://github.com/yargs/yargs-parser/pull/100
62+
// https://github.com/yargs/yargs-parser/pull/106
63+
it('ignores unneeded spaces', function () {
64+
const args = tokenizeArgString(' foo bar "foo bar" ')
65+
args[0].should.equal('foo')
66+
expect(args[1]).equal('bar')
67+
expect(args[2]).equal('"foo bar"')
68+
})
69+
70+
it('handles boolean options', function () {
71+
const args = tokenizeArgString('--foo -bar')
72+
expect(args[0]).to.equal(('--foo'))
73+
expect(args[1]).to.equal(('-bar'))
74+
})
75+
76+
it('handles empty string', function () {
77+
const args = tokenizeArgString('')
78+
expect(args.length).to.equal(0)
79+
})
80+
81+
it('handles array with unquoted string', function () {
82+
const args = tokenizeArgString(['--foo', '99'])
83+
args[0].should.equal('--foo')
84+
args[1].should.equal('99')
85+
})
86+
87+
it('handles array with quoted string with no spaces', function () {
88+
const args = tokenizeArgString(['--foo', "'hello'"])
89+
args[0].should.equal('--foo')
90+
args[1].should.equal("'hello'")
91+
})
92+
93+
it('handles array with single quoted string with spaces', function () {
94+
const args = tokenizeArgString(['--foo', "'hello world'", "--bar='foo bar'"])
95+
args[0].should.equal('--foo')
96+
args[1].should.equal("'hello world'")
97+
args[2].should.equal("--bar='foo bar'")
98+
})
99+
100+
it('handles array with double quoted string with spaces', function () {
101+
const args = tokenizeArgString(['--foo', '"hello world"', '--bar="foo bar"'])
102+
args[0].should.equal('--foo')
103+
args[1].should.equal('"hello world"')
104+
args[2].should.equal('--bar="foo bar"')
105+
})
106+
107+
it('handles array with single quoted empty string', function () {
108+
const args = tokenizeArgString(['--foo', "''", "--bar=''"])
109+
args[0].should.equal('--foo')
110+
args[1].should.equal("''")
111+
args[2].should.equal("--bar=''")
112+
})
113+
114+
it('handles array with double quoted empty string', function () {
115+
const args = tokenizeArgString(['--foo', '""', '--bar=""'])
116+
args[0].should.equal('--foo')
117+
args[1].should.equal('""')
118+
args[2].should.equal('--bar=""')
119+
})
120+
121+
it('handles array with quoted string with embedded quotes', function () {
122+
var args = tokenizeArgString(['--foo', '"hello \'world\'"', '--bar=\'foo "bar"\''])
123+
args[0].should.equal('--foo')
124+
args[1].should.equal('"hello \'world\'"')
125+
args[2].should.equal('--bar=\'foo "bar"\'')
126+
})
127+
128+
it('handles array with boolean options', function () {
129+
const args = tokenizeArgString(['--foo', '-bar'])
130+
expect(args[0]).to.equal('--foo')
131+
expect(args[1]).to.equal('-bar')
132+
})
133+
})

tsconfig.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./node_modules/gts/tsconfig-google.json",
3+
"compilerOptions": {
4+
"outDir": "build",
5+
"rootDir": ".",
6+
"sourceMap": false
7+
},
8+
"include": [
9+
"lib/**/*.ts"
10+
]
11+
}

tsconfig.test.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"sourceMap": true
5+
},
6+
"include": [
7+
"lib/**/*.ts",
8+
"test/**/*.ts"
9+
]
10+
}

0 commit comments

Comments
 (0)