Skip to content

Commit 9d641fb

Browse files
jjuddevmar
authored andcommitted
Replace all slashes with periods in the module name instead of just the first (#154)
* Replace all slashes with periods in the module name instead of just the first * Standardize the pathToModule function across tests and the CLI
1 parent e51fa38 commit 9d641fb

File tree

4 files changed

+41
-48
lines changed

4 files changed

+41
-48
lines changed

src/cli_support.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as path from 'path';
2+
3+
// Postprocess generated JS.
4+
export function pathToModuleName(context: string, fileName: string): string {
5+
if (fileName[0] === '.') {
6+
fileName = path.join(path.dirname(context), fileName);
7+
}
8+
return fileName.replace(/\.js$/, '').replace(/\//g, '.');
9+
}

src/main.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as minimist from 'minimist';
66
import * as path from 'path';
77
import * as ts from 'typescript';
88

9+
import * as cli_support from './cli_support';
910
import * as tsickle from './tsickle';
1011

1112
/**
@@ -208,14 +209,9 @@ function toClosureJS(options: ts.CompilerOptions, fileNames: string[]):
208209
return {errors: diagnostics};
209210
}
210211

211-
// Postprocess generated JS.
212-
function pathToModuleName(context: string, fileName: string): string {
213-
// TODO(evanm): something more sophisticated here?
214-
return fileName.replace('/', '.');
215-
}
216212
for (let fileName of Object.keys(jsFiles)) {
217-
let {output} =
218-
tsickle.convertCommonJsToGoogModule(fileName, jsFiles[fileName], pathToModuleName);
213+
let {output} = tsickle.convertCommonJsToGoogModule(
214+
fileName, jsFiles[fileName], cli_support.pathToModuleName);
219215
jsFiles[fileName] = output;
220216
}
221217

test/test_support.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import * as glob from 'glob';
33
import * as path from 'path';
44
import * as ts from 'typescript';
55

6+
import * as cli_support from '../src/cli_support';
67
import * as tsickle from '../src/tsickle';
78

8-
99
/** The TypeScript compiler options used by the test suite. */
1010
const compilerOptions: ts.CompilerOptions = {
1111
target: ts.ScriptTarget.ES6,
@@ -56,19 +56,12 @@ export function emit(program: ts.Program): {[fileName: string]: string} {
5656
let transformed: {[fileName: string]: string} = {};
5757
let emitRes = program.emit(undefined, (fileName: string, data: string) => {
5858
transformed[fileName] =
59-
tsickle.convertCommonJsToGoogModule(fileName, data, pathToModuleName).output;
59+
tsickle.convertCommonJsToGoogModule(fileName, data, cli_support.pathToModuleName).output;
6060
});
6161
if (emitRes.diagnostics.length) {
6262
throw new Error(tsickle.formatDiagnostics(emitRes.diagnostics));
6363
}
6464
return transformed;
65-
66-
function pathToModuleName(context: string, fileName: string): string {
67-
if (fileName[0] === '.') {
68-
fileName = path.join(path.dirname(context), fileName);
69-
}
70-
return fileName.replace(/\.js$/, '').replace(/\//g, '.');
71-
}
7265
}
7366

7467
export class GoldenFileTest {

test/tsickle_test.ts

+27-32
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as fs from 'fs';
33
import * as path from 'path';
44
import * as ts from 'typescript';
55

6+
import * as cli_support from '../src/cli_support';
67
import * as tsickle from '../src/tsickle';
78

89
import * as test_support from './test_support';
@@ -164,102 +165,96 @@ describe('getJSDocAnnotation', () => {
164165
});
165166

166167
describe('convertCommonJsToGoogModule', () => {
167-
function pathToModuleName(context: string, fileName: string) {
168-
if (fileName[0] === '.') {
169-
fileName = path.join(path.dirname(context), fileName);
170-
}
171-
return fileName.replace(/\//g, '$').replace(/_/g, '__');
172-
}
173-
174168
function expectCommonJs(fileName: string, content: string) {
175-
fileName = fileName.substring(0, fileName.lastIndexOf('.'));
176-
return expect(tsickle.convertCommonJsToGoogModule(fileName, content, pathToModuleName).output);
169+
return expect(
170+
tsickle.convertCommonJsToGoogModule(fileName, content, cli_support.pathToModuleName)
171+
.output);
177172
}
178173

179174
it('adds a goog.module call', () => {
180175
// NB: no line break added below.
181176
expectCommonJs('a.js', `console.log('hello');`)
182-
.to.equal(`goog.module('a');var module = module || {id: 'a'};console.log('hello');`);
177+
.to.equal(`goog.module('a');var module = module || {id: 'a.js'};console.log('hello');`);
183178
});
184179

185180
it('adds a goog.module call to empty files', () => {
186-
expectCommonJs('a.js', ``).to.equal(`goog.module('a');var module = module || {id: 'a'};`);
181+
expectCommonJs('a.js', ``).to.equal(`goog.module('a');var module = module || {id: 'a.js'};`);
187182
});
188183

189184
it('adds a goog.module call to empty-looking files', () => {
190185
expectCommonJs('a.js', `// empty`)
191-
.to.equal(`goog.module('a');var module = module || {id: 'a'};// empty`);
186+
.to.equal(`goog.module('a');var module = module || {id: 'a.js'};// empty`);
192187
});
193188

194189
it('strips use strict directives', () => {
195190
// NB: no line break added below.
196191
expectCommonJs('a.js', `"use strict";
197192
console.log('hello');`)
198-
.to.equal(`goog.module('a');var module = module || {id: 'a'};
193+
.to.equal(`goog.module('a');var module = module || {id: 'a.js'};
199194
console.log('hello');`);
200195
});
201196

202197
it('converts require calls', () => {
203198
expectCommonJs('a.js', `var r = require('req/mod');`)
204199
.to.equal(
205-
`goog.module('a');var module = module || {id: 'a'};var r = goog.require('req$mod');`);
200+
`goog.module('a');var module = module || {id: 'a.js'};var r = goog.require('req.mod');`);
206201
});
207202

208203
it('converts require calls without assignments on first line', () => {
209204
expectCommonJs('a.js', `require('req/mod');`)
210205
.to.equal(
211-
`goog.module('a');var module = module || {id: 'a'};var unused_0_ = goog.require('req$mod');`);
206+
`goog.module('a');var module = module || {id: 'a.js'};var unused_0_ = goog.require('req.mod');`);
212207
});
213208

214209
it('converts require calls without assignments on a new line', () => {
215210
expectCommonJs('a.js', `
216211
require('req/mod');
217212
require('other');`)
218-
.to.equal(`goog.module('a');var module = module || {id: 'a'};
219-
var unused_0_ = goog.require('req$mod');
213+
.to.equal(`goog.module('a');var module = module || {id: 'a.js'};
214+
var unused_0_ = goog.require('req.mod');
220215
var unused_1_ = goog.require('other');`);
221216
});
222217

223218
it('converts require calls without assignments after comments', () => {
224219
expectCommonJs('a.js', `
225220
// Comment
226221
require('req/mod');`)
227-
.to.equal(`goog.module('a');var module = module || {id: 'a'};
222+
.to.equal(`goog.module('a');var module = module || {id: 'a.js'};
228223
// Comment
229-
var unused_0_ = goog.require('req$mod');`);
224+
var unused_0_ = goog.require('req.mod');`);
230225
});
231226

232227
it('converts const require calls', () => {
233228
expectCommonJs('a.js', `const r = require('req/mod');`)
234229
.to.equal(
235-
`goog.module('a');var module = module || {id: 'a'};var r = goog.require('req$mod');`);
230+
`goog.module('a');var module = module || {id: 'a.js'};var r = goog.require('req.mod');`);
236231
});
237232

238233
it('converts export * statements', () => {
239234
expectCommonJs('a.js', `__export(require('req/mod'));`)
240235
.to.equal(
241-
`goog.module('a');var module = module || {id: 'a'};__export(goog.require('req$mod'));`);
236+
`goog.module('a');var module = module || {id: 'a.js'};__export(goog.require('req.mod'));`);
242237
});
243238

244239
it('resolves relative module URIs', () => {
245240
// See below for more fine-grained unit tests.
246241
expectCommonJs('a/b.js', `var r = require('./req/mod');`)
247242
.to.equal(
248-
`goog.module('a$b');var module = module || {id: 'a/b'};var r = goog.require('a$req$mod');`);
243+
`goog.module('a.b');var module = module || {id: 'a/b.js'};var r = goog.require('a.req.mod');`);
249244
});
250245

251246
it('avoids mangling module names in goog: imports', () => {
252247
expectCommonJs('a/b.js', `
253248
var goog_use_Foo_1 = require('goog:foo_bar.baz');`)
254-
.to.equal(`goog.module('a$b');var module = module || {id: 'a/b'};
249+
.to.equal(`goog.module('a.b');var module = module || {id: 'a/b.js'};
255250
var goog_use_Foo_1 = goog.require('foo_bar.baz');`);
256251
});
257252

258253
it('resolves default goog: module imports', () => {
259254
expectCommonJs('a/b.js', `
260255
var goog_use_Foo_1 = require('goog:use.Foo');
261256
console.log(goog_use_Foo_1.default);`)
262-
.to.equal(`goog.module('a$b');var module = module || {id: 'a/b'};
257+
.to.equal(`goog.module('a.b');var module = module || {id: 'a/b.js'};
263258
var goog_use_Foo_1 = goog.require('use.Foo');
264259
console.log(goog_use_Foo_1 );`);
265260
// NB: the whitespace above matches the .default part, so that
@@ -271,7 +266,7 @@ console.log(goog_use_Foo_1 );`);
271266
expectCommonJs('a/b.js', `
272267
console.log(this.default);
273268
console.log(foo.bar.default);`)
274-
.to.equal(`goog.module('a$b');var module = module || {id: 'a/b'};
269+
.to.equal(`goog.module('a.b');var module = module || {id: 'a/b.js'};
275270
console.log(this.default);
276271
console.log(foo.bar.default);`);
277272
});
@@ -282,7 +277,7 @@ console.log(foo.bar.default);`);
282277
*/
283278
"use strict";
284279
var foo = bar;
285-
`).to.equal(`goog.module('a$b');var module = module || {id: 'a/b'};/**
280+
`).to.equal(`goog.module('a.b');var module = module || {id: 'a/b.js'};/**
286281
* docstring here
287282
*/
288283
@@ -294,7 +289,7 @@ var foo = bar;
294289
expectCommonJs('a/b.js', `var foo_1 = require('goog:foo');
295290
var foo_2 = require('goog:foo');
296291
foo_1.A, foo_2.B, foo_2.default, foo_3.default;
297-
`).to.equal(`goog.module('a$b');var module = module || {id: 'a/b'};var foo_1 = goog.require('foo');
292+
`).to.equal(`goog.module('a.b');var module = module || {id: 'a/b.js'};var foo_1 = goog.require('foo');
298293
var foo_2 = foo_1;
299294
foo_1.A, foo_2.B, foo_2 , foo_3.default;
300295
`);
@@ -309,14 +304,14 @@ var es6RelativeRequire = require('./relative');
309304
var es6NonRelativeRequire = require('non/relative');
310305
__export(require('./export-star');
311306
`,
312-
pathToModuleName);
307+
cli_support.pathToModuleName);
313308

314309
return expect(referencedModules).to.deep.equal([
315-
'foo$bare-require',
310+
'foo.bare-require',
316311
'foo.bar',
317-
'a$relative',
318-
'non$relative',
319-
'a$export-star',
312+
'a.relative',
313+
'non.relative',
314+
'a.export-star',
320315
]);
321316
});
322317
});

0 commit comments

Comments
 (0)