Skip to content

Commit 805b3e5

Browse files
authored
Merge pull request #78 from NaridaL/master
Changed `declare module "electron"` to export = Electron
2 parents 54fcd2a + fc8e896 commit 805b3e5

File tree

5 files changed

+40
-15
lines changed

5 files changed

+40
-15
lines changed

base/base_footer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
declare module 'electron' {
2-
const electron: Electron.AllElectron;
3-
export = electron;
2+
export = Electron;
43
}
54

65
interface NodeRequireFunction {
7-
(moduleName: 'electron'): Electron.AllElectron;
6+
(moduleName: 'electron'): typeof Electron;
87
}
98

109
interface File {

lib/master-interfaces.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module.exports = (API, addToOutput) => {
88
const CommonInterface = ['interface CommonInterface {']
99
const MainInterface = ['interface MainInterface extends CommonInterface {']
1010
const RendererInterface = ['interface RendererInterface extends CommonInterface {']
11-
const ElectronMainAndRendererInterface = ['interface AllElectron {']
11+
const ElectronMainAndRendererInterface = ['interface AllElectron extends MainInterface, RendererInterface {}']
12+
const constDeclarations = []
1213
const EMRI = {}
1314

1415
const classify = (moduleName) => {
@@ -28,11 +29,20 @@ module.exports = (API, addToOutput) => {
2829
if (module.name === 'process') return
2930
let TargetInterface
3031
const isClass = module.type === 'Class' || API.some((tModule, tIndex) => index !== tIndex && tModule.name.toLowerCase() === module.name.toLowerCase())
31-
const moduleString = ` ${classify(module.name)}: ${isClass ? 'typeof ' : ''}Electron.${_.upperFirst(module.name)}`
32+
const moduleString = ` ${classify(module.name)}: ${isClass ? 'typeof ' : ''}${_.upperFirst(module.name)}`
3233
if (!module.process) {
3334
// We must be a structure or something
3435
return
3536
}
37+
if (!isClass || module.name !== classify(module.name)) {
38+
if (isClass) {
39+
constDeclarations.push(
40+
`type ${classify(module.name)} = ${_.upperFirst(module.name)};`,
41+
`const ${classify(module.name)}: typeof ${_.upperFirst(module.name)};`)
42+
} else {
43+
constDeclarations.push(`const ${classify(module.name)}: ${_.upperFirst(module.name)};`)
44+
}
45+
}
3646
if (module.process.main && module.process.renderer) {
3747
TargetInterface = CommonInterface
3848
} else if (module.process.main) {
@@ -46,7 +56,6 @@ module.exports = (API, addToOutput) => {
4656
if (TargetInterface) {
4757
debug(classify(module.name).toLowerCase(), EMRI[classify(module.name).toLowerCase()])
4858
if (!EMRI[classify(module.name).toLowerCase()]) {
49-
ElectronMainAndRendererInterface.push(moduleString)
5059
TargetInterface.push(moduleString)
5160
}
5261
EMRI[classify(module.name).toLowerCase()] = true
@@ -56,11 +65,11 @@ module.exports = (API, addToOutput) => {
5665
CommonInterface.push('}')
5766
MainInterface.push('}')
5867
RendererInterface.push('}')
59-
ElectronMainAndRendererInterface.push('}')
6068

6169
addToOutput([''])
6270
addToOutput(CommonInterface, ';')
6371
addToOutput(MainInterface, ';')
6472
addToOutput(RendererInterface, ';')
6573
addToOutput(ElectronMainAndRendererInterface, ';')
74+
addToOutput(constDeclarations)
6675
}

test-smoke/electron/test/main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
session,
2424
systemPreferences,
2525
webContents,
26+
Event,
2627
} from "electron";
2728

2829
import * as path from "path";
@@ -33,6 +34,7 @@ import * as path from "path";
3334
// Keep a global reference of the window object, if you don't, the window will
3435
// be closed automatically when the javascript object is GCed.
3536
let mainWindow: Electron.BrowserWindow = null;
37+
const mainWindow2: BrowserWindow = null;
3638

3739
// Quit when all windows are closed.
3840
app.on("window-all-closed", () => {
@@ -524,6 +526,11 @@ ipcMain.on("synchronous-message", (event: Electron.Event, arg: any) => {
524526
event.returnValue = "pong";
525527
});
526528

529+
ipcMain.on("synchronous-message", (event: Event, arg: any) => {
530+
console.log("event isn't namespaced and refers to the correct type.");
531+
event.returnValue = "pong";
532+
});
533+
527534
const winWindows = new BrowserWindow({
528535
width: 800,
529536
height: 600,

test/output_spec.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@ const path = require('path')
88
const OUTPUT_PATH = path.resolve(__dirname, '..', 'electron.d.ts')
99
let output
1010

11+
function getDefinitionsForInterface (interfaceName) {
12+
const interface_ = output.match(interfaceName + '[^{]+{([\\s\\S]+?)}')
13+
expect(interface_).to.be.an('array')
14+
return interface_[1].split(';').map(l => l.trim())
15+
}
16+
1117
describe('Definition File', function () {
1218
this.timeout(20000)
1319

1420
before((done) => {
1521
spawn('node', ['cli.js', '-o=electron.d.ts'], {
1622
cwd: path.resolve(__dirname, '..')
1723
}).on('exit', () => done())
24+
done()
1825
})
1926

2027
it('should output a electron.d.ts file', () => {
@@ -23,18 +30,20 @@ describe('Definition File', function () {
2330
})
2431

2532
it('should correctly output all exported Electron modules', () => {
26-
const AllElectron = output.match(/AllElectron {([\s\S]+?)}/)
27-
expect(AllElectron).to.be.an('array')
28-
const AllElectronModules = AllElectron[1].split(';').map(l => l.trim())
33+
const AllElectronModules = getDefinitionsForInterface('MainInterface').concat(
34+
getDefinitionsForInterface('CommonInterface'),
35+
getDefinitionsForInterface('RendererInterface')
36+
)
2937
const knownElectronModules = ['clipboard', 'app', 'autoUpdater', 'dialog', 'ipcMain', 'Menu', 'MenuItem', 'webContents', 'BrowserWindow']
3038
knownElectronModules.forEach(knownModule => expect(AllElectronModules.some(tModule => tModule.indexOf(knownModule) === 0)).to.equal(true))
3139
})
3240

3341
it('should not output classes that are not exported Electron modules', () => {
34-
const AllElectron = output.match(/AllElectron {([\s\S]+?)}/)
35-
expect(AllElectron).to.be.an('array')
36-
const AllElectronModules = AllElectron[1].split(';').map(l => l.trim())
42+
const AllElectronModules = getDefinitionsForInterface('MainInterface').concat(
43+
getDefinitionsForInterface('CommonInterface'),
44+
getDefinitionsForInterface('RendererInterface')
45+
)
3746
const unKnownElectronModules = ['Clipboard', 'CrashReport', 'WebContents', 'menu', 'Session']
38-
unKnownElectronModules.forEach(knownModule => expect(AllElectronModules.some(tModule => tModule.indexOf(knownModule) === 0)).to.equal(false))
47+
unKnownElectronModules.forEach(knownModule => expect(AllElectronModules.some(tModule => tModule.indexOf(knownModule) === 0)).to.equal(false, knownModule))
3948
})
4049
})

tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"no-namespace": false,
1414
"quotemark": false,
1515
"unified-signatures": false,
16-
"variable-name": false
16+
"variable-name": false,
17+
"no-shadowed-variable": false
1718
}
1819
}

0 commit comments

Comments
 (0)