|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT |
| 6 | + */ |
| 7 | +'use strict'; |
| 8 | + |
| 9 | +const { testRule, testTypeScript } = require('../shared'); |
| 10 | + |
| 11 | +testRule('newer-version-available', { |
| 12 | + valid: [ |
| 13 | + { |
| 14 | + code: `import { gql, graphql } from 'lightning/graphql';`, |
| 15 | + }, |
| 16 | + { |
| 17 | + code: `import { LightningElement } from 'lwc';`, |
| 18 | + }, |
| 19 | + { |
| 20 | + code: `import { wire } from 'lwc';`, |
| 21 | + }, |
| 22 | + { |
| 23 | + code: `const module = 'lightning/uiGraphQLApi'; // just a string, not an import`, |
| 24 | + }, |
| 25 | + { |
| 26 | + code: `// This is allowed with empty modules with newer versions list |
| 27 | + import something from 'some/module';`, |
| 28 | + options: [{ modulesWithNewerVersions: {} }], |
| 29 | + }, |
| 30 | + ], |
| 31 | + invalid: [ |
| 32 | + { |
| 33 | + code: `import { gql, graphql } from 'lightning/uiGraphQLApi';`, |
| 34 | + errors: [ |
| 35 | + { |
| 36 | + message: |
| 37 | + 'A newer version is available: use "lightning/graphql" instead of "lightning/uiGraphQLApi" for non Mobile-Offline use cases.', |
| 38 | + }, |
| 39 | + ], |
| 40 | + output: `import { gql, graphql } from 'lightning/graphql';`, |
| 41 | + }, |
| 42 | + { |
| 43 | + code: `import { gql } from 'lightning/uiGraphQLApi';`, |
| 44 | + errors: [ |
| 45 | + { |
| 46 | + message: |
| 47 | + 'A newer version is available: use "lightning/graphql" instead of "lightning/uiGraphQLApi" for non Mobile-Offline use cases.', |
| 48 | + }, |
| 49 | + ], |
| 50 | + output: `import { gql } from 'lightning/graphql';`, |
| 51 | + }, |
| 52 | + { |
| 53 | + code: `import { graphql } from 'lightning/uiGraphQLApi';`, |
| 54 | + errors: [ |
| 55 | + { |
| 56 | + message: |
| 57 | + 'A newer version is available: use "lightning/graphql" instead of "lightning/uiGraphQLApi" for non Mobile-Offline use cases.', |
| 58 | + }, |
| 59 | + ], |
| 60 | + output: `import { graphql } from 'lightning/graphql';`, |
| 61 | + }, |
| 62 | + { |
| 63 | + code: `import graphqlApi from 'lightning/uiGraphQLApi';`, |
| 64 | + errors: [ |
| 65 | + { |
| 66 | + message: |
| 67 | + 'A newer version is available: use "lightning/graphql" instead of "lightning/uiGraphQLApi" for non Mobile-Offline use cases.', |
| 68 | + }, |
| 69 | + ], |
| 70 | + output: `import graphqlApi from 'lightning/graphql';`, |
| 71 | + }, |
| 72 | + { |
| 73 | + code: `import * as graphqlApi from 'lightning/uiGraphQLApi';`, |
| 74 | + errors: [ |
| 75 | + { |
| 76 | + message: |
| 77 | + 'A newer version is available: use "lightning/graphql" instead of "lightning/uiGraphQLApi" for non Mobile-Offline use cases.', |
| 78 | + }, |
| 79 | + ], |
| 80 | + output: `import * as graphqlApi from 'lightning/graphql';`, |
| 81 | + }, |
| 82 | + { |
| 83 | + // Test refreshGraphQL import - should warn and NOT auto-fix |
| 84 | + code: `import { refreshGraphQL } from 'lightning/uiGraphQLApi';`, |
| 85 | + errors: [ |
| 86 | + { |
| 87 | + message: |
| 88 | + 'A newer version is available: use "lightning/graphql" instead of "lightning/uiGraphQLApi" for non Mobile-Offline use cases.', |
| 89 | + }, |
| 90 | + { |
| 91 | + message: |
| 92 | + '"refreshGraphQL" is not available in lightning/graphql. Use refresh from the wire adapter result instead.', |
| 93 | + }, |
| 94 | + ], |
| 95 | + output: null, // No auto-fix because refreshGraphQL doesn't exist in replacement |
| 96 | + }, |
| 97 | + { |
| 98 | + // Test mixed imports with refreshGraphQL - should warn and NOT auto-fix |
| 99 | + code: `import { gql, graphql, refreshGraphQL } from 'lightning/uiGraphQLApi';`, |
| 100 | + errors: [ |
| 101 | + { |
| 102 | + message: |
| 103 | + 'A newer version is available: use "lightning/graphql" instead of "lightning/uiGraphQLApi" for non Mobile-Offline use cases.', |
| 104 | + }, |
| 105 | + { |
| 106 | + message: |
| 107 | + '"refreshGraphQL" is not available in lightning/graphql. Use refresh from the wire adapter result instead.', |
| 108 | + }, |
| 109 | + ], |
| 110 | + output: null, // No auto-fix because refreshGraphQL doesn't exist in replacement |
| 111 | + }, |
| 112 | + { |
| 113 | + // Test custom modules with newer versions through options |
| 114 | + code: `import something from 'old/deprecated/module';`, |
| 115 | + options: [ |
| 116 | + { |
| 117 | + modulesWithNewerVersions: { |
| 118 | + 'old/deprecated/module': { |
| 119 | + replacement: 'new/modern/module', |
| 120 | + message: |
| 121 | + 'A newer version is available: use new/modern/module instead of old/deprecated/module.', |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + ], |
| 126 | + errors: [ |
| 127 | + { |
| 128 | + message: |
| 129 | + 'A newer version is available: use new/modern/module instead of old/deprecated/module.', |
| 130 | + }, |
| 131 | + ], |
| 132 | + output: `import something from 'new/modern/module';`, |
| 133 | + }, |
| 134 | + ], |
| 135 | +}); |
| 136 | + |
| 137 | +testTypeScript('newer-version-available', { |
| 138 | + valid: [ |
| 139 | + { |
| 140 | + code: `import { gql, graphql } from 'lightning/graphql'; |
| 141 | + import { LightningElement } from 'lwc'; |
| 142 | +
|
| 143 | + export default class Test extends LightningElement {}`, |
| 144 | + }, |
| 145 | + ], |
| 146 | + invalid: [ |
| 147 | + { |
| 148 | + code: `import { gql, graphql } from 'lightning/uiGraphQLApi'; |
| 149 | + import { LightningElement } from 'lwc'; |
| 150 | +
|
| 151 | + export default class Test extends LightningElement {}`, |
| 152 | + errors: [ |
| 153 | + { |
| 154 | + message: |
| 155 | + 'A newer version is available: use "lightning/graphql" instead of "lightning/uiGraphQLApi" for non Mobile-Offline use cases.', |
| 156 | + }, |
| 157 | + ], |
| 158 | + output: `import { gql, graphql } from 'lightning/graphql'; |
| 159 | + import { LightningElement } from 'lwc'; |
| 160 | +
|
| 161 | + export default class Test extends LightningElement {}`, |
| 162 | + }, |
| 163 | + ], |
| 164 | +}); |
0 commit comments