Skip to content

Commit 11e587b

Browse files
authored
Merge pull request #16 from electron/webview-linting-fixes
Webview linting fixes
2 parents 8ea7d01 + 3567c13 commit 11e587b

File tree

12 files changed

+186
-1237
lines changed

12 files changed

+186
-1237
lines changed

base/base_footer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ declare module 'electron' {
33
export = electron;
44
}
55

6-
type NodeRequireFunction = (moduleName: 'electron') => Electron.AllElectron;
6+
interface NodeRequireFunction {
7+
(moduleName: 'electron'): Electron.AllElectron;
8+
}
79

810
interface File {
911
/**
@@ -16,3 +18,7 @@ declare module 'original-fs' {
1618
import * as fs from 'fs';
1719
export = fs;
1820
}
21+
22+
interface Document {
23+
createElement(tagName: 'webview'): Electron.WebviewTag;
24+
}

base/base_inner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
ctrlkey?: boolean;
2626
metaKey?: boolean;
2727
shiftKey?: boolean;
28-
}
28+
}

index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,27 @@ const wrapWithHeaderAndFooter = (outputLines, electronVersion) => {
2020

2121
newOutputLines.push('declare namespace Electron {')
2222
utils.extendArray(newOutputLines, fs.readFileSync(path.resolve(__dirname, 'base/base_inner.ts'), 'utf8').replace('<<VERSION>>', electronVersion).split(/\r?\n/))
23-
outputLines.forEach((l) => newOutputLines.push(`${_.trimEnd(` ${l}`)}`))
23+
24+
outputLines.slice(1).forEach((l) => newOutputLines.push(`${_.trimEnd(` ${l}`)}`))
2425
utils.extendArray(newOutputLines, ['}', ''])
2526

2627
utils.extendArray(newOutputLines, fs.readFileSync(path.resolve(__dirname, 'base/base_footer.ts'), 'utf8').replace('<<VERSION>>', electronVersion).split(/\r?\n/))
2728
return newOutputLines
2829
}
2930

31+
const appendNodeJSOverride = (outputLines) => {
32+
utils.extendArray(outputLines, ['', 'declare namespace NodeJS {'])
33+
34+
const processAPI = moduleDeclaration.getModuleDeclarations().Process
35+
processAPI.push('}')
36+
utils.extendArray(outputLines, (processAPI.map((l, index) => l.length ? ((index === 0 || index === processAPI.length - 1) ? ` ${l}` : ` ${l}`) : '')))
37+
utils.extendArray(outputLines, [' interface ProcessVersions {', ' electron: string;', ' chrome: string;', ' }'])
38+
39+
utils.extendArray(outputLines, ['}'])
40+
41+
return outputLines
42+
}
43+
3044
module.exports = (API) => {
3145
const outputLines = []
3246

@@ -46,12 +60,15 @@ module.exports = (API) => {
4660

4761
// fetch everything that's been made and pop it into the actual API
4862
Object.keys(moduleDeclaration.getModuleDeclarations()).forEach((moduleKey) => {
63+
if (moduleKey === 'Process') return
4964
const moduleAPI = moduleDeclaration.getModuleDeclarations()[moduleKey]
5065
moduleAPI.push('}')
5166
addToOutput(moduleAPI.map((l, index) => (index === 0 || index === moduleAPI.length - 1) ? l : ` ${l}`))
5267
})
5368

5469
paramInterfaces.flushParamInterfaces(API, addToOutput)
5570

56-
return wrapWithHeaderAndFooter(outputLines, API[0].version)
71+
const electronOutput = wrapWithHeaderAndFooter(outputLines, API[0].version)
72+
73+
return appendNodeJSOverride(electronOutput)
5774
}

lib/dynamic-param-interfaces.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ const flushParamInterfaces = (API, addToOutput) => {
7272
delete paramInterfacesToDeclare[paramKey]
7373
return
7474
}
75-
if (declared[paramKey]) throw new Error('Ruh roh, "' + paramKey + '" is already declared')
76-
declared[paramKey] = true
75+
if (declared[paramKey]) {
76+
if (!_.isEqual(paramInterfacesToDeclare[paramKey], declared[paramKey])) {
77+
throw new Error('Ruh roh, "' + paramKey + '" is already declared')
78+
}
79+
delete paramInterfacesToDeclare[paramKey]
80+
return
81+
}
82+
declared[paramKey] = paramInterfacesToDeclare[paramKey]
7783
const param = paramInterfacesToDeclare[paramKey]
7884
const paramAPI = []
7985
paramAPI.push(`interface ${_.upperFirst(param.tName)} {`)
@@ -85,7 +91,7 @@ const flushParamInterfaces = (API, addToOutput) => {
8591
}
8692

8793
if (!Array.isArray(paramProperty.type) && paramProperty.type.toLowerCase() === 'object') {
88-
let argType = _.upperFirst(_.camelCase(paramProperty.name))
94+
let argType = _.upperFirst(_.camelCase(paramProperty.__type || paramProperty.name))
8995
if (API.some(a => a.name === argType)) {
9096
paramProperty.type = argType
9197
debug(`Auto-correcting type from Object --> ${argType} in Interface: ${_.upperFirst(param.tName)} --- This should be fixed in the docs`.red)
@@ -100,7 +106,7 @@ const flushParamInterfaces = (API, addToOutput) => {
100106
if (!Array.isArray(paramProperty.type) && paramProperty.type.toLowerCase() === 'function') {
101107
paramAPI.push(`${paramProperty.name}${utils.isOptional(paramProperty) ? '?' : ''}: ${utils.genMethodString(module.exports, param, paramProperty, paramProperty.parameters, paramProperty.returns, true)};`)
102108
} else {
103-
paramAPI.push(`${paramProperty.name}${utils.isOptional(paramProperty) ? '?' : ''}: ${utils.typify(paramProperty)};`)
109+
paramAPI.push(`${paramProperty.name}${utils.isOptional(paramProperty) ? '?' : ''}: ${utils.typify(paramProperty.__type || paramProperty)};`)
104110
}
105111
})
106112
paramAPI.push('}')

lib/master-interfaces.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module.exports = (API, addToOutput) => {
2525
}
2626

2727
API.forEach((module, index) => {
28+
if (module.name === 'process') return
2829
let TargetInterface
2930
const isClass = module.type === 'Class' || API.some((tModule, tIndex) => index !== tIndex && tModule.name.toLowerCase() === module.name.toLowerCase())
3031
const moduleString = ` ${classify(module.name)}: ${isClass ? 'typeof ' : ''}Electron.${_.upperFirst(module.name)}`

lib/module-declaration.js

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const generateModuleDeclaration = (module, index, API) => {
1414

1515
// Interface Declaration
1616
if (newModule) {
17-
if (module.type !== 'Structure') {
17+
if (module.type === 'Element') {
18+
moduleAPI.push(`interface ${_.upperFirst(module.name)} extends HTMLElement {`)
19+
moduleAPI.push('', `// Docs: ${module.websiteUrl}`, '')
20+
} else if (module.type !== 'Structure') {
1821
if (utils.isEmitter(module)) {
1922
moduleAPI.push(`${isClass ? 'class' : 'interface'} ${_.upperFirst(module.name)} extends ${module.name === 'remote' ? 'MainInterface' : 'EventEmitter'} {`)
2023
moduleAPI.push('', `// Docs: ${module.websiteUrl}`, '')
@@ -37,10 +40,10 @@ const generateModuleDeclaration = (module, index, API) => {
3740
const args = []
3841
const indent = _.repeat(' ', moduleEvent.name.length + 29)
3942

40-
moduleEvent.returns.forEach((eventListenerArg) => {
43+
moduleEvent.returns.forEach((eventListenerArg, index) => {
4144
let argString = ''
4245
if (eventListenerArg.description) {
43-
argString += `\n${indent}`
46+
if (index === 0) argString += `\n${indent}`
4447
argString += utils.wrapComment(eventListenerArg.description).map((l, i) => `${l}\n${indent}`).join('')
4548
}
4649

@@ -73,9 +76,42 @@ const generateModuleDeclaration = (module, index, API) => {
7376
}
7477
})
7578

79+
// Dom Element Events
80+
if (module.domEvents) {
81+
module.domEvents.forEach((domEvent) => {
82+
utils.extendArray(moduleAPI, utils.wrapComment(domEvent.description))
83+
let eventType = 'Event'
84+
85+
if (domEvent.returns && domEvent.returns.length) {
86+
const fakeObject = {
87+
name: 'event',
88+
type: 'Object',
89+
collection: false,
90+
properties: []
91+
}
92+
93+
domEvent.returns.forEach((eventListenerProp, index) => {
94+
if (eventListenerProp.name === 'result') {
95+
eventListenerProp.__type = `${_.upperFirst(_.camelCase(domEvent.name))}Result`
96+
}
97+
fakeObject.properties.push(eventListenerProp)
98+
})
99+
100+
eventType = paramInterfaces.createParamInterface(fakeObject, _.upperFirst(_.camelCase(domEvent.name)))
101+
}
102+
103+
for (let method of ['addEventListener', 'removeEventListener']) {
104+
moduleAPI.push(`${isClass ? 'public ' : ''}${method}(event: '${domEvent.name}', listener: (event: ${eventType}) => void): this;`)
105+
}
106+
})
107+
}
108+
76109
const returnsThis = (moduleMethod) => ['on', 'once', 'removeAllListeners', 'removeListener'].includes(moduleMethod.name)
77110

78-
const addMethod = (moduleMethod, prefix = '') => {
111+
const addMethod = (moduleMethod, prefix) => {
112+
if (typeof prefix === 'undefined') {
113+
prefix = ''
114+
}
79115
const markAsPublic = isClass ? 'public ' : ''
80116
prefix = `${markAsPublic}${prefix}` || markAsPublic
81117
utils.extendArray(moduleAPI, utils.wrapComment(moduleMethod.description))
@@ -85,8 +121,13 @@ const generateModuleDeclaration = (module, index, API) => {
85121
returnType = moduleMethod.returns
86122
}
87123

88-
if (returnType === 'Object') {
124+
if (returnType === 'Object' || returnType.type === 'Object') {
89125
returnType = paramInterfaces.createParamInterface(moduleMethod.returns, _.upperFirst(moduleMethod.name))
126+
127+
// The process module is not in the Electron namespace so we need to reference the Electron namespace to use these types
128+
if (module.name === 'process') {
129+
returnType = `Electron.${returnType}`
130+
}
90131
}
91132

92133
const paramString = utils.genMethodString(paramInterfaces, module, moduleMethod, moduleMethod.parameters, moduleMethod.returns, false)
@@ -135,17 +176,18 @@ const generateModuleDeclaration = (module, index, API) => {
135176
.sort((a, b) => a.name.localeCompare(b.name))
136177
.forEach(prop => {
137178
if (prop.type === 'Class') {
138-
moduleAPI.push(`public static ${prop.name}: typeof ${prop.name}`);
139-
generateModuleDeclaration(prop, -1, API);
179+
moduleAPI.push(`public static ${prop.name}: typeof ${prop.name};`)
180+
generateModuleDeclaration(prop, -1, API)
140181
} else {
141-
moduleAPI.push(`public static ${prop.name}: ${utils.typify(prop)}`);
182+
moduleAPI.push(`public static ${prop.name}: ${utils.typify(prop)};`)
142183
}
143184
})
144185
}
145186

146187
// Structure properties
147-
if (module.properties) {
148-
module.properties
188+
const pseudoProperties = (module.properties || []).concat((module.attributes || []).map(attr => Object.assign({}, attr, { type: 'String' })))
189+
if (pseudoProperties.length) {
190+
pseudoProperties
149191
.sort((a, b) => a.name.localeCompare(b.name))
150192
.forEach(p => {
151193
let paramType = p
@@ -158,6 +200,8 @@ const generateModuleDeclaration = (module, index, API) => {
158200
const isOptional = utils.isOptional(p) ? '?' : ''
159201
const type = utils.typify(paramType)
160202

203+
utils.extendArray(moduleAPI, utils.wrapComment(p.description))
204+
if (module.name === 'process' && p.name === 'versions') return
161205
moduleAPI.push(`${isPublic}${isStatic}${p.name}${isOptional}: ${type};`)
162206
})
163207
}

lib/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ const typify = (type) => {
8383
return 'Buffer[]'
8484
case 'promise':
8585
return 'Promise<any>'
86+
case 'url':
87+
return 'string'
8688
}
89+
// if (type.substr(0, 8) === 'TouchBar' && type !== 'TouchBar') {
90+
// return `Electron.${type}`
91+
// }
8792
return type
8893
}
8994
const paramify = (paramName) => {

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
"lint-output": "tslint -c tslint.json -t verbose electron.d.ts --fix",
1212
"test": "mocha && standard && npm run test-output && npm run lint-output",
1313
"pretest-output": "npm run build -- -o=test-smoke/electron/index.d.ts && cd test-smoke/electron/test && rm -f *.js",
14-
"test-output": "cd test-smoke && node ../node_modules/types-publisher/bin/tester/test.js --run-from-definitely-typed --nProcesses 1",
15-
"posttest-output": "node scripts/lint.js && cd test-smoke/electron && tsc --project tsconfig.json"
14+
"test-output": "node scripts/lint.js && cd test-smoke/electron && tsc --project tsconfig.json"
1615
},
1716
"author": {
1817
"name": "Samuel Attard",
@@ -25,7 +24,6 @@
2524
"mocha": "^3.1.2",
2625
"standard": "^9.0.2",
2726
"tslint": "^4.5.1",
28-
"types-publisher": "github:microsoft/types-publisher#production",
2927
"typescript": "^2.2.1"
3028
},
3129
"standard": {
@@ -37,7 +35,7 @@
3735
"colors": "^1.1.2",
3836
"debug": "^2.6.3",
3937
"electron-docs": "^2.0.0",
40-
"electron-docs-linter": "^2.0.0",
38+
"electron-docs-linter": "^2.3.0",
4139
"lodash": "^4.16.1",
4240
"mkdirp": "^0.5.1",
4341
"rimraf": "^2.5.4"

test-smoke/electron/test/renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ crashReporter.start({
106106
productName: 'YourName',
107107
companyName: 'YourCompany',
108108
submitURL: 'https://your-domain.com/url-to-submit',
109-
autoSubmit: true
109+
uploadToServer: true
110110
});
111111

112112
// desktopCapturer

tslint.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"no-namespace": false,
1111
"variable-name": false,
1212
"no-empty-interface": false,
13-
"adjacent-overload-signatures": false
13+
"adjacent-overload-signatures": false,
14+
"quotemark": [false],
15+
"callable-types": [false]
1416
}
1517
}

0 commit comments

Comments
 (0)