|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +/** All functions whose names have been prefixed with `m2-` in v18. */ |
| 10 | +const RENAMED_FUNCTIONS = [ |
| 11 | + 'define-light-theme', |
| 12 | + 'define-dark-theme', |
| 13 | + 'define-palette', |
| 14 | + 'get-contrast-color-from-palette', |
| 15 | + 'get-color-from-palette', |
| 16 | + 'get-color-config', |
| 17 | + 'get-typography-config', |
| 18 | + 'get-density-config', |
| 19 | + 'define-typography-level', |
| 20 | + 'define-rem-typography-config', |
| 21 | + 'define-typography-config', |
| 22 | + 'define-legacy-typography-config', |
| 23 | + 'typography-level', |
| 24 | + 'font-size', |
| 25 | + 'line-height', |
| 26 | + 'font-weight', |
| 27 | + 'letter-spacing', |
| 28 | + 'font-family', |
| 29 | +]; |
| 30 | + |
| 31 | +/** All variables whose names have been prefixed with `m2-` in v18. */ |
| 32 | +const RENAMED_VARIABLES = [ |
| 33 | + 'red-palette', |
| 34 | + 'pink-palette', |
| 35 | + 'indigo-palette', |
| 36 | + 'purple-palette', |
| 37 | + 'deep-purple-palette', |
| 38 | + 'blue-palette', |
| 39 | + 'light-blue-palette', |
| 40 | + 'cyan-palette', |
| 41 | + 'teal-palette', |
| 42 | + 'green-palette', |
| 43 | + 'light-green-palette', |
| 44 | + 'lime-palette', |
| 45 | + 'yellow-palette', |
| 46 | + 'amber-palette', |
| 47 | + 'orange-palette', |
| 48 | + 'deep-orange-palette', |
| 49 | + 'brown-palette', |
| 50 | + 'grey-palette', |
| 51 | + 'gray-palette', |
| 52 | + 'blue-grey-palette', |
| 53 | + 'blue-gray-palette', |
| 54 | + 'light-theme-background-palette', |
| 55 | + 'dark-theme-background-palette', |
| 56 | + 'light-theme-foreground-palette', |
| 57 | + 'dark-theme-foreground-palette', |
| 58 | +]; |
| 59 | + |
| 60 | +/** M3 theming functions that were moved into stable. */ |
| 61 | +const M3_FUNCTIONS = ['define-theme', 'define-colors', 'define-typography', 'define-density']; |
| 62 | + |
| 63 | +/** M3 variables that were moved into stable. */ |
| 64 | +const M3_VARIABLES = [ |
| 65 | + 'red-palette', |
| 66 | + 'green-palette', |
| 67 | + 'blue-palette', |
| 68 | + 'yellow-palette', |
| 69 | + 'cyan-palette', |
| 70 | + 'magenta-palette', |
| 71 | + 'orange-palette', |
| 72 | + 'chartreuse-palette', |
| 73 | + 'azure-palette', |
| 74 | + 'violet-palette', |
| 75 | + 'rose-palette', |
| 76 | +]; |
| 77 | + |
| 78 | +/** Possible pairs of comment characters in a Sass file. */ |
| 79 | +const COMMENT_PAIRS = new Map<string, string>([ |
| 80 | + ['/*', '*/'], |
| 81 | + ['//', '\n'], |
| 82 | +]); |
| 83 | + |
| 84 | +/** Prefix for the placeholder that will be used to escape comments. */ |
| 85 | +const COMMENT_PLACEHOLDER_START = '__<<ngM2ThemingMigrationEscapedComment'; |
| 86 | + |
| 87 | +/** Suffix for the comment escape placeholder. */ |
| 88 | +const COMMENT_PLACEHOLDER_END = '>>__'; |
| 89 | + |
| 90 | +/** Replaces all usages of renamed M2 theming APIs in a file. */ |
| 91 | +export function migrateM2ThemingApiUsages(fileContent: string): string { |
| 92 | + // Strip out comments, so they don't confuse our migration. |
| 93 | + let {content, placeholders} = escapeComments(fileContent); |
| 94 | + const materialNamespaces = getNamespaces('@angular/material', content); |
| 95 | + const experimentalNamespaces = getNamespaces('@angular/material-experimental', content); |
| 96 | + |
| 97 | + // Migrate the APIs whose names were prefixed with `m2-`. |
| 98 | + for (const namespace of materialNamespaces) { |
| 99 | + for (const name of RENAMED_FUNCTIONS) { |
| 100 | + content = migrateFunction(content, namespace, name, namespace, 'm2-' + name); |
| 101 | + } |
| 102 | + |
| 103 | + for (const name of RENAMED_VARIABLES) { |
| 104 | + content = migrateVariable(content, namespace, name, namespace, 'm2-' + name); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Migrate themes that were using M3 while it was still in experimental. |
| 109 | + if (experimentalNamespaces.length > 0) { |
| 110 | + const preExperimentalContent = content; |
| 111 | + const stableNamespace = materialNamespaces.length === 0 ? 'mat' : materialNamespaces[0]; |
| 112 | + |
| 113 | + for (const namespace of experimentalNamespaces) { |
| 114 | + // The only mixin that was renamed was the backwards-compatibility one. |
| 115 | + content = migrateMixin( |
| 116 | + content, |
| 117 | + namespace, |
| 118 | + 'color-variants-back-compat', |
| 119 | + stableNamespace, |
| 120 | + 'color-variants-backwards-compatibility', |
| 121 | + ); |
| 122 | + |
| 123 | + // M3 functions weren't prefixed with anything |
| 124 | + // so they just move over to the new namespace. |
| 125 | + for (const name of M3_FUNCTIONS) { |
| 126 | + content = migrateFunction(content, namespace, name, stableNamespace, name); |
| 127 | + } |
| 128 | + |
| 129 | + // Variables were all prefixed with `m3-` which needs to be stripped. |
| 130 | + for (const name of M3_VARIABLES) { |
| 131 | + content = migrateVariable(content, namespace, 'm3-' + name, stableNamespace, name); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // If experimental is imported, but Material isn't, insert a new import at the top. |
| 136 | + // This should be rare since `@angular/material` was still required for the theme. |
| 137 | + if (materialNamespaces.length === 0 && content !== preExperimentalContent) { |
| 138 | + content = `@use '@angular/material' as ${stableNamespace};\n` + content; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return restoreComments(content, placeholders); |
| 143 | +} |
| 144 | + |
| 145 | +/** Renames all usages of a Sass function in a file. */ |
| 146 | +function migrateFunction( |
| 147 | + fileContent: string, |
| 148 | + oldNamespace: string, |
| 149 | + oldName: string, |
| 150 | + newNamespace: string, |
| 151 | + newName: string, |
| 152 | +): string { |
| 153 | + return fileContent.replace( |
| 154 | + new RegExp(`${oldNamespace}\\.${oldName}\\(`, 'g'), |
| 155 | + `${newNamespace}.${newName}(`, |
| 156 | + ); |
| 157 | +} |
| 158 | + |
| 159 | +/** Renames all usages of a Sass variable in a file. */ |
| 160 | +function migrateVariable( |
| 161 | + fileContent: string, |
| 162 | + oldNamespace: string, |
| 163 | + oldName: string, |
| 164 | + newNamespace: string, |
| 165 | + newName: string, |
| 166 | +): string { |
| 167 | + return fileContent.replace( |
| 168 | + new RegExp(`${oldNamespace}\\.\\$${oldName}(?!\\s+:|[-_a-zA-Z0-9:])`, 'g'), |
| 169 | + `${newNamespace}.$${newName}`, |
| 170 | + ); |
| 171 | +} |
| 172 | + |
| 173 | +/** Renames all usages of a Sass mixin in a file. */ |
| 174 | +function migrateMixin( |
| 175 | + fileContent: string, |
| 176 | + oldNamespace: string, |
| 177 | + oldName: string, |
| 178 | + newNamespace: string, |
| 179 | + newName: string, |
| 180 | +): string { |
| 181 | + const pattern = new RegExp(`@include +${oldNamespace}\\.${oldName}`, 'g'); |
| 182 | + return fileContent.replace(pattern, `@include ${newNamespace}.${newName}`); |
| 183 | +} |
| 184 | + |
| 185 | +/** |
| 186 | + * Replaces all the comments in a Sass file with placeholders and |
| 187 | + * returns the list of placeholders, so they can be restored later. |
| 188 | + */ |
| 189 | +function escapeComments(content: string): {content: string; placeholders: Record<string, string>} { |
| 190 | + const placeholders: Record<string, string> = {}; |
| 191 | + let commentCounter = 0; |
| 192 | + let [openIndex, closeIndex] = findComment(content); |
| 193 | + |
| 194 | + while (openIndex > -1 && closeIndex > -1) { |
| 195 | + const placeholder = COMMENT_PLACEHOLDER_START + commentCounter++ + COMMENT_PLACEHOLDER_END; |
| 196 | + placeholders[placeholder] = content.slice(openIndex, closeIndex); |
| 197 | + content = content.slice(0, openIndex) + placeholder + content.slice(closeIndex); |
| 198 | + [openIndex, closeIndex] = findComment(content); |
| 199 | + } |
| 200 | + |
| 201 | + return {content, placeholders}; |
| 202 | +} |
| 203 | + |
| 204 | +/** Finds the start and end index of a comment in a file. */ |
| 205 | +function findComment(content: string): [openIndex: number, closeIndex: number] { |
| 206 | + // Add an extra new line at the end so that we can correctly capture single-line comments |
| 207 | + // at the end of the file. It doesn't really matter that the end index will be out of bounds, |
| 208 | + // because `String.prototype.slice` will clamp it to the string length. |
| 209 | + content += '\n'; |
| 210 | + |
| 211 | + for (const [open, close] of COMMENT_PAIRS.entries()) { |
| 212 | + const openIndex = content.indexOf(open); |
| 213 | + |
| 214 | + if (openIndex > -1) { |
| 215 | + const closeIndex = content.indexOf(close, openIndex + 1); |
| 216 | + return closeIndex > -1 ? [openIndex, closeIndex + close.length] : [-1, -1]; |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + return [-1, -1]; |
| 221 | +} |
| 222 | + |
| 223 | +/** Restores the comments that have been escaped by `escapeComments`. */ |
| 224 | +function restoreComments(content: string, placeholders: Record<string, string>): string { |
| 225 | + Object.keys(placeholders).forEach(key => (content = content.replace(key, placeholders[key]))); |
| 226 | + return content; |
| 227 | +} |
| 228 | + |
| 229 | +/** Parses out the namespace from a Sass `@use` statement. */ |
| 230 | +function extractNamespaceFromUseStatement(fullImport: string): string { |
| 231 | + const closeQuoteIndex = Math.max(fullImport.lastIndexOf(`"`), fullImport.lastIndexOf(`'`)); |
| 232 | + |
| 233 | + if (closeQuoteIndex > -1) { |
| 234 | + const asExpression = 'as '; |
| 235 | + const asIndex = fullImport.indexOf(asExpression, closeQuoteIndex); |
| 236 | + |
| 237 | + // If we found an ` as ` expression, we consider the rest of the text as the namespace. |
| 238 | + if (asIndex > -1) { |
| 239 | + return fullImport |
| 240 | + .slice(asIndex + asExpression.length) |
| 241 | + .split(';')[0] |
| 242 | + .trim(); |
| 243 | + } |
| 244 | + |
| 245 | + // Otherwise the namespace is the name of the file that is being imported. |
| 246 | + const lastSlashIndex = fullImport.lastIndexOf('/', closeQuoteIndex); |
| 247 | + |
| 248 | + if (lastSlashIndex > -1) { |
| 249 | + const fileName = fullImport |
| 250 | + .slice(lastSlashIndex + 1, closeQuoteIndex) |
| 251 | + // Sass allows for leading underscores to be omitted and it technically supports .scss. |
| 252 | + .replace(/^_|(\.import)?\.scss$|\.import$/g, ''); |
| 253 | + |
| 254 | + // Sass ignores `/index` and infers the namespace as the next segment in the path. |
| 255 | + if (fileName === 'index') { |
| 256 | + const nextSlashIndex = fullImport.lastIndexOf('/', lastSlashIndex - 1); |
| 257 | + |
| 258 | + if (nextSlashIndex > -1) { |
| 259 | + return fullImport.slice(nextSlashIndex + 1, lastSlashIndex); |
| 260 | + } |
| 261 | + } else { |
| 262 | + return fileName; |
| 263 | + } |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + throw Error(`Could not extract namespace from import "${fullImport}".`); |
| 268 | +} |
| 269 | + |
| 270 | +/** Gets all the namespaces that a module is available under in a specific file. */ |
| 271 | +function getNamespaces(moduleName: string, content: string): string[] { |
| 272 | + const namespaces = new Set<string>(); |
| 273 | + const escapedName = moduleName.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1'); |
| 274 | + const pattern = new RegExp(`@use +['"]${escapedName}['"].*;?\n`, 'g'); |
| 275 | + let match: RegExpExecArray | null = null; |
| 276 | + |
| 277 | + while ((match = pattern.exec(content))) { |
| 278 | + namespaces.add(extractNamespaceFromUseStatement(match[0])); |
| 279 | + } |
| 280 | + |
| 281 | + return Array.from(namespaces); |
| 282 | +} |
0 commit comments