Skip to content

Commit

Permalink
feat(instrumenter): add support for .mjs and .cjs file formats (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Omhet committed Aug 18, 2020
1 parent 6041699 commit 5ba4c5c
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/instrumenter/src/parsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ function getFormat(fileName: string, override: AstFormat | undefined): AstFormat
const ext = path.extname(fileName).toLowerCase();
switch (ext) {
case '.js':
case '.mjs':
case '.cjs':
return AstFormat.JS;
case '.ts':
case '.tsx':
Expand Down
286 changes: 286 additions & 0 deletions packages/instrumenter/test/integration/parsers.it.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8506,6 +8506,292 @@ export class AppComponent {
}
`;
exports[`parsers integration should allow to parse a cjs file 1`] = `
Object {
"format": "js",
"originFileName": "app.cjs",
"rawContent": "module.exports = class App {}",
"root": Node {
"comments": Array [],
"end": 29,
"errors": Array [],
"loc": SourceLocation {
"end": Position {
"column": 29,
"line": 1,
},
"start": Position {
"column": 0,
"line": 1,
},
},
"program": Node {
"body": Array [
Node {
"end": 29,
"expression": Node {
"end": 29,
"left": Node {
"computed": false,
"end": 14,
"loc": SourceLocation {
"end": Position {
"column": 14,
"line": 1,
},
"start": Position {
"column": 0,
"line": 1,
},
},
"object": Node {
"end": 6,
"loc": SourceLocation {
"end": Position {
"column": 6,
"line": 1,
},
"identifierName": "module",
"start": Position {
"column": 0,
"line": 1,
},
},
"name": "module",
"start": 0,
"type": "Identifier",
},
"property": Node {
"end": 14,
"loc": SourceLocation {
"end": Position {
"column": 14,
"line": 1,
},
"identifierName": "exports",
"start": Position {
"column": 7,
"line": 1,
},
},
"name": "exports",
"start": 7,
"type": "Identifier",
},
"start": 0,
"type": "MemberExpression",
},
"loc": SourceLocation {
"end": Position {
"column": 29,
"line": 1,
},
"start": Position {
"column": 0,
"line": 1,
},
},
"operator": "=",
"right": Node {
"body": Node {
"body": Array [],
"end": 29,
"loc": SourceLocation {
"end": Position {
"column": 29,
"line": 1,
},
"start": Position {
"column": 27,
"line": 1,
},
},
"start": 27,
"type": "ClassBody",
},
"end": 29,
"id": Node {
"end": 26,
"loc": SourceLocation {
"end": Position {
"column": 26,
"line": 1,
},
"identifierName": "App",
"start": Position {
"column": 23,
"line": 1,
},
},
"name": "App",
"start": 23,
"type": "Identifier",
},
"loc": SourceLocation {
"end": Position {
"column": 29,
"line": 1,
},
"start": Position {
"column": 17,
"line": 1,
},
},
"start": 17,
"superClass": null,
"type": "ClassExpression",
},
"start": 0,
"type": "AssignmentExpression",
},
"loc": SourceLocation {
"end": Position {
"column": 29,
"line": 1,
},
"start": Position {
"column": 0,
"line": 1,
},
},
"start": 0,
"type": "ExpressionStatement",
},
],
"directives": Array [],
"end": 29,
"interpreter": null,
"loc": SourceLocation {
"end": Position {
"column": 29,
"line": 1,
},
"start": Position {
"column": 0,
"line": 1,
},
},
"sourceType": "module",
"start": 0,
"type": "Program",
},
"start": 0,
"type": "File",
},
}
`;
exports[`parsers integration should allow to parse a mjs file 1`] = `
Object {
"format": "js",
"originFileName": "app.mjs",
"rawContent": "export class App {}",
"root": Node {
"comments": Array [],
"end": 19,
"errors": Array [],
"loc": SourceLocation {
"end": Position {
"column": 19,
"line": 1,
},
"start": Position {
"column": 0,
"line": 1,
},
},
"program": Node {
"body": Array [
Node {
"declaration": Node {
"body": Node {
"body": Array [],
"end": 19,
"loc": SourceLocation {
"end": Position {
"column": 19,
"line": 1,
},
"start": Position {
"column": 17,
"line": 1,
},
},
"start": 17,
"type": "ClassBody",
},
"end": 19,
"id": Node {
"end": 16,
"loc": SourceLocation {
"end": Position {
"column": 16,
"line": 1,
},
"identifierName": "App",
"start": Position {
"column": 13,
"line": 1,
},
},
"name": "App",
"start": 13,
"type": "Identifier",
},
"loc": SourceLocation {
"end": Position {
"column": 19,
"line": 1,
},
"start": Position {
"column": 7,
"line": 1,
},
},
"start": 7,
"superClass": null,
"type": "ClassDeclaration",
},
"end": 19,
"loc": SourceLocation {
"end": Position {
"column": 19,
"line": 1,
},
"start": Position {
"column": 0,
"line": 1,
},
},
"source": null,
"specifiers": Array [],
"start": 0,
"type": "ExportNamedDeclaration",
},
],
"directives": Array [],
"end": 19,
"interpreter": null,
"loc": SourceLocation {
"end": Position {
"column": 19,
"line": 1,
},
"start": Position {
"column": 0,
"line": 1,
},
},
"sourceType": "module",
"start": 0,
"type": "Program",
},
"start": 0,
"type": "File",
},
}
`;
exports[`parsers integration should allow to parse a react file with custom babelrc file 1`] = `
Object {
"format": "js",
Expand Down
10 changes: 10 additions & 0 deletions packages/instrumenter/test/integration/parsers.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ describe('parsers integration', () => {
expect(actual).to.matchSnapshot();
});

it('should allow to parse a mjs file', async () => {
const actual = await actAssertJS('app.mjs');
expect(actual).to.matchSnapshot();
});

it('should allow to parse a cjs file', async () => {
const actual = await actAssertJS('app.cjs');
expect(actual).to.matchSnapshot();
});

async function act(testResourceFileName: string, options: ParserOptions) {
const fileName = resolveTestResource(testResourceFileName);
const input = await fs.readFile(fileName, 'utf8');
Expand Down
1 change: 1 addition & 0 deletions packages/instrumenter/testResources/parser/app.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = class App {}
1 change: 1 addition & 0 deletions packages/instrumenter/testResources/parser/app.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class App {}

0 comments on commit 5ba4c5c

Please sign in to comment.