Skip to content

Commit 5786fd6

Browse files
author
Dimitar Tachev
authored
Merge pull request nativescript-community#255 from NativeScript/tachev/fix-source-map-overrides
fix: replace a hardcoded platform in the source maps with a dynamic one
2 parents 0ddb260 + 28cf52e commit 5786fd6

File tree

5 files changed

+135
-107
lines changed

5 files changed

+135
-107
lines changed

src/debug-adapter/nativeScriptDebugAdapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class NativeScriptDebugAdapter extends ChromeDebugAdapter {
143143
const appDirPath = this.getAppDirPath(transformedArgs.webRoot);
144144

145145
(this.pathTransformer as any).setTransformOptions(args.platform, appDirPath, transformedArgs.webRoot);
146-
(this.sourceMapTransformer as NativeScriptSourceMapTransformer).setDebugAdapter(this);
146+
(this.sourceMapTransformer as NativeScriptSourceMapTransformer).setTransformOptions(args.platform.toLowerCase(), this);
147147
(ChromeDebugAdapter as any).SET_BREAKPOINTS_TIMEOUT = 20000;
148148

149149
this.isLiveSync = args.watch;

src/debug-adapter/nativeScriptSourceMapTransformer.ts

Lines changed: 99 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -3,108 +3,17 @@ import { BaseSourceMapTransformer, logger as vsCodeChromeDebugLogger, utils as v
33
import * as sourceMapUtils from 'vscode-chrome-debug-core/out/src/sourceMaps/sourceMapUtils'; // tslint:disable-line
44
import { NativeScriptDebugAdapter } from './nativeScriptDebugAdapter';
55

6-
// a workaround for https://github.com/NativeScript/nativescript-vscode-extension/issues/252
7-
// tslint:disable
8-
//--- begin part copied from vscode-chrome-debug-core (https://github.com/microsoft/vscode-chrome-debug-core/blob/d1fe8ca062e277a3480879e1fb63a8ef3b48015a/src/sourceMaps/sourceMapUtils.ts#L78)
9-
//
10-
// VS Code - Debugger for Chrome
11-
//
12-
// Copyright (c) Microsoft Corporation
13-
//
14-
// All rights reserved.
15-
//
16-
// MIT License
17-
//
18-
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
19-
//
20-
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
21-
//
22-
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23-
24-
sourceMapUtils.applySourceMapPathOverrides = function applySourceMapPathOverrides(sourcePath, sourceMapPathOverrides, isVSClient = false) {
25-
const forwardSlashSourcePath = sourcePath.replace(/\\/g, '/');
26-
// Sort the overrides by length, large to small
27-
const sortedOverrideKeys = Object.keys(sourceMapPathOverrides)
28-
.sort((a, b) => b.length - a.length);
29-
30-
// Iterate the key/vals, only apply the first one that matches.
31-
for (const leftPattern of sortedOverrideKeys) {
32-
const rightPattern = sourceMapPathOverrides[leftPattern];
33-
const entryStr = `"${leftPattern}": "${rightPattern}"`;
34-
const asterisks = leftPattern.match(/\*/g) || [];
35-
36-
if (asterisks.length > 1) {
37-
vsCodeChromeDebugLogger.log(`Warning: only one asterisk allowed in a sourceMapPathOverrides entry - ${entryStr}`);
38-
continue;
39-
}
40-
const replacePatternAsterisks = rightPattern.match(/\*/g) || [];
41-
42-
if (replacePatternAsterisks.length > asterisks.length) {
43-
vsCodeChromeDebugLogger.log(`Warning: the right side of a sourceMapPathOverrides entry must have 0 or 1 asterisks - ${entryStr}}`);
44-
continue;
45-
}
46-
// Does it match?
47-
const escapedLeftPattern = vsCodeChromeDebugUtils.escapeRegexSpecialChars(leftPattern, '/*');
48-
const leftRegexSegment = escapedLeftPattern
49-
.replace(/\*/g, '(.*)')
50-
.replace(/\\\\/g, '/');
51-
const leftRegex = new RegExp(`^${leftRegexSegment}$`, 'i');
52-
const overridePatternMatches = forwardSlashSourcePath.match(leftRegex);
53-
54-
if (!overridePatternMatches) {
55-
continue;
56-
}
57-
// Grab the value of the wildcard from the match above, replace the wildcard in the
58-
// replacement pattern, and return the result.
59-
const wildcardValue = overridePatternMatches[1];
60-
61-
// ***************** CUSTOM CODE START *****************
62-
// handle linked files
63-
let mappedPath = path.isAbsolute(wildcardValue) ? wildcardValue : rightPattern.replace(/\*/g, wildcardValue);
64-
// ***************** CUSTOM CODE END *****************
65-
66-
mappedPath = path.join(mappedPath); // Fix any ..
67-
68-
// ***************** CUSTOM CODE START *****************
69-
// handle platform-specific files
70-
const platform = 'android';
71-
const { dir, name, ext } = path.parse(mappedPath);
72-
73-
const platformFileName = `${name}.${platform}${ext}`;
74-
const platformPath = path.join(dir, platformFileName);
75-
76-
if (vsCodeChromeDebugUtils.existsSync(platformPath)) {
77-
mappedPath = platformPath;
78-
}
79-
// ***************** CUSTOM CODE END *****************
80-
81-
if (isVSClient && leftPattern === 'webpack:///./*' && !vsCodeChromeDebugUtils.existsSync(mappedPath)) {
82-
// This is a workaround for a bug in ASP.NET debugging in VisualStudio because the wwwroot is not properly configured
83-
const pathFixingASPNETBug = path.join(rightPattern.replace(/\*/g, path.join('../ClientApp', wildcardValue)));
84-
85-
if (vsCodeChromeDebugUtils.existsSync(pathFixingASPNETBug)) {
86-
mappedPath = pathFixingASPNETBug;
87-
}
88-
}
89-
vsCodeChromeDebugLogger.log(`SourceMap: mapping ${sourcePath} => ${mappedPath}, via sourceMapPathOverrides entry - ${entryStr}`);
90-
91-
return mappedPath;
92-
}
93-
94-
return sourcePath;
95-
};
96-
97-
//--- end part copied from vscode-chrome-debug-core
98-
// tslint:enable
99-
1006
export class NativeScriptSourceMapTransformer extends BaseSourceMapTransformer {
1017
private debugAdapter: NativeScriptDebugAdapter;
8+
private targetPlatform: string;
1029

10310
constructor(sourceHandles: any) {
10411
super(sourceHandles);
12+
this.setupSourceMapPathOverrides();
10513
}
10614

107-
public setDebugAdapter(debugAdapter: NativeScriptDebugAdapter): void {
15+
public setTransformOptions(targetPlatform: string, debugAdapter: NativeScriptDebugAdapter) {
16+
this.targetPlatform = targetPlatform.toLowerCase();
10817
this.debugAdapter = debugAdapter;
10918
}
11019

@@ -119,4 +28,99 @@ export class NativeScriptSourceMapTransformer extends BaseSourceMapTransformer {
11928

12029
return scriptParsedResult;
12130
}
31+
32+
private setupSourceMapPathOverrides() {
33+
// a workaround for https://github.com/NativeScript/nativescript-vscode-extension/issues/252
34+
// tslint:disable
35+
//--- begin part copied from vscode-chrome-debug-core (https://github.com/microsoft/vscode-chrome-debug-core/blob/d1fe8ca062e277a3480879e1fb63a8ef3b48015a/src/sourceMaps/sourceMapUtils.ts#L78)
36+
//
37+
// VS Code - Debugger for Chrome
38+
//
39+
// Copyright (c) Microsoft Corporation
40+
//
41+
// All rights reserved.
42+
//
43+
// MIT License
44+
//
45+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
46+
//
47+
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
48+
//
49+
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50+
51+
sourceMapUtils.applySourceMapPathOverrides = (sourcePath, sourceMapPathOverrides, isVSClient = false) => {
52+
const forwardSlashSourcePath = sourcePath.replace(/\\/g, '/');
53+
// Sort the overrides by length, large to small
54+
const sortedOverrideKeys = Object.keys(sourceMapPathOverrides)
55+
.sort((a, b) => b.length - a.length);
56+
57+
// Iterate the key/vals, only apply the first one that matches.
58+
for (const leftPattern of sortedOverrideKeys) {
59+
const rightPattern = sourceMapPathOverrides[leftPattern];
60+
const entryStr = `"${leftPattern}": "${rightPattern}"`;
61+
const asterisks = leftPattern.match(/\*/g) || [];
62+
63+
if (asterisks.length > 1) {
64+
vsCodeChromeDebugLogger.log(`Warning: only one asterisk allowed in a sourceMapPathOverrides entry - ${entryStr}`);
65+
continue;
66+
}
67+
const replacePatternAsterisks = rightPattern.match(/\*/g) || [];
68+
69+
if (replacePatternAsterisks.length > asterisks.length) {
70+
vsCodeChromeDebugLogger.log(`Warning: the right side of a sourceMapPathOverrides entry must have 0 or 1 asterisks - ${entryStr}}`);
71+
continue;
72+
}
73+
// Does it match?
74+
const escapedLeftPattern = vsCodeChromeDebugUtils.escapeRegexSpecialChars(leftPattern, '/*');
75+
const leftRegexSegment = escapedLeftPattern
76+
.replace(/\*/g, '(.*)')
77+
.replace(/\\\\/g, '/');
78+
const leftRegex = new RegExp(`^${leftRegexSegment}$`, 'i');
79+
const overridePatternMatches = forwardSlashSourcePath.match(leftRegex);
80+
81+
if (!overridePatternMatches) {
82+
continue;
83+
}
84+
// Grab the value of the wildcard from the match above, replace the wildcard in the
85+
// replacement pattern, and return the result.
86+
const wildcardValue = overridePatternMatches[1];
87+
88+
// ***************** CUSTOM CODE START *****************
89+
// handle linked files
90+
let mappedPath = path.isAbsolute(wildcardValue) ? wildcardValue : rightPattern.replace(/\*/g, wildcardValue);
91+
// ***************** CUSTOM CODE END *****************
92+
93+
mappedPath = path.join(mappedPath); // Fix any ..
94+
95+
// ***************** CUSTOM CODE START *****************
96+
// handle platform-specific files
97+
const { dir, name, ext } = path.parse(mappedPath);
98+
99+
const platformFileName = `${name}.${this.targetPlatform}${ext}`;
100+
const platformPath = path.join(dir, platformFileName);
101+
102+
if (vsCodeChromeDebugUtils.existsSync(platformPath)) {
103+
mappedPath = platformPath;
104+
}
105+
// ***************** CUSTOM CODE END *****************
106+
107+
if (isVSClient && leftPattern === 'webpack:///./*' && !vsCodeChromeDebugUtils.existsSync(mappedPath)) {
108+
// This is a workaround for a bug in ASP.NET debugging in VisualStudio because the wwwroot is not properly configured
109+
const pathFixingASPNETBug = path.join(rightPattern.replace(/\*/g, path.join('../ClientApp', wildcardValue)));
110+
111+
if (vsCodeChromeDebugUtils.existsSync(pathFixingASPNETBug)) {
112+
mappedPath = pathFixingASPNETBug;
113+
}
114+
}
115+
vsCodeChromeDebugLogger.log(`SourceMap: mapping ${sourcePath} => ${mappedPath}, via sourceMapPathOverrides entry - ${entryStr}`);
116+
117+
return mappedPath;
118+
}
119+
120+
return sourcePath;
121+
};
122+
123+
//--- end part copied from vscode-chrome-debug-core
124+
// tslint:enable
125+
}
122126
}

src/services/iOSTeamService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class iOSTeamService {
5252

5353
for (const file of files) {
5454
const filePath = path.join(dir, file);
55-
const data = fs.readFileSync(filePath, {encoding: 'utf8'});
55+
const data = fs.readFileSync(filePath, { encoding: 'utf8' });
5656
const teamId = this.getProvisioningProfileValue('TeamIdentifier', data);
5757
const teamName = this.getProvisioningProfileValue('TeamName', data);
5858

@@ -64,7 +64,7 @@ export class iOSTeamService {
6464
const teamIdsArray = new Array<{ id: string, name: string }>();
6565

6666
for (const teamId in teamIds) {
67-
teamIdsArray.push({id: teamId, name: teamIds[teamId]});
67+
teamIdsArray.push({ id: teamId, name: teamIds[teamId] });
6868
}
6969

7070
return teamIdsArray;

src/tests/nativeScriptDebugAdapter.tests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const defaultArgsMock: any = {
2727
};
2828

2929
const mockConstructor = (mockObject: any): any => {
30-
return function() {
30+
return function () {
3131
return mockObject;
3232
};
3333
};
@@ -64,7 +64,7 @@ describe('NativeScriptDebugAdapter', () => {
6464

6565
sourceMapTransformer = {
6666
clearTargetContext: () => undefined,
67-
setDebugAdapter: () => undefined,
67+
setTransformOptions: () => undefined,
6868
};
6969

7070
nativeScriptDebugAdapter = new NativeScriptDebugAdapter({

tslint.json

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@
44
"tslint:latest"
55
],
66
"rules": {
7-
"quotemark": [true,
7+
"space-before-function-paren": [
8+
true,
9+
{
10+
"anonymous": "always",
11+
"named": "never",
12+
"asyncArrow": "always"
13+
}
14+
],
15+
"quotemark": [
16+
true,
817
"single",
918
"avoid-escape"
1019
],
11-
"whitespace": [true,
20+
"whitespace": [
21+
true,
1222
"check-branch",
1323
"check-decl",
1424
"check-operator",
@@ -19,13 +29,27 @@
1929
"newline-before-return": true,
2030
"no-unused-variable": true,
2131
"no-duplicate-variable": true,
22-
"no-unused-expression": [true, "allow-fast-null-checks"],
32+
"no-unused-expression": [
33+
true,
34+
"allow-fast-null-checks"
35+
],
2336
"ter-newline-after-var": true,
24-
"max-line-length": [ true, 160 ],
37+
"max-line-length": [
38+
true,
39+
160
40+
],
2541
"max-classes-per-file": false,
26-
"variable-name": [ true, "allow-leading-underscore" ],
42+
"variable-name": [
43+
true,
44+
"allow-leading-underscore"
45+
],
2746
"forin": false,
28-
"no-implicit-dependencies": [true, "dev"],
29-
"only-arrow-functions": [false]
47+
"no-implicit-dependencies": [
48+
true,
49+
"dev"
50+
],
51+
"only-arrow-functions": [
52+
false
53+
]
3054
}
3155
}

0 commit comments

Comments
 (0)