Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 86c7b1b

Browse files
author
Simon Stone
authored
Restrict JavaScript code to ES5 language features (#1008)
1 parent 28d3974 commit 86c7b1b

File tree

9 files changed

+95
-52
lines changed

9 files changed

+95
-52
lines changed

packages/composer-common/lib/codegen/javascriptparser.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,27 @@ class JavaScriptParser {
3333
* Create a JavaScriptParser.
3434
*
3535
* @param {string} fileContents - the text of the JS file to parse
36-
* @param {boolean} includePrivates - if true methods tagged as private are also returned
36+
* @param {boolean} [includePrivates] - if true methods tagged as private are also returned
37+
* @param {number} [ecmaVersion] - the ECMAScript version to use
3738
*/
38-
constructor(fileContents, includePrivates) {
39+
constructor(fileContents, includePrivates, ecmaVersion) {
3940
let comments = [],
4041
tokens = [];
4142

42-
let ast = acorn.parse(fileContents, {
43+
let options = {
4344
// collect ranges for each node
4445
ranges: true,
4546
// collect comments in Esprima's format
4647
onComment: comments,
4748
// collect token ranges
4849
onToken: tokens
49-
});
50+
};
51+
52+
if (ecmaVersion) {
53+
options.ecmaVersion = ecmaVersion;
54+
}
55+
56+
let ast = acorn.parse(fileContents, options);
5057

5158
this.includes = [];
5259
this.classes = [];

packages/composer-common/lib/introspect/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Script {
4545
throw new Error('Empty script contents');
4646
}
4747

48-
const parser = new JavaScriptParser(this.contents);
48+
const parser = new JavaScriptParser(this.contents, false, 5);
4949

5050
const functions = parser.getFunctions();
5151

packages/composer-common/test/codegen/javascriptparser.js

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,41 @@
1414

1515
'use strict';
1616

17-
// const FileWriter = require('../../lib/codegen/filewriter');
18-
// const fs = require('fs');
19-
// const ModelManager = require('../../lib/modelmanager');
20-
// const path = require('path');
21-
22-
const chai = require('chai');
23-
const expect = chai.expect;
24-
2517
const JavascriptParser = require('./../../lib/codegen/javascriptparser');
2618

19+
require('chai').should();
20+
2721
describe('JavascriptParser', () => {
2822

23+
describe('#constructor', () => {
24+
it('should use a default ECMAScript version of 7', () => {
25+
const contents = `
26+
class cls {
27+
28+
}
29+
const theCls = new cls();
30+
let theCls = new cls();
31+
`;
32+
33+
const parser = new JavascriptParser(contents);
34+
parser.getClasses().should.deep.equal([{ name: 'cls', methods: [] }]);
35+
});
36+
37+
it('should accept a non-default ECMAScript version of 5', () => {
38+
const contents = `
39+
class cls {
40+
41+
}
42+
const theCls = new cls();
43+
let theCls = new cls();
44+
`;
45+
46+
(() => {
47+
new JavascriptParser(contents, false, 5);
48+
}).should.throw(/The keyword.*is reserved/);
49+
});
50+
});
51+
2952
describe('#getClasses', () => {
3053
it('should return the classes', () => {
3154
const contents = `
@@ -179,9 +202,9 @@ describe('JavascriptParser', () => {
179202
*/
180203
`;
181204

182-
expect(() => {
205+
(() => {
183206
JavascriptParser.getReturnType(comment);
184-
}).to.throw(Error);
207+
}).should.throw(Error);
185208
});
186209

187210
it('should throw if there is more than one return/returns tag', () => {
@@ -196,9 +219,9 @@ describe('JavascriptParser', () => {
196219
*/
197220
`;
198221

199-
expect(() => {
222+
(() => {
200223
JavascriptParser.getReturnType(comment);
201-
}).to.throw(Error);
224+
}).should.throw(Error);
202225
});
203226

204227
it('should throw if there no return type', () => {
@@ -212,9 +235,9 @@ describe('JavascriptParser', () => {
212235
*/
213236
`;
214237

215-
expect(() => {
238+
(() => {
216239
JavascriptParser.getReturnType(comment);
217-
}).to.throw(Error);
240+
}).should.throw(Error);
218241
});
219242

220243
it('should throw if there no return type name', () => {
@@ -228,9 +251,9 @@ describe('JavascriptParser', () => {
228251
*/
229252
`;
230253

231-
expect(() => {
254+
(() => {
232255
JavascriptParser.getReturnType(comment);
233-
}).to.throw(Error);
256+
}).should.throw(Error);
234257
});
235258
});
236259

@@ -261,9 +284,9 @@ describe('JavascriptParser', () => {
261284
* @exception {Error} - Description
262285
*/
263286
`;
264-
expect(() => {
287+
(() => {
265288
JavascriptParser.getThrows(comment);
266-
}).to.throw(Error);
289+
}).should.throw(Error);
267290
});
268291

269292
it('should throw if a type isn\'t included', () => {
@@ -276,9 +299,9 @@ describe('JavascriptParser', () => {
276299
* @throws - Description
277300
*/
278301
`;
279-
expect(() => {
302+
(() => {
280303
JavascriptParser.getThrows(comment);
281-
}).to.throw(Error);
304+
}).should.throw(Error);
282305
});
283306

284307
it('should throw if there is no type name', () => {
@@ -291,9 +314,9 @@ describe('JavascriptParser', () => {
291314
* @throws {} - Description
292315
*/`;
293316

294-
expect(() => {
317+
(() => {
295318
JavascriptParser.getThrows(comment);
296-
}).to.throw(Error);
319+
}).should.throw(Error);
297320
});
298321
});
299322

@@ -340,9 +363,9 @@ describe('JavascriptParser', () => {
340363
*/
341364
`;
342365

343-
expect(() => {
366+
(() => {
344367
JavascriptParser.getMethodArguments(comment);
345-
}).to.throw(Error);
368+
}).should.throw(Error);
346369
});
347370

348371
it('throws an error if there is no closing curly brace', () => {
@@ -355,9 +378,9 @@ describe('JavascriptParser', () => {
355378
*/
356379
`;
357380

358-
expect(() => {
381+
(() => {
359382
JavascriptParser.getMethodArguments(comment);
360-
}).to.throw(Error);
383+
}).should.throw(Error);
361384
});
362385

363386
it ('doesn\'t throw an error if no description is given', () => {
@@ -370,9 +393,7 @@ describe('JavascriptParser', () => {
370393
*/
371394
`;
372395

373-
expect(() => {
374-
JavascriptParser.getMethodArguments(comment);
375-
}).not.to.throw();
396+
JavascriptParser.getMethodArguments(comment);
376397
});
377398
});
378399

@@ -408,9 +429,9 @@ describe('JavascriptParser', () => {
408429
*/
409430
`;
410431

411-
expect(() => {
432+
(() => {
412433
JavascriptParser.getExample(comment);
413-
}).to.throw(Error);
434+
}).should.throw(Error);
414435
});
415436
});
416437

packages/composer-common/test/data/model/mozart.cto.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
/*eslint-disable no-unused-vars*/
1818
/*eslint-disable no-undef*/
19+
/*eslint-disable no-var*/
1920

2021
/**
2122
* A transaction processor for AnimalMovementDeparture
@@ -33,7 +34,7 @@ function onAnimalMovementDeparture(movementDeparture) {
3334
movementDeparture.animal.movementStatus = 'IN_TRANSIT';
3435

3536
// save the animal
36-
let ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
37+
var ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
3738
ar.update(movementDeparture.animal);
3839

3940
// add the animal to the incoming animals of the
@@ -46,7 +47,7 @@ function onAnimalMovementDeparture(movementDeparture) {
4647
}
4748

4849
// save the business
49-
let br = getAssetRegistry('com.ibm.concerto.mozart.Business');
50+
var br = getAssetRegistry('com.ibm.concerto.mozart.Business');
5051
br.update(movementDeparture.to);
5152
}
5253

@@ -74,7 +75,7 @@ function onAnimalMovementArrival(movementArrival) {
7475
movementArrival.animal.location = movementArrival.arrivalField;
7576

7677
// save the animal
77-
let ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
78+
var ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
7879
ar.update(movementArrival.animal);
7980

8081
// remove the animal from the incoming animals
@@ -89,7 +90,7 @@ function onAnimalMovementArrival(movementArrival) {
8990
});
9091

9192
// save the business
92-
let br = getAssetRegistry('com.ibm.concerto.mozart.Business');
93+
var br = getAssetRegistry('com.ibm.concerto.mozart.Business');
9394
br.update(movementArrival.to);
9495
}
9596

packages/composer-common/test/data/zip/test-archive-dotfolders/lib/mozart.cto.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
/*eslint-disable no-unused-vars*/
1818
/*eslint-disable no-undef*/
19+
/*eslint-disable no-var*/
1920

2021
/**
2122
* A transaction processor for AnimalMovementDeparture
@@ -31,7 +32,7 @@ function onAnimalMovementDeparture(movementDeparture) {
3132
movementDeparture.animal.movementStatus = 'IN_TRANSIT';
3233

3334
// save the animal
34-
let ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
35+
var ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
3536
ar.update(movementDeparture.animal);
3637

3738
// add the animal to the incoming animals of the
@@ -44,7 +45,7 @@ function onAnimalMovementDeparture(movementDeparture) {
4445
}
4546

4647
// save the business
47-
let br = getAssetRegistry('com.ibm.concerto.mozart.Business');
48+
var br = getAssetRegistry('com.ibm.concerto.mozart.Business');
4849
br.update(movementDeparture.to);
4950
}
5051

@@ -70,7 +71,7 @@ function onAnimalMovementArrival(movementArrival) {
7071
movementArrival.animal.location = movementArrival.arrivalField;
7172

7273
// save the animal
73-
let ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
74+
var ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
7475
ar.update(movementArrival.animal);
7576

7677
// remove the animal from the incoming animals
@@ -85,7 +86,7 @@ function onAnimalMovementArrival(movementArrival) {
8586
});
8687

8788
// save the business
88-
let br = getAssetRegistry('com.ibm.concerto.mozart.Business');
89+
var br = getAssetRegistry('com.ibm.concerto.mozart.Business');
8990
br.update(movementArrival.to);
9091
}
9192

134 Bytes
Binary file not shown.

packages/composer-common/test/data/zip/test-archive/lib/mozart.cto.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
/*eslint-disable no-unused-vars*/
1818
/*eslint-disable no-undef*/
19+
/*eslint-disable no-var*/
1920

2021
/**
2122
* A transaction processor for AnimalMovementDeparture
@@ -31,7 +32,7 @@ function onAnimalMovementDeparture(movementDeparture) {
3132
movementDeparture.animal.movementStatus = 'IN_TRANSIT';
3233

3334
// save the animal
34-
let ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
35+
var ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
3536
ar.update(movementDeparture.animal);
3637

3738
// add the animal to the incoming animals of the
@@ -44,7 +45,7 @@ function onAnimalMovementDeparture(movementDeparture) {
4445
}
4546

4647
// save the business
47-
let br = getAssetRegistry('com.ibm.concerto.mozart.Business');
48+
var br = getAssetRegistry('com.ibm.concerto.mozart.Business');
4849
br.update(movementDeparture.to);
4950
}
5051

@@ -70,7 +71,7 @@ function onAnimalMovementArrival(movementArrival) {
7071
movementArrival.animal.location = movementArrival.arrivalField;
7172

7273
// save the animal
73-
let ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
74+
var ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
7475
ar.update(movementArrival.animal);
7576

7677
// remove the animal from the incoming animals
@@ -85,7 +86,7 @@ function onAnimalMovementArrival(movementArrival) {
8586
});
8687

8788
// save the business
88-
let br = getAssetRegistry('com.ibm.concerto.mozart.Business');
89+
var br = getAssetRegistry('com.ibm.concerto.mozart.Business');
8990
br.update(movementArrival.to);
9091
}
9192

packages/composer-common/test/data/zip/test-npm-archive/lib/mozart.cto.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
/*eslint-disable no-unused-vars*/
1818
/*eslint-disable no-undef*/
19+
/*eslint-disable no-var*/
1920

2021
/**
2122
* A transaction processor for AnimalMovementDeparture
@@ -31,7 +32,7 @@ function onAnimalMovementDeparture(movementDeparture) {
3132
movementDeparture.animal.movementStatus = 'IN_TRANSIT';
3233

3334
// save the animal
34-
let ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
35+
var ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
3536
ar.update(movementDeparture.animal);
3637

3738
// add the animal to the incoming animals of the
@@ -44,7 +45,7 @@ function onAnimalMovementDeparture(movementDeparture) {
4445
}
4546

4647
// save the business
47-
let br = getAssetRegistry('com.ibm.concerto.mozart.Business');
48+
var br = getAssetRegistry('com.ibm.concerto.mozart.Business');
4849
br.update(movementDeparture.to);
4950
}
5051

@@ -70,7 +71,7 @@ function onAnimalMovementArrival(movementArrival) {
7071
movementArrival.animal.location = movementArrival.arrivalField;
7172

7273
// save the animal
73-
let ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
74+
var ar = getAssetRegistry('com.ibm.concerto.mozart.Animal');
7475
ar.update(movementArrival.animal);
7576

7677
// remove the animal from the incoming animals
@@ -85,7 +86,7 @@ function onAnimalMovementArrival(movementArrival) {
8586
});
8687

8788
// save the business
88-
let br = getAssetRegistry('com.ibm.concerto.mozart.Business');
89+
var br = getAssetRegistry('com.ibm.concerto.mozart.Business');
8990
br.update(movementArrival.to);
9091
}
9192

0 commit comments

Comments
 (0)