Skip to content

Commit 24d31d5

Browse files
committed
Use JavaScript Standard Style
1 parent 378bf6e commit 24d31d5

File tree

5 files changed

+97
-76
lines changed

5 files changed

+97
-76
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ node_js:
77
- "4.2"
88
- "node"
99
sudo: false
10+
script: [ "npm test", "npm run lint" ]

README.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,45 @@ var parse = require('spdx-expression-parse')
33
var assert = require('assert')
44

55
var firstAST = {
6-
left: { license: 'LGPL-2.1' },
6+
left: {license: 'LGPL-2.1'},
77
conjunction: 'or',
88
right: {
9-
left: { license: 'BSD-3-Clause' },
9+
left: {license: 'BSD-3-Clause'},
1010
conjunction: 'and',
11-
right: { license: 'MIT' } } }
11+
right: {license: 'MIT'}
12+
}
13+
}
1214

1315
assert.deepEqual(
1416
parse('(LGPL-2.1 OR BSD-3-Clause AND MIT)'),
15-
firstAST)
17+
firstAST
18+
)
1619

1720
var secondAST = {
18-
left: { license: 'MIT' },
21+
left: {license: 'MIT'},
1922
conjunction: 'and',
2023
right: {
21-
left: {
22-
license: 'LGPL-2.1',
23-
plus: true },
24+
left: {license: 'LGPL-2.1', plus: true},
2425
conjunction: 'and',
25-
right: { license: 'BSD-3-Clause' } } }
26+
right: {license: 'BSD-3-Clause'}
27+
}
28+
}
2629

2730
assert.deepEqual(
2831
parse('(MIT AND (LGPL-2.1+ AND BSD-3-Clause))'),
29-
secondAST)
32+
secondAST
33+
)
3034

3135
// We handle all the bare SPDX license and exception ids as well.
32-
require('spdx-license-ids').forEach(function(id) {
33-
assert.deepEqual(
34-
parse(id),
35-
{ license: id })
36-
require('spdx-exceptions').forEach(function(e) {
36+
require('spdx-license-ids').forEach(function (id) {
37+
assert.deepEqual(parse(id), {license: id})
38+
require('spdx-exceptions').forEach(function (e) {
3739
assert.deepEqual(
3840
parse(id + ' WITH ' + e),
39-
{ license: id, exception: e }) }) })
41+
{license: id, exception: e}
42+
)
43+
})
44+
})
4045
```
4146

4247
---

generate-parser.js

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,98 @@ var Generator = require('jison').Generator
22
var options = {
33
type: 'slr',
44
moduleType: 'commonjs',
5-
moduleName: 'spdxparse' }
5+
moduleName: 'spdxparse'
6+
}
67

7-
var words = [ 'AND', 'OR', 'WITH' ]
8+
var words = ['AND', 'OR', 'WITH']
89

9-
var quote = function(argument) {
10-
return '\'' + argument + '\'' }
10+
var quote = function (argument) {
11+
return '\'' + argument + '\''
12+
}
1113

12-
var regexEscape = function(s) {
13-
return s.replace(/[\^\\$*+?.()|{}\[\]\/]/g, '\\$&') }
14+
var regexEscape = function (s) {
15+
return s.replace(/[\^\\$*+?.()|{}\[\]\/]/g, '\\$&')
16+
}
1417

15-
var handleLicensesAndExceptions = function() {
18+
var handleLicensesAndExceptions = function () {
1619
var ids = require('spdx-license-ids')
1720
var exceptions = require('spdx-exceptions')
1821

1922
// Sort tokens longest-first (both license ids and exception strings)
2023
var tokens = ids.concat(exceptions)
21-
tokens.sort(function(a, b) { return ( b.length - a.length ) })
22-
return tokens.map(function(t) {
23-
var type = ( (ids.indexOf(t) >= 0) ? 'LICENSE' : 'EXCEPTION' )
24-
return [ regexEscape(t), ( 'return ' + quote(type) ) ] }) }
24+
tokens.sort(function (a, b) { return b.length - a.length })
25+
return tokens.map(function (t) {
26+
var type = (ids.indexOf(t) >= 0) ? 'LICENSE' : 'EXCEPTION'
27+
return [regexEscape(t), 'return ' + quote(type)]
28+
})
29+
}
2530

2631
var grammar = {
2732
lex: {
28-
macros: { },
33+
macros: {},
2934
rules: [
30-
[ '$', 'return ' + quote('EOS') ],
31-
[ '\\s+', '/* skip whitespace */' ],
32-
[ '\\+', 'return ' + quote('PLUS') ],
33-
[ '\\(', 'return ' + quote('OPEN') ],
34-
[ '\\)', 'return ' + quote('CLOSE') ],
35-
[ ':', 'return ' + quote('COLON') ],
36-
[ 'DocumentRef-([0-9A-Za-z-+.]+)',
37-
'return ' + quote('DOCUMENTREF') ],
38-
[ 'LicenseRef-([0-9A-Za-z-+.]+)',
39-
'return ' + quote('LICENSEREF') ] ]
40-
.concat(words.map(function(word) {
41-
return [ word, 'return ' + quote(word) ] }))
42-
.concat(handleLicensesAndExceptions()) },
35+
['$', 'return ' + quote('EOS')],
36+
['\\s+', '/* skip whitespace */'],
37+
['\\+', 'return ' + quote('PLUS')],
38+
['\\(', 'return ' + quote('OPEN')],
39+
['\\)', 'return ' + quote('CLOSE')],
40+
[':', 'return ' + quote('COLON')],
41+
[
42+
'DocumentRef-([0-9A-Za-z-+.]+)',
43+
'return ' + quote('DOCUMENTREF')
44+
],
45+
[
46+
'LicenseRef-([0-9A-Za-z-+.]+)',
47+
'return ' + quote('LICENSEREF')
48+
]
49+
]
50+
.concat(words.map(function (word) {
51+
return [word, 'return ' + quote(word)]
52+
}))
53+
.concat(handleLicensesAndExceptions())
54+
},
4355
operators: [
44-
[ 'left', 'OR' ],
45-
[ 'left', 'AND' ],
46-
[ 'right', 'PLUS', 'WITH' ] ],
56+
['left', 'OR'],
57+
['left', 'AND'],
58+
['right', 'PLUS', 'WITH']
59+
],
4760
tokens: [
4861
'CLOSE',
4962
'COLON',
5063
'EXCEPTION',
5164
'LICENSE',
5265
'LICENSEREF',
5366
'OPEN',
54-
'PLUS' ]
55-
.concat(words)
56-
.join(' '),
67+
'PLUS'
68+
].concat(words).join(' '),
5769
start: 'start',
5870
bnf: {
59-
start: [
60-
[ 'expression EOS', 'return $$ = $1' ] ],
71+
start: [['expression EOS', 'return $$ = $1']],
6172
simpleExpression: [
62-
[ 'LICENSE',
63-
'$$ = { license: yytext }' ],
64-
[ 'LICENSE PLUS',
65-
'$$ = { license: $1, plus: true }' ],
66-
[ 'LICENSEREF',
67-
'$$ = { license: yytext }' ],
68-
[ 'DOCUMENTREF COLON LICENSEREF',
69-
'$$ = { license: yytext }' ] ],
73+
['LICENSE', '$$ = {license: yytext}'],
74+
['LICENSE PLUS', '$$ = {license: $1, plus: true}'],
75+
['LICENSEREF', '$$ = {license: yytext}'],
76+
['DOCUMENTREF COLON LICENSEREF', '$$ = {license: yytext}']
77+
],
7078
expression: [
71-
[ 'simpleExpression',
72-
'$$ = $1' ],
73-
[ 'simpleExpression WITH EXCEPTION',
74-
[ '$$ = { exception: $3 }',
75-
'$$.license = $1.license',
76-
'if ($1.hasOwnProperty(\'plus\')) {',
77-
' $$.plus = $1.plus',
78-
'}' ]
79-
.join('\n') ],
80-
[ 'expression AND expression',
81-
'$$ = { conjunction: \'and\', left: $1, right: $3 }' ],
82-
[ 'expression OR expression',
83-
'$$ = { conjunction: \'or\', left: $1, right: $3 }' ],
84-
[ 'OPEN expression CLOSE',
85-
'$$ = $2' ] ] } }
79+
['simpleExpression', '$$ = $1'],
80+
['simpleExpression WITH EXCEPTION', [
81+
'$$ = {exception: $3}',
82+
'$$.license = $1.license',
83+
'if ($1.hasOwnProperty(\'plus\')) {',
84+
' $$.plus = $1.plus',
85+
'}'].join('\n')],
86+
[
87+
'expression AND expression',
88+
'$$ = {conjunction: \'and\', left: $1, right: $3}'
89+
],
90+
[
91+
'expression OR expression',
92+
'$$ = {conjunction: \'or\', left: $1, right: $3}'
93+
],
94+
['OPEN expression CLOSE', '$$ = $2']
95+
]
96+
}
97+
}
8698

8799
console.log(new Generator(grammar, options).generate())

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var parser = require('./parser.generated.js').parser
1+
var parser = require('./parser.min.js').parser
22

3-
module.exports = function(argument) {
4-
return parser.parse(argument) }
3+
module.exports = function (argument) {
4+
return parser.parse(argument)
5+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"defence-cli": "^1.0.1",
1212
"jison": "^0.4.15",
1313
"replace-require-self": "^1.0.0",
14+
"standard": "^7.1.2",
1415
"uglify-js": "^2.4.24"
1516
},
1617
"keywords": [
@@ -26,7 +27,8 @@
2627
"license": "(MIT AND CC-BY-3.0)",
2728
"repository": "kemitchell/spdx-expression-parse.js",
2829
"scripts": {
29-
"prepublish": "node generate-parser.js | uglifyjs > parser.generated.js",
30+
"lint": "standard",
31+
"prepublish": "node generate-parser.js | uglifyjs > parser.min.js",
3032
"pretest": "npm run prepublish",
3133
"test": "defence -i javascript README.md | replace-require-self | node"
3234
}

0 commit comments

Comments
 (0)