-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add warning for constrainProportions in favor of targetAspectRatio (#34)
- Loading branch information
1 parent
7261892
commit a6b8588
Showing
8 changed files
with
199 additions
and
13 deletions.
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
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
2 changes: 2 additions & 0 deletions
2
dist/rules/constrainProportionsReplacedByTargetAspectRatioAdvice.d.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,2 @@ | ||
import type { TSESLint as _ } from '@typescript-eslint/utils'; | ||
export declare const constrainProportionsReplacedByTargetAspectRatioAdvice: _.RuleModule<"readAdvice" | "writeAdvice", never[], _.RuleListener>; |
52 changes: 52 additions & 0 deletions
52
dist/rules/constrainProportionsReplacedByTargetAspectRatioAdvice.js
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,52 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.constrainProportionsReplacedByTargetAspectRatioAdvice = void 0; | ||
const typescript_estree_1 = require("@typescript-eslint/typescript-estree"); | ||
const util_1 = require("../util"); | ||
exports.constrainProportionsReplacedByTargetAspectRatioAdvice = (0, util_1.createPluginRule)({ | ||
name: 'constrain-proportions-replaced-by-target-aspect-ratio-advice', | ||
meta: { | ||
docs: { | ||
description: 'Warns against using constrainProportions in favor of targetAspectRatio', | ||
}, | ||
messages: { | ||
readAdvice: 'Please use targetAspectRatio instead of constrainProportions for determining if a node will resize proportinally.', | ||
writeAdvice: 'Please use lockAspectRatio() or unlockAspectRatio() instead of setting constrainProportions.', | ||
}, | ||
schema: [], | ||
type: 'suggestion', | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
MemberExpression(node) { | ||
const property = node.property; | ||
if (property.type === typescript_estree_1.AST_NODE_TYPES.Identifier && | ||
property.name === 'constrainProportions') { | ||
// Check if the receiver is a LayoutMixin, since that's what constrainProportions lives on | ||
const match = (0, util_1.matchAncestorTypes)(context, node.object, ['LayoutMixin']); | ||
if (!match) { | ||
return; | ||
} | ||
// Check if it's being read or written to | ||
const parent = node.parent; | ||
if ((parent === null || parent === void 0 ? void 0 : parent.type) === typescript_estree_1.AST_NODE_TYPES.AssignmentExpression && | ||
parent.left === node) { | ||
// It's being written to | ||
context.report({ | ||
node, | ||
messageId: 'writeAdvice', | ||
}); | ||
} | ||
else { | ||
// It's being read | ||
context.report({ | ||
node, | ||
messageId: 'readAdvice', | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
5 changes: 5 additions & 0 deletions
5
docs/rules/constrain-proportions-replaced-by-target-aspect-ratio-advice.md
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,5 @@ | ||
# Warns against using constrainProportions in favor of targetAspectRatio (`@figma/figma-plugins/constrain-proportions-replaced-by-target-aspect-ratio-advice`) | ||
|
||
⚠️ This rule _warns_ in the 👍 `recommended` config. | ||
|
||
<!-- end auto-generated rule header --> |
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
59 changes: 59 additions & 0 deletions
59
src/rules/constrainProportionsReplacedByTargetAspectRatioAdvice.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,59 @@ | ||
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' | ||
import { createPluginRule, matchAncestorTypes } from '../util' | ||
|
||
// Copied from dynamicPageFindMethodAdvice | ||
// Calls to createPluginRule() cause typechecker errors without this import. | ||
// Needed for TypeScript bug | ||
import type { TSESLint as _ } from '@typescript-eslint/utils' | ||
|
||
export const constrainProportionsReplacedByTargetAspectRatioAdvice = createPluginRule({ | ||
name: 'constrain-proportions-replaced-by-target-aspect-ratio-advice', | ||
meta: { | ||
docs: { | ||
description: 'Warns against using constrainProportions in favor of targetAspectRatio', | ||
}, | ||
messages: { | ||
readAdvice: 'Please use targetAspectRatio instead of constrainProportions for determining if a node will resize proportinally.', | ||
writeAdvice: 'Please use lockAspectRatio() or unlockAspectRatio() instead of setting constrainProportions.', | ||
}, | ||
schema: [], | ||
type: 'suggestion', | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
MemberExpression(node: TSESTree.MemberExpression) { | ||
const property = node.property | ||
if ( | ||
property.type === AST_NODE_TYPES.Identifier && | ||
property.name === 'constrainProportions' | ||
) { | ||
// Check if the receiver is a LayoutMixin, since that's what constrainProportions lives on | ||
const match = matchAncestorTypes(context, node.object, ['LayoutMixin']) | ||
if (!match) { | ||
return | ||
} | ||
|
||
// Check if it's being read or written to | ||
const parent = node.parent | ||
if ( | ||
parent?.type === AST_NODE_TYPES.AssignmentExpression && | ||
parent.left === node | ||
) { | ||
// It's being written to | ||
context.report({ | ||
node, | ||
messageId: 'writeAdvice', | ||
}) | ||
} else { | ||
// It's being read | ||
context.report({ | ||
node, | ||
messageId: 'readAdvice', | ||
}) | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
59 changes: 59 additions & 0 deletions
59
test/constrainProportionsReplacedByTargetAspectRatio.test.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,59 @@ | ||
import { constrainProportionsReplacedByTargetAspectRatioAdvice } from '../src/rules/constrainProportionsReplacedByTargetAspectRatioAdvice' | ||
import { ruleTester } from './testUtil' | ||
|
||
const types = ` | ||
interface LayoutMixin { | ||
constrainProportions: boolean | ||
} | ||
interface DefaultFrameMixin extends LayoutMixin {} | ||
interface FrameNode extends DefaultFrameMixin {} | ||
interface SceneNode extends LayoutMixin {} | ||
` | ||
|
||
ruleTester().run('constrain-proportions-replaced-by-target-aspect-ratio', constrainProportionsReplacedByTargetAspectRatioAdvice, { | ||
valid: [ | ||
{ | ||
code: ` | ||
${types} | ||
function func(node: FrameNode) { | ||
node.someOtherProp = true | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
// Test write case | ||
code: ` | ||
${types} | ||
function func(node: SceneNode) { | ||
node.constrainProportions = true | ||
} | ||
`, | ||
errors: [{ messageId: 'writeAdvice' }], | ||
}, | ||
{ | ||
// Test write case | ||
code: ` | ||
${types} | ||
function func(node: FrameNode) { | ||
node.constrainProportions = false | ||
} | ||
`, | ||
errors: [{ messageId: 'writeAdvice' }], | ||
}, | ||
{ | ||
// Test read case | ||
code: ` | ||
${types} | ||
function func(node: SceneNode) { | ||
const value = node.constrainProportions | ||
} | ||
`, | ||
errors: [{ messageId: 'readAdvice' }], | ||
}, | ||
], | ||
}) |