forked from microsoft/pyright
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add warning diagnostic when using V2 api #33
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c5e5a5c
Add warning diagnostic when using V2 api
microbit-robert c610dc8
Refactor
microbit-robert 799acea
Add SoundEffect to V2 API check
microbit-robert 3d9147c
Special case pin_logo and pin_speaker
microbit-robert a2dca10
Merge branch 'microbit' into version-warning
microbit-robert 59d2c2d
Improve callback typing
microbit-robert a9e64c3
Add SoundEvent and bail if type includes subclasses
microbit-robert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
packages/pyright-internal/src/analyzer/microbitUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import { DiagnosticLevel } from '../common/configOptions'; | ||
import { Diagnostic } from '../common/diagnostic'; | ||
import { DiagnosticRule } from '../common/diagnosticRules'; | ||
import { Localizer } from '../localization/localize'; | ||
import { NameNode, ParseNode } from '../parser/parseNodes'; | ||
import { AnalyzerFileInfo } from './analyzerFileInfo'; | ||
import * as AnalyzerNodeInfo from './analyzerNodeInfo'; | ||
import { isClass, isFunction, isModule, isOverloadedFunction, Type } from './types'; | ||
|
||
type AddDiagnostic = ( | ||
diagLevel: DiagnosticLevel, | ||
rule: string, | ||
message: string, | ||
node: ParseNode | ||
) => Diagnostic | undefined; | ||
|
||
export const device = 'micro:bit V1'; | ||
|
||
function getNames(type: Type) { | ||
if (isFunction(type) || isClass(type)) { | ||
return { | ||
moduleName: type.details.moduleName, | ||
name: type.details.name, | ||
}; | ||
} | ||
if (isOverloadedFunction(type)) { | ||
return { | ||
moduleName: type.overloads[0].details.moduleName, | ||
name: type.overloads[0].details.name, | ||
}; | ||
} | ||
return { | ||
moduleName: '', | ||
name: '', | ||
}; | ||
} | ||
|
||
function usesMicrobitV2Api(moduleName: string, name?: string) { | ||
return ( | ||
['log', 'microbit.microphone', 'microbit.speaker', 'power'].includes(moduleName) || | ||
(moduleName === 'microbit' && | ||
['run_every', 'set_volume', 'Sound', 'SoundEvent', 'pin_logo', 'pin_speaker'].includes(name ?? '')) || | ||
(moduleName === 'microbit.audio' && name === 'SoundEffect') || | ||
(moduleName === 'neopixel' && ['fill', 'write'].includes(name ?? '')) | ||
); | ||
} | ||
|
||
function getFileInfo(node: ParseNode): AnalyzerFileInfo { | ||
return AnalyzerNodeInfo.getFileInfo(node); | ||
} | ||
|
||
function getDiagLevel(fileInfo: AnalyzerFileInfo): DiagnosticLevel { | ||
return fileInfo.diagnosticRuleSet.reportMicrobitV2ApiUse; | ||
} | ||
|
||
function addModuleVersionWarning(addDiagnostic: AddDiagnostic, moduleName: string, node: ParseNode) { | ||
const fileInfo = getFileInfo(node); | ||
addDiagnostic( | ||
getDiagLevel(fileInfo), | ||
DiagnosticRule.reportMicrobitV2ApiUse, | ||
Localizer.Diagnostic.microbitV2ModuleUse().format({ | ||
moduleName: moduleName, | ||
device, | ||
}), | ||
node | ||
); | ||
} | ||
|
||
function addModuleMemberVersionWarning( | ||
addDiagnostic: AddDiagnostic, | ||
name: string, | ||
moduleName: string, | ||
node: ParseNode | ||
) { | ||
const fileInfo = getFileInfo(node); | ||
addDiagnostic( | ||
getDiagLevel(fileInfo), | ||
DiagnosticRule.reportMicrobitV2ApiUse, | ||
Localizer.Diagnostic.microbitV2ModuleMemberUse().format({ | ||
name, | ||
moduleName, | ||
device, | ||
}), | ||
node | ||
); | ||
} | ||
|
||
function addClassMethodVersionWarning( | ||
addDiagnostic: AddDiagnostic, | ||
methodName: string, | ||
className: string, | ||
node: ParseNode | ||
) { | ||
const fileInfo = getFileInfo(node); | ||
addDiagnostic( | ||
getDiagLevel(fileInfo), | ||
DiagnosticRule.reportMicrobitV2ApiUse, | ||
Localizer.Diagnostic.microbitV2ClassMethodUse().format({ | ||
methodName, | ||
className, | ||
device, | ||
}), | ||
node | ||
); | ||
} | ||
|
||
export function maybeAddMicrobitVersionWarning( | ||
type: Type, | ||
node: ParseNode, | ||
addDiagnostic: AddDiagnostic, | ||
memberName?: string | ||
) { | ||
if (isModule(type)) { | ||
if (usesMicrobitV2Api(type.moduleName, memberName)) { | ||
addModuleVersionWarning(addDiagnostic, type.moduleName, node); | ||
} | ||
} | ||
|
||
const moduleName = getNames(type).moduleName; | ||
let name = getNames(type).name; | ||
if (!moduleName && !name) { | ||
return; | ||
} | ||
|
||
// Special case pin_logo and pin_speaker. | ||
if (name === 'MicroBitAnalogDigitalPin' && (node as NameNode).value === 'pin_speaker') { | ||
name = 'pin_speaker'; | ||
} | ||
if (name === 'MicroBitTouchPin' && (node as NameNode).value === 'pin_logo') { | ||
name = 'pin_logo'; | ||
} | ||
|
||
if (usesMicrobitV2Api(moduleName, name)) { | ||
if (isFunction(type) && type.boundToType) { | ||
const className = type.boundToType?.details.name; | ||
addClassMethodVersionWarning(addDiagnostic, name, className, node); | ||
return; | ||
} | ||
|
||
if (isClass(type) && type.includeSubclasses) { | ||
// Bail. Example, we only want to warn for microbit.Sound once and not again for microbit.Sound.GIGGLE. | ||
return; | ||
} | ||
|
||
// The type must be a function, overloaded function or class at this point. | ||
addModuleMemberVersionWarning(addDiagnostic, name, moduleName, node); | ||
} | ||
} | ||
|
||
// Required as passing in this._addDiagnostic results in errors. | ||
// See binder.ts for use. | ||
export function maybeAddMicrobitVersionWarningBinderWrapper(moduleName: string, callback: () => void) { | ||
if (usesMicrobitV2Api(moduleName)) { | ||
callback(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,9 @@ | |
"memberSet": "Cannot assign member \"{name}\" for type \"{type}\"", | ||
"moduleNotCallable": "Module is not callable", | ||
"moduleUnknownMember": "\"{name}\" is not a known member of module \"{module}\"", | ||
"microbitV2ClassMethodUse": "The \"{methodName}\" method on the \"{className}\" class cannot be used with a {device}", | ||
"microbitV2ModuleMemberUse": "\"{name}\" from the \"{moduleName}\" module cannot be used with a {device}", | ||
"microbitV2ModuleUse": "\"{moduleName}\" cannot be used with a {device}", | ||
Comment on lines
+61
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Highlighting for review as these will be tweaked based on prior discussion. |
||
"nonDefaultAfterDefault": "Non-default argument follows default argument", | ||
"noOverload": "Arguments do not match parameter types", | ||
"objectNotCallable": "Object is not callable", | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is setup with a callback so we can track micro:bit specific diagnostics in one file,
microbitUtils.ts
. Useful if we only want to warn on the first use of a V2 API call in the future. We can't directly pass inthis_.addDiagnostic
as this causes errors.